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.
Listing 1: Usage examples
<!-- 1. Resize an image to 50% the size -->
<img src="imgresize.php?percent=30&img=elvita.jpg" />
<!-- 2. Resize an image to 150 pixels wide and autocompute the height -->
<img src="imgresize.php?w=150&img=elvita.jpg" />
<!-- 3. Resize an image to 225 pixels tall and autocompute the width -->
<img src="imgresize.php?h=225&img=elvita.jpg" />
<!-- 4. Resize to 150 pixels width OR 500 pixels tall,
whichever resulting image is smaller -->
<img src="imgresize.php?w=150&h=500&constrain=1&img=elvita.jpg" />
<!-- 5. Display a default image if the requested image doesn't exist -->
<img src="imgresize.php?w=150&img=miss_elvita.jpg" />
And here are the results:
Related Articles
The Downloads