I am using CodeIgniter to resize image, but the small image I have resized is low quality.
Here is my code:
$config = array(
'image_library' => 'gd2',
'quality' => '100',
'source_image' => $temp_full_path,
'new_image' => $full_temp_path."img_mobile/",
'maintain_ratio' => true,
'create_thumb' => false,
'width' => 50,
'height' => 50
);
$this->image_lib->initialize($config);
$this->image_lib->resize();
How can make resize image with high quality like it's original image?
Here is original image, image resize generate from php code and image resize generate from codeigniter: Image
Looking at the source code, GD2 doesn't use the "quality" parameter.
But some of the other libraries do use it.
Give ImageMagick a try.
You should write 'quality' => '100%' instead of only '100'
Default:
$config['image_library'] = 'gd2';
Alternative:
$config['image_library'] = 'gd';
$config['quality'] = 100;
Best Quality:
$config['image_library'] = 'imagemagick';
$config['library_path'] = "/usr/bin";
$config['quality'] = 100;
Ref: https://forum.codeigniter.com/thread-17374.html
It's a small mistake or a pitfall.
Do:
'quality' => '100%',
instead of
'quality' => '100',
Should work, according to docs.
EDIT:
There seems to be another typo!
'maintain_ration'
should be
'maintain_ratio'
Also, how can new_image end with a /? you are doing something wrong there.
Related
I am using CKFinder PHP 3.5.3.
When I upload an image, it creates only one default 150x150 thumbnail currently.
I tried the following configuration of a thumbnail on the config.php, but it doesn't work:
$config['thumbnails'] = array(
'enabled' => true,
'sizes' => array(
array('width' => '150', 'height' => '150', 'quality' => 80),
array('width' => '300', 'height' => '300', 'quality' => 80),
array('width' => '500', 'height' => '500', 'quality' => 80),
),
'bmpSupported' => true,
);
I would like to create multiple thumbnails such as 150x150, 300x300 and 500x500 when upload an image.
How can I generate multiple thumbnails when I upload an image?
In CKFinder 3 thumbnails of different sizes are generated lazily, on demand. This means the thumbnail of a given size will not be generated until requested by CKFinder's frontend. It's possible to change this behavior with a plugin. Here you can find a plugin I have created that enforces CKFinder to generate all the private thumbnails immediately after the file is uploaded.
I am using CI version 3.1.2 to process an image and using Windows 8. My problem is every time i process an image and trying to create a thumbnail, i am countering a message "Unable to save the image. Please make sure the image and file directory are writable". This is occurred when trying to save the copy of thumbnail into different location. Here are my codes processing the image:
$this->original_path = realpath(APPPATH.'../assets/galery/full');
// Uploading image
$config = [
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->original_path,
'max_size' => 2048,
'maintain_ratio' => true,
'file_name' => 'img_'.strtotime(date('d-m-y-H-i-s')), // miss type
'overwrite' => true,
'remove_spaces' => true
];
$this->load->library('upload', $config);
$this->upload->do_upload('galery');
$file_name = $this->upload->file_name;
// Processing image, success processing image
$this->load->library('image_lib');
$img = [
'image_library' => 'gd2',
'source_image' => $this->original_path.'/'.$file_name,
'maintain_ratio' => true,
'width' => 600
];
$this->image_lib->initialize($img);
$this->image_lib->resize();
// This is fail, with message "Unable to save the image... using(var_dump())
$thumb = [
'source_image' => $this->original_path.'/'.$file_name,
'new_image' => '/assets/galery/small',
'maintain_ratio' => true,
'width' => 200
];
$this->image_lib->initialize($thumb);
$this->image_lib->resize();
I've tried different config, like using realpath(APPPATH.'../assets/galery/small'), but still no luck. It is success, by the way, when create config using 'create_thumb'.
But my purpose is, saving that thumbnail in another folder. How can i achieve that?
UPDATE
This change seem to work well. I change realpath(APPPATH.'../assets/galery/full') to realpath(APPPATH.'../assets/galery') and make several change in:
...
'upload_path' => $this->original_path.'/full'
...
'source_image' => $this->original_path.'/full/'.$file_name
...
'new_image' => $this->original_path.'/small/thumb_'.$file_name
Until now i'm still trying to make my code simple and put it in MY_Model. Thanks for all comment(s). If some one may have better solution, it would be nice to share here.
I want to resize the uploaded image as the ratio of 400 x 350. I use below code for this.
$config = array(
'image_library' => 'gd2',
'source_image' => $img_data['full_path'],
'new_image' => $this->upload_path. '/thumbs',
'maintain_ration' => FALSE,
'width' => 350,
'height' => 400
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Codeigniter give me the image as the ratio of 400 x 340.
see if this could help.
Here is the link to the class required to accomplish the task you looking for
http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/
I have two resize functions that I would like to perform one after the other using codeigniters image manipulation class:http://codeigniter.com/user_guide/libraries/image_lib.html
At the moment when I try running the function only the first one works, it ignores the second.
I have added $this->image_lib->clear() which according to codeigniters user guide: 'resets all of the values used when processing an image. You will want to call this if you are processing images in a loop.'
Why can't I run the two seperate resize functions? How would I go about doing this??
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $upload_path . '/thumbs/',
'maintain_ration' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->image_lib->clear();
$config = array(
'source_image' => $image_data['full_path'],
'maintain_ration' => true,
'width' => 620,
'height' => 410
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
If you don't want to use clear then store configs in two different arrays.
$this->image_lib->clear(); is used to flush previous settings & to add new.
i have a problem while uploading and resizing images in a loop.
can anyone please provide me the working sample of code of codeigniter for uploading and resizing at the same time in a loop.
I want to upload and resize images uploaded from the form. There will be more than 1 images so i have to upload them in loop.
My code first uploads the image then resizes it. 1ts images is uploaded and resized correctly but during 2nd loop the image is uploaded but not resized. It throws this error:
Your server does not support the GD
function required to process this type
of image.
I have tried the clear function too
$this->image_lib->clear();
can anyone please help
Dont load image_lib multiple times. Add image_lib in autoload libs and change
$this->load->library('image_lib', $config);
to
$this->image_lib->initialize($config);
I had the same problem but this seemed to work:
// Do upload
if (! $this->upload->do_upload($image_name)) {
// return errors
return array('errors' => $this->upload->display_errors());
}
$data = $this->upload->data();
$config_manip = array(
'image_library' => 'gd2',
'source_image' => "./assets/uploads/{$data['file_name']}",
'new_image' => "./assets/uploads/thumbs/{$data['file_name']}",
'create_thumb' => true,
'thumb_marker' => '',
'maintain_ratio' => true,
'width' => 140,
'height' => 140
);
// Create thumbnail
$this->load->library('image_lib');
$this->image_lib->resize();
$this->image_lib->clear();
$this->image_lib->initialize($config_manip);
if ( ! $this->image_lib->resize()){
return array('errors' => $this->image_lib->display_errors());
}
Notice the Create Thumbnail goes:
Load Library,
Resize Image,
CLEAR,
Initialize Config
I was putting the clear after the initialize which was causing the same error you're getting.
Here is a working code from my image gallery controller. This function uploads a batch of images, resizes them and saves them to database.
public function create_photo_batch() {
$this->load->library('image_lib');
$this->load->library('upload');
// Get albums list for dropdown
$this->data['albums'] = $this->gallery_m->get_albums_list();
if(isset($_FILES['images']) && $_FILES['images']['size'] > 0) {
$album_id = $this->input->post('albumid');
$images = array();
$ret = array();
// Upload
$files = $_FILES['images'];
foreach ($files['name'] as $key => $image) {
$_FILES['images[]']['name']= $files['name'][$key];
$_FILES['images[]']['type']= $files['type'][$key];
$_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
$_FILES['images[]']['error']= $files['error'][$key];
$_FILES['images[]']['size']= $files['size'][$key];
$upload_config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => realpath(APPPATH . "../uploads/gallery"),
'max_size' => 5000,
'remove_spaces' => TRUE,
'file_name' => md5(time())
);
$this->upload->initialize($upload_config);
if ($this->upload->do_upload('images[]')) {
$image_data = $this->upload->data();
$images[] = $image_data['file_name'];
} else {
$this->session->set_flashdata('error', $this->upload->display_errors() );
redirect('admin/gallery/create_photo_batch');
}
}
// Resize
foreach ($images as $image) {
$resize_config = array(
'source_image' => realpath(APPPATH . '../uploads/gallery') .'/'. $image,
'new_image' => realpath(APPPATH . '../uploads/gallery/thumbs'),
'maintain_ratio' => TRUE,
'width' => 500,
'height' => 500
);
$this->image_lib->initialize($resize_config);
if ( ! $this->image_lib->resize() ) {
echo $this->image_lib->display_errors();
die;
}
$this->image_lib->clear();
}
// Save to db
foreach ($images as $image) {
$ret[] = array(
'AlbumID' => (int) $album_id,
'Url' => $image,
'IsActive' => 1
);
}
$this->gallery_m->save_images($ret);
redirect('admin/gallery/album/'.$album_id);
}
//Load view
$this->data['subview'] = 'admin/gallery/photo_upload_batch_view';
$this->load->view('admin/_layout_main', $this->data);
}
Your error message suggest that it's not the loop that is the problem, but rather that the 2nd file is of a different filetype than the 1st. And that the underlying server don't have the needed libraries (http://www.libgd.org/Main_Page) installed to handle that file type.
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/upload_images/A.jpg';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 1600;
$config['height'] = 900;
$config['new_image'] = './assets/upload_images/'.$randy;
$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
I had same problem but solved these by this code.