Resizing images at runtime?

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

josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Resizing images at runtime?

Post by josh »

What do you mean, any size can be requested and the code blindly complies? wouldn't you still have to tweak the settings if requirements changed? I'd definitely do it at runtime for anything under 10k images tho.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Resizing images at runtime?

Post by allspiritseve »

josh wrote:What do you mean, any size can be requested and the code blindly complies? wouldn't you still have to tweak the settings if requirements changed? I'd definitely do it at runtime for anything under 10k images tho.
Requested by the template, not the user. The user would just upload their image, and the template would request the image size it needed. Whatever class it requests from would handle determining if an image of that size existed, and if not, make one. That means I have more flexibility in the templates because I can request an image of any given size, rather than picking from a pre-determined set of widths.

An alternative option would be to allow the user to select what sizes they want when they upload the image. I don't know which one is better for my purposes though.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Resizing images at runtime?

Post by John Cartwright »

Why not implement the caching mechanism with dynamic image resizing? If that particular image has already been generated for those exact dimensions, use the cache version, otherwise generate it on request. Best of both worlds.

If this were the case, it would also be trivial to delete generated images that have not been requested in x days.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Resizing images at runtime?

Post by allspiritseve »

John Cartwright wrote:Why not implement the caching mechanism with dynamic image resizing? If that particular image has already been generated for those exact dimensions, use the cache version, otherwise generate it on request. Best of both worlds.

If this were the case, it would also be trivial to delete generated images that have not been requested in x days.
That's the plan :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Resizing images at runtime?

Post by josh »

Well I assumed any scheme would have caching, but with a larger image base you still have the possibility for performance delays. I'm more interested in how you handle it in the template though, so the class that generates the URL builds the image before the http request is fired off? By user generated I mean if the user wanted to could he copy the image URL and hack the paramaters?
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Resizing images at runtime?

Post by allspiritseve »

I was thinking more like this:

Code: Select all

<img src="<?php echo $image->setWidth ('250px')->getFilename(); ?>" width="250px" height="<?php echo $image->getHeight(); ?>" alt="<?php echo $image->getAltText(); ?>" />
Where $image is an instance of a class that checks to see whether images/image_250.jpg exists. If so, it will output that filename. If not, it will create a new file. It's done through PHP, not the URL, so there's no way anybody could hijack it.
User avatar
inghamn
Forum Contributor
Posts: 174
Joined: Mon Apr 16, 2007 10:33 am
Location: Bloomington, IN, USA

Re: Resizing images at runtime?

Post by inghamn »

What has been working very well for my photo system is to resize the images at display time, but you absolutely must cache them. In a photo system where someone uploads 100 photos at a time, you do not want to make them wait as you try to create all the possible sizes. Waiting until the images are actually displayed spreads the pain out over a longer period of time.

We also support infinite number of sizes that are cached. Since our photos are stored on the filesystem (meta-data in the database, of course), we can create sub-directories for each of the various sizes. These directories can be created on the fly by the resizing, as needed

Code: Select all

 
/media
    /images
        /thumbnails
        /previews
        /80x80
 
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Resizing images at runtime?

Post by josh »

allspiritseve wrote:I was thinking more like this:

Code: Select all

<img src="<?php echo $image->setWidth ('250px')->getFilename(); ?>" width="250px" height="<?php echo $image->getHeight(); ?>" alt="<?php echo $image->getAltText(); ?>" />
Where $image is an instance of a class that checks to see whether images/image_250.jpg exists. If so, it will output that filename. If not, it will create a new file. It's done through PHP, not the URL, so there's no way anybody could hijack it.
Ah so the URL contains an id to a row in the database or a salted hash to id the file?
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Resizing images at runtime?

Post by allspiritseve »

josh wrote:Ah so the URL contains an id to a row in the database or a salted hash to id the file?
No, the url would point to the actual image. In the example I posted, say the original image is 800px wide, located at "/images/originals/123.jpg". Since I asked for a 250px-wide image, it would generate an image at "images/250/123.jpg" and output that as the filename instead of the original.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Resizing images at runtime?

Post by josh »

Gotchya, I like your idea, I'm already thinking of a hybrid approach for my smaller sites. Normally I proxy images thru a script so I can layer ACL and stats ontop of it
Post Reply