Resizing Images On The Fly With PHP
Every once in a while it's useful to be able to dynamically resize an image, on the fly. Especially if you're running a web service like Gravatar, where users want their images in all sorts of different sizes. Some blogs have their gravatars set to 50x50px, while other have their gravatars set to 75x70px. It wouldn't make sense for Gravatar to store an image of every size on their servers. It makes sense to store one copy of the image, and then dynamically resize it. Generally speaking though, you should avoid this method since it puts a lot of load on the server (image manipulation happening all the time!). Anyways, here's a simple example of how it's done:
getImage.php
<?php
if(isset($_GET['url']))
{
$default_size = 50;
$width = (isset($_GET['width'])) ? $_GET['width'] : $default_size;
$height = (isset($_GET['height'])) ? $_GET['height'] : $default_size;
require_once('lib/Image.php');
$image = new Image();
$image->getImage($_GET['url']);
$image->resizeImage($width, $height);
$image->displayImage();
}
?>
Image.php (PHP Class)
<?php
/**
* Image
*
* @author Rob Keplin
* @link http://www.robkeplin.com
**/
class Image
{
public $errors;
private $_image;
public function __construct()
{
$this->errors = array();
$this->_image = NULL;
}
public function getImage($url)
{
$ext = strrchr($url, '.');
$ext = strtolower($ext);
switch($ext) {
case '.jpeg':
case '.jpg':
$this->_image = imagecreatefromjpeg($url);
break;
case '.gif':
$this->_image = imagecreatefromgif($url);
break;
case '.png':
$this->_image = imagecreatefrompng($url);
default:
$this->errors[] = "The linked file must be a .jpeg, .jpg, .gif, or .png";
break;
}
}
public function resizeImage($new_width, $new_height)
{
if($this->_image)
{
//Resize the image
$old_width = imagesx($this->_image);
$old_height = imagesy($this->_image);
//Keep the resolution of the image the same
if($old_width > $old_height)
{
$ratio = $old_height / $new_height;
}
else
{
$ratio = $old_width / $new_width;
}
$new_width = ceil($old_width / $ratio);
$new_height = ceil($old_height / $ratio);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $this->_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
$this->_image = $new_image;
}
}
public function displayImage()
{
if($this->_image)
{
header('Content-type: image/jpeg');
imagejpeg($this->_image, NULL, 100);
imagedestroy($this->_image);
}
}
}
?>
And BAM - you have full control of the image sizes, without having to use the ugly HTML width and height attributes which produce a pixilated mess.
Here's some live examples:
- height: 200px
- height: 175px
- height: 150px
- height: 50px
- height: 25px
If you'd like to download the PHP files and manipulate them however you want, go right ahead! Download them here.
Note: You must have php-gd installed in order to use the image functions defined in the Image class.



Rob Keplin » Resizing Images On The Fly With PHP | Coder Online - January 10th, 2010 @ 10:46am reply