Skip navigation

Tag Archives: Image Manipulation

Some websites that has photo hosting services might require standard specs for pictures stored on the server. Specs like maximum size, max resolution, certain compression and a fixed file format. Since you can’t force the user to abide to those specs, your best option is to adapt the uploaded images to your specs.

I thought at the beginning that CI is not capable of doing that, despite its outstanding image manipulation class. But I could not find any clue on how to do it. Even some people on irc said it’s not possible with CI and I should find another way. However, by pure coincidence I discovered this possible if you set ImageMagick as the default image manipulation library, and all you’ve got to do is rename the file.

$config[‘image_library’] = ‘ImageMagick’;
$config[‘library_path’]=’/usr/bin’;
$config[‘source_image’]=’/images/picture.png‘;
$config[‘new_image’]=’/images/picture.jpg‘;
$this->load->library(‘image_lib’,$config);

The above code first sets the default image library to ImageMagick. If you use a library other than the default (gd2), then you must specify its path (line 2). Then I specified the source image and the new image paths. Notice the change of extension from png to jpg. Then load the library. That’s all. Now do whatever manipulation you want on the image (resize, crop or whatever) and the new image will be saved converted to the new format.

Advertisements