UPDATE: THE ANSWER IS USE THE GD2 LIBRARY, DOH!
I am working with Codeigniter's image manipulation library to resize some photos. Unfortunately, they are producing a blue tint or hue to the photos. Not sure why this is and needed to see if it was something I am doing. Here is the code I am using to create the thumb's. Let me know if you want to see image links, and I will upload them somewhere.
$this->load->library('image_lib');
$config['image_library'] = 'GD';
$config['source_image'] ="images/IMG_0007.jpg";
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = '450';
$config['height'] = '450';
$this->image_lib->initialize($config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
I strongly recommend you to use ImageMagick for image resizing. It respects color profiles, is slightly faster, less memory hungry and generally produces better quality. See this question:
How to stop GD2 from washing away the colors upon resizing images?
If you do not have ImageMagick installed, this may be of help:
http://ferdychristant.com/blog//archive/DOMM-8GAFGL
Related
I have a project in which i need to select whole folder with pictures inside it, and then do a transformation on all of the pictures inside the folder. Something similar like scripts in photoshop for massive pictures adjustment.
Check PHP's scandir() function
Loop through the results of your directory and do your image manipulation on each item.
Well, CodeIgniters Manual does have some helper functions, but I'm not using them myself.
Personally, I'm more comfortable using the DirectoryIterator PHP supplies you with. Combining this with CodeIgniters Image Manipulation Class you can achieve some pretty nifty handling of your image processing:
$images = array("jpg", "png", "bmp"); // Just to make sure that we get image files
foreach(new DirectoryIterator("/path/to/images/") as $file)
{
if( $file->isFile() && in_array($file->getExtension(), $images) )
{
$config['image_library'] = 'imagemagick';
$config['library_path'] = 'usr/bin/local/';
$config['source_image'] = $file->getPathname();
$config['new_image'] = $file->getPathname(); //Overwriting the source image
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
}
Or do some image processing of your own choice.
If you are using linux, you can mass edit the images using ImageMagick's mogrify command.
Example: mogrify -resize 150x150 *.jpg
Result: all images were resized to 150x150
There is also a PHP class that deals with ImageMagick functionality in PEAR.
I don't think there is a function that could perform multiple image processing. But you could select files with glob() and then do your process in a loop ?
I am looking to accomplish this in CodeIgniter specifically.
The PHP App I am coding allows a user to upload either a jpg or an animated gif image. On the next step I want to allow the user to use jCrop to crop a few different size thumbnails. This would require me to convert a new copy of the animated gif to a jpg. My code works fine with uploaded jpg images, but creates a broken image for gif files. Is what I am trying to do possible?
My Code:
// Create image to crop
$config['image_library'] = 'ImageMagick';
$config['library_path'] = '/usr/bin';
$config['source_image'] = $this->config->item('upload_dir_path') . $file_path . 'original.' . $file_ext;
chmod($config['source_image'], 0777);
$config['new_image'] = $this->config->item('upload_dir_path') . $file_path . 'crop-me.jpg';
$this->image_lib->initialize($config);
$this->image_lib->resize();
For those interested in my solution, I simply used the built in GD PHP functions. I have little experience dealing with gif files so I was expecting this to be difficult. The fact of the matter is the CodeIgniter Image_lib and extended library (Which I never got to work properly) is overkill for this.
Here is what I used:
$image = imagecreatefromgif($path_to_gif_image);
imagejpeg($image, $output_path_with_jpg_extension);
Very easy and worked perfectly for what I needed.
When I am creating thumbnail in codeigniter I get an image with black background color.
Why did it happen?
This is the code in controller:
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $file ;
$config['maintain_ratio'] = FALSE;
$this->image_lib->initialize($config);
$this->md_image->resize_image('./images/logo_images/'.$filename,380,80,'./images/logo_images/thumbs/'.$filename);
It is a PHP and GD2 problem, and not taken into account in the Image Lib in CodeIgniter.
Have a look here in the PHP manual to be able to keep transparency on resize :
http://www.php.net/manual/en/function.imagecolortransparent.php
Stéph
Hey :) I want to resize an image when I address it in a form(save to database). I dont think it will be a problem [pointing to the image etc, however the space I am going display the thumb in is 228 x 228. I know I can maintain the ratio and determine the size in the controller, yet how is it done on images that are frequently different sizes?
Is the best way to address this is to put pre-calculated values(make my own ratio) in my form so that the controller can use those values to determine max height and width? See code.
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75; // use $this->input->post('width') instead??
$config['height'] = 50; // use $this->input->post('height')
Or is there a way to tell the function no bigger than 228 and no wider than 228?
Thanks
I am not sure what you want exactly, but by using the following settings you can make sure either the width or the height of the image is 228px. If width > height, width will be 228px. And vice versa.
$config['maintain_ratio'] = TRUE;
$config['master_dim'] = 'auto' // auto is default, so you can leave this out
$config['width'] = 228;
$config['height'] = 228;
master_dim
Specifies what to use as the master
axis when resizing or creating thumbs.
For example, let's say you want to
resize an image to 100 X 75 pixels. If
the source image size does not allow
perfect resizing to those dimensions,
this setting determines which axis
should be used as the hard value.
"auto" sets the axis automatically
based on whether the image is taller
then wider, or vice versa.
If this is not what you mean, please add a bit more explanation.
getImageSize()
I think you'd have to throw some logic in your controller to handle this. Using the function above you can test the height/width of the uploaded image and resize if necessary.
I have uploaded an image with the resolution 1600x1200. How can I show users 60x60 and 200x200 without creating thumbnails and uploading again. (like phpthumb library) in CodeIgniter?
http://codeigniter.com/forums/viewthread/155086/
That's the timThumb controller for CI.
Codeigniter comes with its own image library which can resize images. This example is from the documentation, just edit the $config variable:
$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['new_image'] = '/path/to/new/image.jpg';
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
You can also create thumbnails dynamically (that is, without saving them to the hard drive) but that requires a lot of memory so I would discourage that.
The image class can also crop the image if you need that. I use it to create thumbnails in the application I am currently working on...
Visit the documentation: http://codeigniter.com/user_guide/libraries/image_lib.html