we are creating an application which allows image upload, now we want to reduce the image size to reduce the page load time, now this is the code i'm using, it doesn't work, image size remains 100% it doesn't reduce, please help maybe i'm missing something or i'm doing something wrong
if(!empty($_FILES['image_field']['name']))
{
$config['upload_path'] = 'image_folder';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '262144';
$config['file_name'] = 'my_image_name';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('image_field'))
{
$this->session->set_flashdata('error','<div class="alert alert-warning">Something went wrong...try again...</div>');
redirect('Controller/function_name');
}
else
{
$type = array('image/png'=>'png','image/jpg'=>'jpg','image/jpeg'=>'jpg');
$config['image_library'] = 'gd2';
$config['source_image'] = base_url().'image_folder'.$config['file_name'].'.'.$type[$_FILES['image_field']['type']];
$config['quality'] = '50%';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
// save to database
}
}
i'm using codeigniter 3.0
You should use without % as integer like that:
$config['quality'] = 50;
$config['image_library'] = 'gd2';
$config['quality'] = 60;
works great adjust the quality and check the image file size it save. will be different.
I also had to set my image_library parameter to "gd" before the quality change started working. I wonder why it didn't work with "gd2"
$config['image_library'] = 'gd';
$config['quality'] = 40;
I found this would not work for me using the GD or GD2 library.
In order for the "quality" to be affected - I needed to use the "ImageMagick" Library.
As stated in the documentation - if you use either the "ImageMagick" or the "NetPBM" library - "you must supply the path": https://codeigniter.com/userguide3/libraries/image_lib.html
On my GoDaddy server the path was "/usr/bin"
$config['source_image'] = $source_image;
$config['library_path'] = '/usr/bin';
$config['image_library'] = 'ImageMagick';
$config['maintain_ratio'] = TRUE;
$config['quality'] = "20%"; // Maintained the aspect ratio and dimensions but reduced the file size by about 60%
$this->image_lib->initialize($config);
if(!$this->image_lib->resize()){
echo "QUCKVIEW Re-sizing - ";
echo $this->image_lib->display_errors();
echo "Image Resize for Grooming Quickview Failed! Contact System Administrator.";
die();
}
Related
I have a piece of code where I compress images via Codeigniter gd2 library. However, it turns this photo
into this one
because I set width and height in my code. However, when I remove these 2 lines, $config['quality'] property does not work. How can I solve this problem and save the actual size of the image compressing it?
Here is my code:
$image_datar = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/img/single_courses/'.$image_datar["file_name"];
$config['maintain_ratio'] = false;
$config['quality'] = '60%';
$config['width'] = '750';
$config['height'] = '500';
$config['new_image'] = './assets/img/single_courses/'.$image_datar["file_name"];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$post_image = $image_datar["file_name"];
It's a bit confusing what you are trying to accomplish even after reading the comments, and your code as it specifies the width. If you want it to be 750x500 by have the same ratio (so things don't look "compressed") than you should have $config['maintain_ratio'] = true; which generates:
If you just want to reduce the quality of the image so that the file size is smaller yet keep the same dimensions of the original image than one would think you can comment out the width and height of the config. However, in my testing that yielded the exact same file size as the original image, even when changing the quality down to 10%. Weird, bug? I confirmed this with filesize() and in my os!
Turns out not a bug, but a coded feature:
// If the target width/height match the source, AND if the new file name is not equal to the old file name
// we'll simply make a copy of the original with the new name... assuming dynamic rendering is off.
if ($this->dynamic_output === FALSE && $this->orig_width === $this->width && $this->orig_height === $this->height)
{
if ($this->source_image !== $this->new_image && #copy($this->full_src_path, $this->full_dst_path))
{
chmod($this->full_dst_path, $this->file_permissions);
}
return TRUE;
}
I've tried to figure out a way to get around this but that would require overwriting and messing with the libraries functionality. For now, and if your ok with your image being 1px less in width and height than the original, you can do something like:
$source = $this->frontend_image_upload_path . 'org/upload_original.jpg';
list($width, $height) = getimagesize($source);
$config['image_library'] = 'gd2';
$config['source_image'] = $source;
//$config['maintain_ratio'] = true;
$config['quality'] = '20%';
$config['width'] = $width - 1;
$config['height'] = $height - 1;
$config['new_image'] = $this->frontend_image_upload_path . 'org/new_image.jpg';
$this->load->library('image_lib', $config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
i have a problem with resizing image in CI ( i'm new to CI by the way ).
so here is my code :
$config['image_library'] = 'gd2';
$config['source_image'] = base_url()."/uploads/test.png";
$config['maintain_ratio'] = TRUE;
$config['width'] = 800;
$config['height'] = 600;
$config['new_image'] = base_url().'/uploads/resized.jpg';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->resize();
echo $this->image_lib->display_errors();
echo $config['source_image'];
it give me this error :
The path to the image is not correct. Your server does not support the
GD function required to process this type of image.
i'm using MAMP as a server and i see GD enabled on phpinfo.
i also try to echo the image url , and yes its really there.
please help.
Thanks
You have to use relative path and you can try with this one
$this->load->library('image_lib');
// Set your config up
$this->image_lib->initialize($config);
// Do your manipulation
$this->image_lib->clear();
Please see this one
I am using ImageMagick library in CodeIgniter for re-sizing and rotating image using image library. But its generating error. The error is -"The path to your image library is not correct. Please set the correct path in your image preferences."
$config = array();
$config['image_library'] = 'ImageMagick';
$config['source_image'] = $file;
$config['new_image'] = $file;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->image_lib->initialize($config);
if ( !$this->image_lib->resize())
{
echo "resize -".$this->image_lib->display_errors();
}
$this->image_lib->clear();
Below has given me expected result. Hope you will also get same.
$this->load->library('image_lib');
//For resizing of image in size of dilog
$config['image_library'] = 'ImageMagick';
$config['library_path'] = 'C:\\ImageMagick\\';
$config['source_image'] = $source_filepath;
$config['new_image'] = $new_filepath;
$config['width'] = 128;
$config['height'] = 128;
$config['quality'] = '100%';
$config['maintain_ratio'] = TRUE;
$this->image_lib->initialize($config);
if (! $this->image_lib->resize()) {
$error_msg = $this->image_lib->display_errors();
print_r($error_msg);
}
else {
echo "Done";
}
Here
$config['library_path'] = 'C:\\ImageMagick\\';
is the path for windows where your imageMagick application is installed.(Try to install in such a folder to which we can easily map it for library path).
Change the image library to :
$config['image_library'] = 'ImageMagick';
& other all configuration is remains same.
I think you are not giving correct path basically imagemagick is installed somewhere ideally in /user/bin. When I worked I use to give like this
imageMagickConvert = /usr/bin/convert
imageMagickComposite = /usr/bin/composite
$config['library_path'] = '/usr/bin';
add this line in your config.
Hi all : ) Thank you in advance for any assistance with this question.
I am using CodeIgniter and have images uploading, rotating and resizing successfully.
It is when I try to create multiple sizes fro an image that I am a little lost.
Here is my code:
// First I make two copies of the origional image - there is no problem here - these always work. I always have the successfully copied images.
$Copy_Mid_Size_File = "Some_Path_For_The_Newly_Copied_Mid_Size_Image"
if(!copy($source_image, $Copy_Mid_Size_File)){
echo "failed to copy $Copy_Mid_Size_File - Please contact the system administrator. \n";
die();
}
$Copy_Thumbnail_Size_File = "Some_Path_For_The_Newly_Copied_Thumbnail_Size_Image"
if(!copy($source_image, $Copy_Thumbnail_Size_File)){
echo "failed to copy $Copy_Thumbnail_Size_File - Please contact the system administrator. \n";
die();
}
// Now I resize for the mid size image
$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/bin';
$config['source_image'] = $Copy_Mid_Size_File;
$config['maintain_ratio'] = TRUE;
$config['quality'] = '100%';
$config['master_dim'] = 'auto';
$config['width'] = 200;
$config['height'] = 200;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()){
echo $this->image_lib->display_errors();// This has never reported an error yet
echo "Image Re-size for Mid Size Image FAILED!";// This has never reported an error yet
die();
}else{
echo" Mid-Size Re-size Worked!";
}
// Now I try to resize for the Thumbnail
$config['image_library'] = 'imagemagick';
$config['library_path'] = '/usr/bin';
$config['source_image'] = $Copy_Thumbnail_Size_File;
$config['maintain_ratio'] = TRUE;
$config['quality'] = '100%';
$config['master_dim'] = 'auto';
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()){
echo $this->image_lib->display_errors();// This has never reported an error yet
echo "Image Re-size for Thumbnail Size Image FAILED!";// This has never reported an error yet
die();
}else{
echo" Thumbnail Re-size Worked!";
}
I always end up with the proper number of images correctly named - The thumbnail image just does not re-size - there is no error reported. It always says that is succeeded.
If I put the thumbnail re-size code first - the thumbnail re-sizes correctly - but then the mid-size image does not.
I realize there are other ways to load the libraries - but I don't see why the first re-size works but the second does not.
Any ideas?
Thanks.
Regards,
Ken
You only need to load the library once, but then you need to initialize it with the new config each time. So your code will look like this:
// load the library
$this->load->library('image_lib');
// first image:
// set some config
$config['image_library'] = 'imagemagick';
...
// initialize the library with this config
$this->image_lib->initialize($config);
// Do the resize:
$this->image_lib->resize();
// clear the config:
$this->image_lib->clear();
// second image:
// set some config
$config['image_library'] = 'imagemagick';
...
// re-initialize the library with the new config
$this->image_lib->initialize($config);
// Do the resize:
$this->image_lib->resize();
I suspect you could even just update $config['width'] and $config['height'] without having to clear, but I've not tested that!
As a side note, you don't really have to copy the image into the correct directories first; the CodeIgniter image library can create a new one for you, if you set your config like this:
$config['new_image'] = '/path/to/new_image.jpg';
This will save the image into the new path without having to create copies all over the place.
Reference: http://codeigniter.com/user_guide/libraries/image_lib.html
Following is the code which resize the image, but here i am not able to resize the image
function processHome(){
$this->load->library('image_lib');
$img_path = base_url().'img/image/50X50/ori.jpeg';
$config['image_library'] = 'gd2';
$config['source_image'] = $img_path;
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 50;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
if ( ! $this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
echo "No error";
exit;
$this->load->view('index', $data);
}
For a start, remove the
$this->load->library('image_lib');
at the beginning - you only need to load the library once, and only after you set the parameters.
Then, give the relative/server path to your image folder, instead of the url - as in, no base_url().
Finally, check the permissions for the folder the image is in - it must be readable/writable by all, and php should be allowed to create new files.
I guess that's all.