PHP Image Resize Made Easy

Dec 21, 2008 | Tags: PHP, Image | del.icio.us del.icio.us | digg Digg

Resizing image has always been a difficult task for us. But thanks to Mike Lopez, who has made this wonderful PHP Image Resize Script available for us. It's very simple and easy to understand. And with Mike's blessing, I added some features to the script:

  • Displays a default image if the requested image doesn't exist.
  • Hide the image path from user.

Below are some usage examples and the result images. I will use the image shown in Figure 1 as the source image.

Elvita Khairani, my favorite news anchor! (300x450 pixels).
Fig 1. Elvita Khairani, my favorite news anchor! (300x450 pixels).

Listing 1: Usage examples

<!-- 1. Resize an image to 50% the size -->
<img src="imgresize.php?percent=30&amp;img=elvita.jpg" />

<!-- 2. Resize an image to 150 pixels wide and autocompute the height -->
<img src="imgresize.php?w=150&amp;img=elvita.jpg" />

<!-- 3. Resize an image to 225 pixels tall and autocompute the width -->
<img src="imgresize.php?h=225&amp;img=elvita.jpg" />

<!-- 4. Resize to 150 pixels width OR 500 pixels tall,
        whichever resulting image is smaller -->
<img src="imgresize.php?w=150&amp;h=500&amp;constrain=1&amp;img=elvita.jpg" />

<!-- 5. Display a default image if the requested image doesn't exist -->
<img src="imgresize.php?w=150&amp;img=miss_elvita.jpg" />

And here are the results:

The result image for example 1, 2, 3 and 4 (150x225 pixels).
Fig 2. The result image for example 1, 2, 3 and 4 (150x225 pixels).
The result image for example 5.
Fig 3. The result image for example 5.

Related Articles

The Downloads

1 Comment

Best of jeiboy on Nov 25, 2010:

Thanks for the script.

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000