Resizing images at runtime?
Moderator: General Moderators
Re: Resizing images at runtime?
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.
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Resizing images at runtime?
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.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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Resizing images at runtime?
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.
If this were the case, it would also be trivial to delete generated images that have not been requested in x days.
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Resizing images at runtime?
That's the planJohn 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.
Re: Resizing images at runtime?
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?
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Resizing images at runtime?
I was thinking more like this:
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.
Code: Select all
<img src="<?php echo $image->setWidth ('250px')->getFilename(); ?>" width="250px" height="<?php echo $image->getHeight(); ?>" alt="<?php echo $image->getAltText(); ?>" />- inghamn
- Forum Contributor
- Posts: 174
- Joined: Mon Apr 16, 2007 10:33 am
- Location: Bloomington, IN, USA
Re: Resizing images at runtime?
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
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?
Ah so the URL contains an id to a row in the database or a salted hash to id the file?allspiritseve wrote:I was thinking more like this:
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.Code: Select all
<img src="<?php echo $image->setWidth ('250px')->getFilename(); ?>" width="250px" height="<?php echo $image->getHeight(); ?>" alt="<?php echo $image->getAltText(); ?>" />
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Resizing images at runtime?
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 wrote: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?
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