PHP: Object Oriented Image Manipulation

I’ve been working on a CMS lately and having to create thumbnails for uploaded images is always a pain, lots of maths working out the correct sizes and such, so I’ve created a fairly small script to manipulate images in an object-oriented style.

For example:

scale(400, 300);
$image->write('small-image.jpg');
 
$image2 = new Image('image2.jpg');
$image2->watermark = 'sample.png';
$image2->output();

I’ve only implemented a few of the GD library methods, but I think these are the most useful methods. I might have to work on something that rounds the corners too, but I don’t have any need for it just yet.

There’s a demo page as ever, and you can get the script here (It’s a bit big, because the sample image is included, here’s one without it).

4 thoughts on “PHP: Object Oriented Image Manipulation

  1. Beautyfull class!!!

    A contribution:
    Take a look at lines 184 AND 349, it seems to be a typo

    throw new Exception(‘Imgae::

    I think you mean

    throw new Exception(‘Image::

    • I wonder how many times I corrected that typo while creating it too. One of my most common typos along with ‘webiste’ I think.

      Thanks for commenting, I’ll get it fixed up soon as possible! :)

  2. Thanks for sharing such script.
    I am facing one problem with this class.
    This class is not working in php 4.
    If I modified this class for php4 removing private, protected and public functions are not working fine.
    Have any solutions for this?

    • Hi Monsur,

      I haven’t actually tested the script on PHP4 I’m afraid. I don’t have a PHP4 environment to test it on either…

      I assume you’ve changed the majority of function-names accordingly (__construct -> Image, etc) if it still doesn’t work, it’s possible that I’m using features that are PHP5 only… You may be able to pull apart what I’ve done and reconstruct a PHP4 version but since my primary platform is PHP5 i haven’t worked on it…

      Sorry about that, I’d be interested if you manage to get the script working in PHP4, you might be able to help someone else out…

      Dom

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>