Laziest image resize in PHP

Today I saw a post at digg.com 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:

<?php
require_once 'Image/Transform.php';
$i =& Image_Transform::factory('');

$i->load('test.jpg');
$i->fit(100,100);
$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.

This entry was posted on Wednesday, December 13th, 2006 and is filed under images, PEAR, php. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

17 Responses to “Laziest image resize in PHP”

  1. PHPDeveloper.org Says:

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

  2. Marc Jakubowski Says:

    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. Jamees Says:

    @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. jr Says:

    @Jamees

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

  5. Michael Jackson Says:

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

  6. Pol Clause Says:

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

  7. a tvorog Says:

    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. a tvorog Says:

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

  9. InstaHippo » Image Resize Says:

    [...] 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. Jim Says:

    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. www.redips.net | Resize images with PHP script Says:

    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. joey Says:

    Nice code lines I myself use exactley the same method! PEAR is great but if you like OO programming check out Zend Framework! (http://framework.zend.com/) 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. George P. Says:

    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. Thomas Says:

    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. John Says:

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

  16. Mayur Says:

    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. fhgjuyyh Says:

    horrible

Leave a Reply