Skip navigation

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

5 Comments

  1. Actually it doesn’t convert images that way- browsers will recognize them with changed extensions, but if You try to open the converted image with e.g. Photoshop, it won’t recognize the image. If You try Photoshop’s “Open As..” command and specify the previous file type (png in Your case)- it opens.

  2. I compared the converted images with images that I manually changed their extensions. Definitely the images are converted correctly. I even opened the images with a hex editor to check. This is of course provided when you are using ImageMagick, not GD2. Actually if you think about, ImageMagick converts images in way as simple as this:

    convert x.jpg x.png

    run in console, so it might be detecting extension change in $config[‘new_image’], and so it invokes convert

    Sorry for the late reply.

  3. Do you know how to change PHP’s GD extension for processing images, which is not good enough, to ImageMagick, which part of Social Engine script we have to change ?

  4. What are you pointing at with $config[‘library_path’]=’/usr/bin’; ?

    I need to do this on localhost – but i can’t seem to find an imagemagick library.


One Trackback/Pingback

  1. […] Convert Image File Format […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: