Resizing images at runtime?
Moderator: General Moderators
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Resizing images at runtime?
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?
Yes it makes sense to generate dynamically, just cache the result so you don't suffer the performance hit next time.
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Resizing images at runtime?
Exactly. If it's not already resized, the image gets resized and saved so the next lookup can just load it.PCSpectra wrote:Yes it makes sense to generate dynamically, just cache the result so you don't suffer the performance hit next time.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: Resizing images at runtime?
There ya go. 
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.
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.
Re: Resizing images at runtime?
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
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Resizing images at runtime?
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.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
Re: Resizing images at runtime?
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.
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Resizing images at runtime?
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.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.
Re: Resizing images at runtime?
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.
Re: Resizing images at runtime?
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
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Resizing images at runtime?
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 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
Re: Resizing images at runtime?
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 )
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Resizing images at runtime?
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 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 )
Re: Resizing images at runtime?
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
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: Resizing images at runtime?
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.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