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.
";
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.
";
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