A ridiculously easy-to-use PHP script for resizing images the smart way.

One of the challenges that comes with maintaining a graphic-intensive website like Shifting Pixel is finding a way to get high quality images throughout the site with as little effort as possible. To tackle this, I developed the Smart Image Resizer and have been using it around the site for the past few months. I couldn’t be happier.

The major advantage of this script is that it allows me to resize and crop any image on my website without touching the actual image or writing any code. I upload each image once at a high enough resolution and can then reuse it at any size I want, anywhere I want. It doesn’t matter if the images are in a post, on a page, or in a template file–it just works. All of the magic is done through the query string part of the URL in the src attribute of the img tag.

And, if this wasn’t a big enough time-saver for me right now, it’ll be a huge time-saver the next time I decide to redesign my site. It’s a relief that I won’t have to go through to resize and re-upload a thousand photos to make sure they look right–I’ll just have to change the links to them and I’ll be all set.

Features

  • Resizes JPEGs, GIFs, and PNGs
  • Intelligently sharpens for crisp photos at any size
  • Can crop based on width:height ratios
  • Can color-fill transparent GIFs and PNGs
  • Built-in caching keeps image variations for optimal performance

Requirements

PHP 5.1.0+ compiled with GD. (You need a version of GD that supports imageconvolution(), such as the bundled version. Note that on some distributions, the default php-gd package may not be adequate.)

Download

Download SLIR 2.0

Download Smart Image Resizer v1.4.1 (Released August 6, 2008)

Tip jar

[flattr /]

To Install

  1. Unzip on your web server
  2. In the same directory, create a directory called “imagecache”
  3. Make your imagecache directory is writable by the web server (usually chmod 775)

Troubleshooting

  • Turn on “display_errors = on” in your php.ini file. Restart Apache and verify with a phpinfo().
  • Try visiting the URL that you are using as the src attribute of your img tag directly in the browser. For example, for example, stick this in your browser’s address bar: http://example.com/image.php?width=180&height=180&image=/path/to/image.jpg. If there is an error message here, it should be quite helpful.
  • If that doesn’t give you any information, turn on error reporting (add error_reporting(E_ALL); to the top of image.php) and see if that gives you any information. It is possible that the script is trying to use a function that doesn’t exist in your installation and configuration of PHP.
  • If you can’t get the script to give you any information, try peaking into your error logs.
  • Finally, if you can’t get any information out of any of these things, run a phpinfo() and send me a link–if I have time I’ll take a look at it and tell you if anything looks out of the ordinary. As of this time, I am spending more time focusing on SLIR 2.

Common error messages

Fatal error: Call to undefined function imagecreatetruecolor()
This most likely means that you do not have GD installed. To fix this, install GD (make sure you are using a version that includes the imageconvolution() function).
Fatal error: Call to undefined function imageconvolution()
This means that you are using a version of GD that does not include the imageconvolution() function. To fix this, you will either need to find a repository that includes the version of GD that you need, or compile php-gd from source.

License

I love to hear when my work is being used, so if you decide to use it, feel encouraged to send me an email. Smart Image Resizer is released under a Creative Commons Attribution-Share Alike 3.0 United States license. All I ask is that you include a link back to Shifting Pixel (either this page or shiftingpixel.com), but don’t worry about including a big link on each page if you don’t want to–one will do just nicely. Feel free to contact me to discuss any specifics.

Examples

These examples use my photo of this coffee bean. For more fine photography, browse my “Must See” photos or subscribe.

Resizing a JPEG

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

Coffee Bean Coffee Bean Coffee Bean

Resizing and cropping a JPEG into a square

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

Coffee Bean Coffee Bean Coffee Bean


371 Comments

 
1 4 5 6 7 8
  1. i am new to php and i want to use that smart image resize script but i do not know how it can be used on website?

    plz guide me,

    i will thankful to u.

    waiting for quick response.

  2. Have anybody checked this in the php version-PHP Version 5.2.9-0.dotdeb.1
    GD library- bundled (2.0.34 compatible)
    It work great in my old server but above mentioned is my current server details.
    This is not working here.No image is displayed .I can able to get the alt text only.
    No permission problem.
    What else cause this to stop working.

  3. Ravi Patidar said

    hello,
    Is there any way to use this image display by Imagemagick library? I have been used this script and it is working fine but I want to use Imagemagick library for image resizing and display them, Even I done at the level of resizing the images and also sent them the imagecache folder but unable to display those resized images.
    Please help me if you have any solution or suggestions.
    Thanks

  4.  
  5. [...] 19. «Умное» изменение размера изображения [...]

  6. Hey, i need HELP! I’m using appServ. It’s root is “www” directory. I have a folder named “avp” in “www”. Your php file is in this directory. However, i can’t manage to show images!!! There is not enough explanation or example, please help… Thing i tried is below:

    when i make it “src=’./im….” it shows the picture. However, when i put it to web, it doesn’t work! (On the server, the file structure is the same! Everything is in “avp” folder)

  7. Great script! Exactly what I was looking for!

    This is going to be used in a photography portfolio and I’m also looking to add a small watermark to the bottom right corner of the resized image. I hope I can figure it out on my own as I’m not php-savvy, but in any case, it’s an idea for future versions :)

  8. @Jon
    If you found out how to add watermarking, please let me know! I’ve been trying to do that for a loooooong time.

  9. Me too waiting for the water marking …

  10. Joe -

    I came across your image resizer when looking for some info on TimThumb. Looks way better!

    Have you given any thought to doing a simple TimThumb emulation mode so I can drop it into my existing WordPress Theme? I am not the author of the theme and would have to hack code all over the place to swap it out, where a quick command line emulator of TimThumb would allow it to drop in to many WP blogs without much work at all.

    Couple of adds to the feature list :)

    Pass through mode, do nothing but use the passed in image, just use the same url and skip all processing (cache check, etc).
    Automatic Cache Aging some how ;)

    Anyway code and documentation look very good can’t wait for a v2 final release

  11. [...] Go here to read the rest: Smart Image Resizer (PHP): a simple way to create thumbnails and more … [...]

  12. Hello,

    I am definitely loving using this tool– it has been a life saver! I have just one simple question, that I’m hoping someone can answer.

    After I have resized the image– how would I be able use the getimagesize() function to get the current height/width of the image? Would I have to get it from the cached folder somehow?

    Many thanks!

  13. Nevermind, it was easier than I thought it’d be. Just place the path in the getimagesize() function.


    list($width, $height) = getimagesize("http://www.yoursite.com/image.php/file-name.jpg?width=250&height=250&image=filename.jpg");

  14. [...] Fica aí a dica: Smart Image Resizer PHP: a simple way to create thumbnails and more on the fly — Shifting Pixel. [...]

  15. First off let me say thanks for this great tool! I have a question though – in my php error log I keep intermittently finding:

    PHP Fatal error: Cannot redeclare findsharp() (previously declared in E:\sites\hydrofarm.com\image.php:369) in E:\sites\hydrofarm.com\image.php on line 379

    I’m using an unmodified version 1.4.1 image.php and looking at the code I can’t fathom why I would get that error. Any ideas?

  16. you say “intermittently”. i see E:\sites\… is the script run locally?
    are you using a cloud? it may be leftover memory

  17. Not using a cloud… we host the site locally on a dedicated (IIS) server and E:\sites\… is the physical path to the site. As far as defining “intermittently” I’m getting maybe 10 of these errors a day. The script runs thousands of times a day and while it doesn’t seem to be causing a problem, I just like keeping my error log tidy. :-)

  18. [...] Smart Image Resizer [...]

  19. Very very nice script.

    All other scripts I found do resize the image but make it very ugly :)

    Thanks again

  20. hi, this is pretty nice.. looks well written. would be nice to have as a class or function though.. so can use it in other contexts aside from an tag.

  21. I’ve been using this script on my server for a few projects and it works fine. But today I tried using it on a client’s server, and when I try to resize a JPG, the script returns nothing (or a blank/empty image – I’m not 100% sure). Their phpinfo() reports GD version “2.0 or higher”. PNGs work fine. And the files definitely exist. Any idea what could be wrong? I’ve also noticed that when trying to access a JPG nothing is added to the imagecache folder (when you access a PNG there IS something added).

    Thanks!

  22. @steven: the first time I used this script, I run it on an IIS web server. The script gave me nothing, no thumbnail displayed.
    And then I moved it to an apache web server. And it works okay.
    I wonder if your client’s server is an IIS web server also?

  23. Joe Lencioni said

    @steven, I’m guessing that while you are using a new enough version of GD, you are not using a version that has the imageconvolution() function in it. I am using imageconvolution() to sharpen the resized JPEGs, which would explain why it still works for you on PNGs. Unfortunately this function is only available if PHP is compiled with the bundled version of the GD library.

    Some people have added a function to get around this like the one here. Be warned, however, that this function is much slower than the one available in GD and I cannot speak toward the quality of its output.

    I hope that helps.

  24. You were right – I set $doSharpen to false and it works. Thanks!

  25. [...] of open source technologies, including: WordPress, JQuery, Google Maps API, TinyMCE, Gravatar and Smart image resizer. I also created a mobile version of the [...]

  26. I have a suspicious error that I am sick of looking at because I see no difference. Basically, I have the script in my page twice – once if there is a thumb defined for that article and one that finds the latest picture linked to that article to use as a thumb. The code for the image is the same however, its just my messy way of using ifs to change the variables within that that is different.

    When the code is kicked into gear via the latter example the source outputs:
    image.php/filename.jpg?width=/alt="Alt Text"

    The filename and alt text are correct from the database so the php making the variables is okay – therefore I dont understand that when it is bringing in the variables where thumb=1 rather than the most recent date, it works perfectly with the source reading (as it should):
    ...width=30&height=30&cropratio...

    What would cause it to ignore all the middle, put in a slash and skip to alt? The same image works in an “all images” gallery so the database data itself that I insert to do the “image=” must be okay as that particular one works elsewhere.

    Thanks if you can try and help, I am bored of looking at it now!

  27. Awesome script. Thanks for posting, re-posting and helping out the rest of us!

  28. I’m attempting to use SLIR v 2.0b2 and my server is giving me the following error:

    “.htaccess rewrite engine not allowed here”.

    Have any of you seen this error or have an idea what I might be doing wrong?

  29. Hi!

    I want to thank you so very much for this. I’ve been looking for something that will resize images without screwing up the look. I had found a “decent” one, but left the images very choppy. On top of that, it made many copies of the images. Thumbnail, crop, and the original. This one beats all that by a long shot!

    Thank you!

  30. Hi,

    Is it not possible to use images that reside on a server different from your own ? I’m trying to use the SIR script on youtube thumbnails, such as http://img.youtube.com/vi/5gUtSDQAwYw/0.jpg, but I’m getting an error saying that the image does not exist, and the path that shows up in the error has my domain in it instead of the domain of the image that I specified.

    Thanks in advance.

  31. very very cool one, thank you!!

    one very little thing i am missing: rounded corner :)
    have a look right here: http://ru2.php.net/manual/de/function.imagecopyresampled.php#80417

    and may be to have the url as a link for the script!

  32. Thanks for this excellent script! It makes image resizing sooo easy.
    An integration for TinyMCE would be great.

  33. Thank you very much for sharing! I agree integration into tinyMce would be perfect.

  34. [...] And lastly, I was using this solution to manage thumbnail images from the always helpful folks at nettuts.  But at point, I broke it.  Plus, I wasn’t complete satisfied with the quality of the thumbnails.  So now I’m using the ridiculously awesome Smart Image Resizer by Joe Lencioni. [...]

  35. I’m testing the SLIR beta version (SLIR v 2.0b2), and it’s working great except that the first time I request an image configuration, it returns blank, with no errors. If I reload the page, the image appears.

    Do you think this is a problem with SLIR?

  36. I’ve been trying in vain to get v1.4.1 to work and I keep getting either 404 errors (direct path to image works fine) or if I fully scope the src then I get a 403 Forbidden error. Not sure what’s up there and I was about to give up on it all.

    I decided to try out v2.0b1 and in my tests everything is working like a charm!

    Thanks a lot for this awesome script, now I just have to figure out how to work v2 so that I can add watermarks ^__^

  37. [...] thumbs of every upload and then try to store them in the DB somewhere. So I found this script (the Smart Image Resizer by Joe Lencioni) that allows you to set some parameters in your img src tag in order to resize and crop (and [...]

  38. I like this script.

    One question…does it take significantly longer or is it much more intensive on the script to size, say, an 800px x 800px image down to 200×200, vs a 600×600.

    How much does it matter to the script how large the original image is in sizing it down?

  39. Joe Lencioni said

    @chris

    “.htaccess rewrite engine not allowed here”.
    Have any of you seen this error or have an idea what I might be doing wrong?

    It sounds like your web server has been set up to restrict the use of mod_rewrite in .htaccess files in that directory. This is most likely due to optimizing for speed. If you want SLIR 2.0 to work well you will probably have to mess around with your web server’s configuration files.

    @brian

    Is it not possible to use images that reside on a server different from your own ?

    The script is written to strip out the protocol and domain name portion of the image request for security reasons. With a little re-coding, you could make it so it will work with remote images, but I really don’t recommend it. You’ll most likely be opening yourself up to a whole world of trouble.

    You would be much better off writing a script that fetches the images from the remote server without requiring the URL of that remote image as a parameter (it should be all figured out internally).

    @Lukas

    one very little thing i am missing: rounded corner

    Cool, thanks for the link.

    @Simon7

    An integration for TinyMCE would be great.

    Although I probably won’t be working on anything like that any time soon, someone else here might want to take up the challenge. What, specifically, would you like it to do?

    @chris

    I’m testing the SLIR beta version (SLIR v 2.0b2), and it’s working great except that the first time I request an image configuration, it returns blank, with no errors. If I reload the page, the image appears.

    I believe that is a problem with SLIR and I most likely have fixed it in the SVN. Try checking out the latest version from the repository and let me know if it starts working as expected. Please note that the changes that are in SVN will most likely require a lot more server resources when cropping, especially if the final image is large.

    @Charles

    One question…does it take significantly longer or is it much more intensive on the script to size, say, an 800px x 800px image down to 200×200, vs a 600×600.
    How much does it matter to the script how large the original image is in sizing it down?

    It will definitely take longer and it is more intensive on the script to size an 800×800 to a 200×200 vs a 600×600 to a 200×200 image. It will, however, only perform the resize once for each image and then it is cached so the additional impact should be minimal over time.

    As far as specifically how much more intensive it is, I am not sure. The most intensive parts of the script are resizing, sharpening, and cropping. Since the final image in your test case is 200×200, sharpening and cropping should be about the same in both situations, so resizing is the greatest variable. The script uses PHP’s imagecopyresampled() function if you care to investigate further. Or, you could try a PHP profiler such as Xdebug.

  40. Cory Burke said

    Hello, I’ve been using this script, but like others – have issue with the harshness of the sharpening matrix.

    Someone in this thread attempted to create a modification which included an additional filter called “slevel” to modify the sharpening amount.

    That modification appears to no longer be available. Do you have any suggestion and or comment regarding adjusting of the level of sharpening in your script?

    Thanks a lot! Your script is fantastic but is sharpening too much for my images presently!

  41. What is new in your script compared to phpThumb? Do you have any comparison chart? I’ve not tried it yet, but I guess it may not work with corrupted images. I have very bad experience with resizing corrupted image.

    You may have a look at Problem with resizing corrupted images using PHP image functions

  42. When i try this code. i m getting this error.. any body help me on this

    Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/jooryfas/public_html/image.php on line 249

    Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/jooryfas/public_html/image.php on line 308

    Fatal error: Call to undefined function: imageconvolution() in /home/jooryfas/public_html/image.php on line 324

  43. This is going to save me a ton of time on a project I am working on right now. Thank you!

  44. Joe Lencioni said

    @Cory Burke

    Do you have any suggestion and or comment regarding adjusting of the level of sharpening in your script?

    Although I am not sure of what you would need to specifically do to adjust the level of sharpening, you should probably look at lines 310–325 and 369–379 in v1.4.1

    @adnan

    What is new in your script compared to phpThumb? Do you have any comparison chart?

    I’m not sure, I’ve never really looked at phpThumb.

    I guess it may not work with corrupted images.

    I am betting you are correct.

    You may have a look at Problem with resizing corrupted images using PHP image functions

    After reading that article and poking around, I believe phpThumb can be configured to use ImageMagick, which may be a little more flexible and robust than GD.

    @Rifath

    Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/jooryfas/public_html/image.php on line 249

    Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/jooryfas/public_html/image.php on line 308

    It is hard to say what is going on without more information. However, it looks like you may be trying to resize an unsupported type of image. Perhaps it is corrupted or saved in a weird format.

    Fatal error: Call to undefined function: imageconvolution() in /home/jooryfas/public_html/image.php on line 324

    This means that you don’t have the imageconvolution() function in your copy of PHP. According to the PHP documentation, imageconvolution() “is only available if PHP is compiled with the bundled version of the GD library.”

  45. [...] image cache so that it wouldn’t be ridiculously slow every time it was run. It’s called Smart Image Resizer from Shifting Pixel. I was excited and decided to test [...]

  46. [...] this, I usually use a script such as Smart Image Resizer working together with the following code in my funtions.php [...]

  47. Here an improoved version
    http://luciorota.altervista.org/xoops/modules/wfdownloads/singlefile.php?cid=2&lid=13

    now is possible to apply filters to images (experimental)

  48. The script seems to be working great except for one thing … when I try to resize to a 3000x 2000 size. I want to provide an option to resize the file into 4 different sizes:
    3000 x 2000
    800 x 600
    480 x 340
    540 x 360

    The code is the same for each (except for the width/height and cropration) yet the 3000 x 2000 doesn’t output (though it appears to think awhile.

    Any ideas?

    Here’s the specific code line:
    “;

  49. [...] I know you want it. Here is where you get it: Shifting Pixel Smart Image Resizer. [...]

  50. @chris

    I’m testing the SLIR beta version (SLIR v 2.0b2), and it’s working great except that the first time I request an image configuration, it returns blank, with no errors. If I reload the page, the image appears.

    I believe that is a problem with SLIR and I most likely have fixed it in the SVN. Try checking out the latest version from the repository and let me know if it starts working as expected. Please note that the changes that are in SVN will most likely require a lot more server resources when cropping, especially if the final image is large.

    @Joe

    It’s working great now with the new version, thanks!

  51. [...] uses Joe Lencioni’s Smart Image Resizer script for resizing the images used in the featured posts showcase and in archive listings. Smart [...]

 
1 4 5 6 7 8