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.


Post this entry to: » del.icio.us  » Digg  » Furl  » Newsvine  » reddit  » Y!

Somewhat related posts

9 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 […]

Leave a Reply