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

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

Resizing images at runtime?

Post by allspiritseve »

Does it make sense to resize images at runtime instead of when they're uploaded? I'm thinking that image size is a display concept, and all I would need to do is set the size I need when I display it. The object can then either grab the image if it exists at that size, or resize the raw image to fit. Does that seem like an ok approach? Are there any pitfalls I might encounter doing it this way?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Resizing images at runtime?

Post by alex.barylski »

Yes it makes sense to generate dynamically, just cache the result so you don't suffer the performance hit next time.
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 »

PCSpectra wrote:Yes it makes sense to generate dynamically, just cache the result so you don't suffer the performance hit next time.
Exactly. If it's not already resized, the image gets resized and saved so the next lookup can just load it.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Resizing images at runtime?

Post by alex.barylski »

There ya go. :P

I don't think it's a matter of "does it make sense" so much as it is "is it worth the extra effort". In which I say, you only ever get back, exactly what you put in. :)
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 »

On marinas.com our images come in at 10MB + so we had to generate ahead of time. You can zoom in on the high resolution images though which does the resize in real time
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:On marinas.com our images come in at 10MB + so we had to generate ahead of time. You can zoom in on the high resolution images though which does the resize in real time
In that case I would probably resize a 1024x768 or so image, and use that image when resizing at runtime. Though I suppose it depends on where the image is going to be used... if you need the detail of that 10MB image, you probably aren't just uploading to a photo gallery or placing a side image in an article.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Resizing images at runtime?

Post by Benjamin »

I would think it best to resize them as they are uploaded. If there are lots of images to be resized, I wouldn't want to add that extra burden during periods of heavy traffic.
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 »

astions wrote:I would think it best to resize them as they are uploaded. If there are lots of images to be resized, I wouldn't want to add that extra burden during periods of heavy traffic.
I don't always know what size I'm going to need until later though. Sometimes the design changes, and I need a different version of all of the images I have already uploaded. If I resize them at runtime, then I can set the size I need in the template (which makes a lot of sense to me). That doesn't mean that each image needs to be resized for every page load though... once it's resized then that size gets saved and reused.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Resizing images at runtime?

Post by Benjamin »

So these are all variable sized images? There aren't a set of specific sizes? Then I guess you have to resize them once the first time they are loaded and just cache them.
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 »

Sizing from 1024 down to thumbnail still takes forever, If I needed to change the design in a pinch I could switch to "lazy" resizing, but if the image base + design don't need to change frequently aggressive loading wins performance wise in my book. Thing is if you have a page of 50 thumbnails or something even shaving .01 off your load time is noticeable to the end user
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:Sizing from 1024 down to thumbnail still takes forever, If I needed to change the design in a pinch I could switch to "lazy" resizing, but if the image base + design don't need to change frequently aggressive loading wins performance wise in my book. Thing is if you have a page of 50 thumbnails or something even shaving .01 off your load time is noticeable to the end user
Noticeable to one user only, though. Who very well could be the developer, designer, the client, etc. As opposed to me having to write a custom resize script to go through and resize all the images that have already been uploaded.
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 n images mean it effects <= n users, so 120k images on marinas.com would mean 120k different images, lets say we were under peak load and it was taking .4 seconds to resize... that'd be 13hrs of wasted CPU cycles ( if every image got hit ), not to mention you'd be flooding your disk queue, all to save 20 minutes of coding ( then the resizing takes place off site which takes 4+ 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 »

josh wrote:Well n images mean it effects <= n users, so 120k images on marinas.com would mean 120k different images, lets say we were under peak load and it was taking .4 seconds to resize... that'd be 13hrs of wasted CPU cycles ( if every image got hit ), not to mention you'd be flooding your disk queue, all to save 20 minutes of coding ( then the resizing takes place off site which takes 4+ days )
I'm not sure what you're trying to prove, obviously resizing at runtime wouldn't be the right fit for a site like that, but that doesn't mean it wouldn't work for other situations. Also, I can bet that you probably already know the size requirements up front for these images, since your site is very image-centric.
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 »

Not trying to prove anything just saying it depends on context, was just pointing out the contra-example, we're actually discussing changing the way we do it right now
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:Not trying to prove anything just saying it depends on context, was just pointing out the contra-example, we're actually discussing changing the way we do it right now
Gotcha. I don't even know if this is something I'd use for a photo gallery, as that would be better done batched. When user-uploaded images are inset into pages, articles, blogs, etc., though, we've traditionally had a couple of different sizes of each image made automatically, but that hasn't been working very well when requirements change.
Post Reply