Image Resizing - SitePoint Script

I’m working on the backend for an application and need to resize images via PHP. A quick Google search turned up this article: http://articles.sitepoint.com/article/image-resizing-php

As far as I can tell though, the only thing this does is figure out the correct aspect ration for the resized image and output that as HTML. This creates two problems:

  1. The images being displayed haven’t been reduced in size so a list of “thumbnails” would take forever and a day to download. (This limitation was pointed out in the article as well.)

  2. The image being displayed is going to be resized by the browser which generally results in jagged edges and a crap-tacular job.

Am I missing something here or does this script have some magic that I just don’t see?

In most cases, I would use ImageMagik so that I could have a regular version and a thumbnail version of each image.

http://php.net/manual/en/book.imagick.php
This will let you actually create a thumbnail size. I used it and it works well. I coded my script quite sometime ago, so I can’t easily help with the details. But, they are there. When a new picture is uploaded to my site the thumbnail is created automatically.
Hope this helps.

I’ve got a script to do the resizing. Thanks for the resource though. I guess I was just commenting on this article. I see the usefulness of the PHP function within a script that creates a resized image but just don’t see how it’s useful without saving a resized image.

It would be useful if like the author, you didn’t want

4,000 different thumbnails sitting around taking up space.

(I guess 4,000 full-size full-weight images are OK?)

Other than that, IMHO it falls into the category of “stupid PHP tricks”. Interesting exercize for a PHP newbie perhaps but not really very applicable in general.

Article is awful and outdated

it was written in 2003…
Maybe it should now be removed as outdated and not great practice as there are NOW better ways to do it.

That being said, here’s a really cool script which actually does resize the image on the fly. http://shiftingpixel.com/2008/03/03/smart-image-resizer/

Good call Jeff, I noticed it’s now hosted on Google Code too.

It is not such cool, but reliable.
But it will be first in the queue to optimization once you decide to profile your site because of slowness. Too much useless memory and CPU consumption.

Is there an easier way to use this script though?

<img src="/image.php/coffee-bean.jpg?width=200&amp;height=200&amp;image=/wp-content/uploads/2008/03/coffee-bean.jpg" alt="Coffee Bean" />

What’s the point of including the /coffee-bean.jpg after the image.php file?

… see this is what happens when I dabble in PHP (:

In the past, I’ve utilised mod_rewrite invoke the thumbnail creation process. Given /images/thumbnails/coffee-bean.jpg, if this was to result in a 404, the script would be invoked to create the thumbnail and display it.

Lazy loading of sorts.

However, it adds a dependency on mod_rewrite.

Merely aesthetics I would say without reading the source. :frowning:

So you would code the 404 to look for an image extension in the requested path? I don’t understand how you would distinguish between a 404 from the image path and a 404 from a truly non-existent page. Additionally, how were you passing where the image was actually located so the thumbnail could be generated?

If the script is working correctly, the images are cached so as not to suck up all the memory and CPU. Not using any more resources than resizing them on upload.

I’ll see if I can dig out the .htaccess Jeff, but in a nutshell, the logic was…

If matches thumbnail url and is not a file, redirect to php passing the last url segment as the image to process.

This way valid thumbnails were served as normal to avoid disk I/O and php overhead, and non-exisitant ones created when initially requested.

If you can’t find the .htaccess don’t worry. I think I understand where you’re coming from. That seems a bit overly complicated for this application but I like the idea. I’ll have to keep it in mind for future projects.

But it reading whole cached image into memory, instead of raw output it using readfile().
No big deal for the thumbnail though.

I have no idea what that means so I’ll trust you. :confused:

You see, that’s what you get, coming all up in here with your Member Of The Month badge demanding answers. :lol:

As short-n-sharp as he is, Shrapnel_N5 does offer good advice more-often-than-not.

rubs Shrapnel_N5’s head condescendingly
:stuck_out_tongue:

Both of you gurus are amazing. Thanks! :smiley: