Page 1 of 2

OOP Image Handling

Posted: Mon Jul 14, 2008 7:15 pm
by allspiritseve
Hello all,

I am at the point where I need to start incorporating image handling into my apps. I think I will be using the GD library, since it is already packaged with PHP. I feel a bit hypocritical using functions, though, when the rest of my code is exclusively object-oriented. What should I do? Should I whip up a wrapper to work with images? Is there one with a simple interface I could use? I'm not looking for anything terribly complex, in fact all I will be doing so far is resizing/resampling.

Thanks,

Cory

Re: OOP Image Handling

Posted: Mon Jul 14, 2008 7:18 pm
by Eran
This one is nice for simple resizing / resampling - http://codeutopia.net/blog/2008/07/08/a ... humbnails/

Re: OOP Image Handling

Posted: Mon Jul 14, 2008 7:40 pm
by alex.barylski
http://code.google.com/p/php-image/

Onion2k is a frequent member so if you have any troubles you acn get help quickly...

Re: OOP Image Handling

Posted: Tue Jul 15, 2008 12:16 am
by Ollie Saunders
There must be others too. I've wanted this many times myself and always considered writing one.

Re: OOP Image Handling

Posted: Tue Jul 15, 2008 2:44 am
by dbevfat
You can also use WideImage (http://wideimage.sourceforge.net/wiki/MainPage), which is fullt object-oriented and fairly clean and simple:

Code: Select all

wiImage::load('image.png')->resize(50, 30)->saveToFile('new-image.jpg');
best regards

Re: OOP Image Handling

Posted: Tue Jul 15, 2008 3:47 am
by onion2k
There are a few options. If all you're doing is resampling stuff it won't make much difference which you use because essentially it'll just be a wrapper for imagecopyresampled(), so just try a bunch and pick the one whose syntax is most like your style of coding.

Personally, of course, I'd recommend my PHP Image library. It's early days but it does a lot already. The current "contact sheet" of examples:

Image

(I'm rather proud of the drop shadow thumbnail ... it's exactly the same as Photoshop :) )

Re: OOP Image Handling

Posted: Tue Jul 15, 2008 4:02 am
by Ollie Saunders
Impressed ole is impressed

Oh wait I'm not ole anymore, I'm almost sad :-(

Re: OOP Image Handling

Posted: Tue Jul 15, 2008 4:15 am
by onion2k
Cheers ole (You'll always be ole to me). :oops:

Re: OOP Image Handling

Posted: Tue Jul 15, 2008 8:10 am
by inghamn
Two basic recommendations.

Keep the images files on the hard drive in some human-discernable path. eventually, you'll have to dig through them by hand for some reason or the other. I use the date of the original upload in the path. Yes it means looking up some information on the image, but it ends up being a whole lot faster to fix a problem with something later on. I use something like: /media/image/jpg/$year/$mon/$mday/$id.jpg (Of course it goes without saying that you still store all the meta information in the database.)

Use ImageMagick instead of GD. GD is slow and memory intensive. You always end up bumping up against PHP's memory limit. Even if you have to wrap exec() calls to get ImageMagick going, it's way faster. If you're using the latest version of PHP, you can compile PHP with native support for ImageMagick functions - haven't tried it myself, yet. My wrapper functions work well so far...one day, though.

Re: OOP Image Handling

Posted: Tue Jul 15, 2008 8:14 am
by onion2k
You appear to have forgotten to put any words in your post.

EDIT: Ah, no, you're bumping into the 'too much code in the code tags' bug. PHP and it's silly regexp limits. I suggest you host that code "snippet" elsewhere and link to it instead.

Re: OOP Image Handling

Posted: Tue Jul 15, 2008 8:15 am
by inghamn
Rock on - didn't realize there were problems with that.

Here's the link to the code hosted on Sourceforge.
http://photodatabase.svn.sourceforge.ne ... iew=markup

Re: OOP Image Handling

Posted: Tue Jul 15, 2008 8:38 am
by onion2k
While you're right that ImageMagick is better able to handle larger images, GD is fine for most things people will be uploading to a normal gallery. It's only a problem if you're writing "media centre" code to handle print resolution images really. It's also worth noting that Imagemagick requires exec() if you don't have access to recompile PHP ... and on 99% of shared hosts that is blocked by safemode. GD is a more universally available library.

Re: OOP Image Handling

Posted: Tue Jul 15, 2008 8:49 am
by inghamn
Onion2k, you are entirely correct. The choice of GD vs ImageMagick depends alot on the host. It also depends on the volume of images you're dealing with.

In my case, this project is for my family's photo album. We've got around 20,000+ high resolution photos in there. At that volume, you don't want to sit and upload photo one at a time. We upload a whole batch of photos that get processed and added as a roll.

I recently had to change my photodatabase system because I was using too much CPU on the shared hosting system I'm on. I liked the optimization so much I ported the image handling stuff to our content manager system.

Re: OOP Image Handling

Posted: Tue Jul 15, 2008 9:44 am
by allspiritseve
So basically, if I have the choice-- I should go with ImageMagick?

I'll have to check out the libraries mentioned-- I may just go with a simple wrapper until I need more capability though.

Re: OOP Image Handling

Posted: Tue Jul 15, 2008 10:15 am
by onion2k
allspiritseve wrote:So basically, if I have the choice-- I should go with ImageMagick?
Not quite:

If you need to handle large batches of (hundreds of) images or very large images, and you don't need 'per pixel' effects, then use Imagemagick. Check you can use it first though (that it's on your server and you have access to it via exec()).

If you're handling a few images at a time, they're anything up to normal digital camera resolutions (5MP sort of size), or you need 'per pixel' level interaction, use GD. Check it's installed first though (with gdinfo()).

There's another image extension called NetPBM too but I've never played around with it to know what it's like.