Page 2 of 2
Re: Resizing images at runtime?
Posted: Tue Feb 17, 2009 4:27 pm
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.
Re: Resizing images at runtime?
Posted: Tue Feb 17, 2009 10:14 pm
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.
Re: Resizing images at runtime?
Posted: Tue Feb 17, 2009 10:18 pm
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.
Re: Resizing images at runtime?
Posted: Tue Feb 17, 2009 10:21 pm
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

Re: Resizing images at runtime?
Posted: Wed Feb 18, 2009 1:55 am
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?
Re: Resizing images at runtime?
Posted: Wed Feb 18, 2009 2:01 am
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.
Re: Resizing images at runtime?
Posted: Wed Feb 18, 2009 8:08 am
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
Re: Resizing images at runtime?
Posted: Wed Feb 18, 2009 1:45 pm
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?
Re: Resizing images at runtime?
Posted: Wed Feb 18, 2009 1:51 pm
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.
Re: Resizing images at runtime?
Posted: Wed Feb 18, 2009 2:18 pm
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