OOP Image Handling

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

OOP Image Handling

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: OOP Image Handling

Post by Eran »

This one is nice for simple resizing / resampling - http://codeutopia.net/blog/2008/07/08/a ... humbnails/
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: OOP Image Handling

Post 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...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: OOP Image Handling

Post by Ollie Saunders »

There must be others too. I've wanted this many times myself and always considered writing one.
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Re: OOP Image Handling

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: OOP Image Handling

Post 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 :) )
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: OOP Image Handling

Post by Ollie Saunders »

Impressed ole is impressed

Oh wait I'm not ole anymore, I'm almost sad :-(
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: OOP Image Handling

Post by onion2k »

Cheers ole (You'll always be ole to me). :oops:
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: OOP Image Handling

Post 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.
Last edited by inghamn on Tue Jul 15, 2008 8:15 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: OOP Image Handling

Post 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.
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: OOP Image Handling

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: OOP Image Handling

Post 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.
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: OOP Image Handling

Post 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.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: OOP Image Handling

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: OOP Image Handling

Post 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.
Post Reply