Laziest image resize in PHP

December 13th, 2006. Tagged: images, PEAR, php

Today I saw a post at on image resizing with PHP and there was quite a discussion. Let me share the laziest way (that I know of) how to do it - PEAR::Image_Transform is all it takes. Here goes:

require_once 'Image/Transform.php';
$i =& Image_Transform::factory('');
$i->save('resized.png', 'png');

In addition, the Image_Transform library offers diffferent ways (to skin the old cat) to resize an image - by given pixel value, only on the X axis, on Y, scalling in percentage and so on. And, of course, the library can do much more than resizing, as you can see in the API docs.

It supports all kinds of image manipulation extensions - GD, GD1, ImageMagick, NetPBM, Imlib... If you want to use a specific one, you set as a parameter to the factory() method. In the example above I passed an empty string, so it will try to figure out what's available in my PHP setup and use it, trying Imagick2 first, then GD, then Imlib.

You have setOption() and setOptions() methods if you want to play around with the image quality and those sort of things.

Tell your friends about this post: Facebook, Twitter, Google+

19 Responses

  1. Stoyan Stefanov’s Blog: Laziest image resize in PHP…

  2. One drawback I find disturbing about this package is that you can only do one tranformation per image/object. When you want to do a resize and then a crop you get a notice that only one transformation is allowed, if I remember correctly.

  3. @Marc

    I found many of PEAR’s libraries to be lacking in substance. For image transformation, i have my own lib. As for the case that you pointed out, the code using my lib would be this:

    $img = Momo_Image::load(‘test.png’);
    $resized = Momo_Image::resize($img, array(‘width’ => 500)); // It’ll figure out the height by proportion
    $cropped = Momo_Image::crop($resized, array(‘start_x’ => 10, ‘start_y’ => 100, ‘width’ => 200, ‘height’ => 300)); // If you omit start_x or start_y, they will default to 0
    Momo_Image::render($cropped, array(‘file_path’ => ‘path/to/file.png’));

    I avoid Object Oriented Programming for all of my libraries. Makes my life easier when refactoring.

  4. @Jamees

    is your image library avaliable?
    was planning to use Pear, but I agree it is a little basic.

  5. Right on man. The best coders are lazy coders. Thanks for the tip!

  6. I’m using very simply program picture resizer, and I have not any problem.

  7. Hi.
    Sorry for the perheps dumb question, but I’m really having troubles with using PEAR libraries since I’m not good at all at OOP.
    So when trying to use Image_Transform it tells me “Image library not supported… aborting”.
    Could you explain how to fix it?

    I suppose that the problem is in the way the PEAR is installed at my web-site.
    I don’t know if it is installed at my host, so at some foums I was suggested just to place it in a local folder at my web-site and just include it to my pages.
    The PEAR extantions I ‘plug’ in the same way.
    Do you think there’s any better solution?

  8. Sorry for flood.
    I fixed my problem.
    I had to just to change all the path in icludes.

  9. [...] In addition, the Image_Transform library offers diffferent ways (to skin the old cat) to resize an image – by given pixel value, only on the X axis, on Y, scalling in percentage and so on. And, of course, the library can do much more than resizing, as you can see in the API docs.4 [...]

  10. There is no need to host CPU-intensive thumbnailing scripts like this on your webserver anymore. Instead, you can use a specialized service for remote, on-demand image manipulation like SteadyOffload.

    All you have to do is use a custom HTML attribute called “xmanip” with the img tag. This will deliver the thumbnail from one of the globally scattered cache servers:

    <img srcx=”image.jpg” xmanip=”RescaleWidth 130″ />

    So much easier than all the hassle with GD or ImageMagick! Moreover, this reduces the CPU load of your webserver and saves some bandwidth.

  11. Nice & short script … but
    if you need simple PHP script to resize many images at once from command line, I have one. I also used ImageMagick tools, so PHP script will work without GD library. Hope this will help …

  12. Nice code lines I myself use exactley the same method! PEAR is great but if you like OO programming check out Zend Framework! ( It is a superb collection of classes and I risk that is better than PEAR.

    And Jim: That’s cool to provide such service but when it comes to sensitive data you cannot really afford to outsource your image data processing. On the other hand resizing a picture (once at initial processing/adding to database etc) should not raise such a big CPU load. Taking that these libraryies are single threaded and a really lot of servers are multi-cored and users are resource limited if should not be a risk.
    So I do not think it is a good idea to outsource image resize jobs. The optimal solution is still making your own cache of resized images and store image information in database.

  13. I was googl’ing and found the same Digg discussion you were talking about – and the first thing that came to my mind was ‘doesn’t PEAR do this?’… and then I stumbled on this post :)

  14. I am getting the below error

    Fatal error: Call to undefined method PEAR_Error::load() in /home/knowtho2/public_html/new1/photos/upload_file.php on line 41

    code : $i->load(‘test.jpg’);

    Am I missing any pear or Image libraries?

  15. Wow, this is so simple! You just saved me an hour. Thanks!

  16. Ok, how to use this if the image names are coming out of database through a while loop? Thats how i am displaying images and thats where i need to resize the images.

  17. horrible

  18. Unquestionably imagine that which you said. Your favorite justification appeared to be on the net the simplest factor to take into accout of. I say to you, I definitely get irked at the same time as people think about worries that they plainly do not recognise about. You managed to hit the nail upon the highest and outlined out the entire thing with no need side effect , folks could take a signal. Will probably be back to get more. Thanks

  19. Great post. I am going through some of these issues as

    Also visit my web site … waist to hip ratio chart

Leave a Reply