CodersAdvocate

I work hard, so you don't have to

Resize Image On The Fly PHP

 

If you like me, you want to be able to resize images on the fly because... to be honest, lets face it we're all lazy.

Now with PHP 5, you can resize AND rotate images on the fly. The below code is a sample of what can be done and should serve as a basis to develop a feasable image resizing mechanism.

what i am going to use this for is on the fly image size manipulation similar to this.

<img src="http://www.codersadvocate.com/imagesize.php?imagesrc=
images/1235126180.jpg&percent=10&rotation=0" />

Comment if you require any help!


// Change the content type to jpeg
header('Content-type: image/jpeg');

// Pull the variables from the query string
$fileileSource = $_GET['imagesrc'];
$percent = $_GET['percent'] / 100;
$rotation = $_GET['rotation'];

// Sizes of the image are stored in an array
// index 0 is the width
// index 1 is the height
$size = getimagesize($fileileSource);
$width = $size[0];
$height = $size[1];
$requiredWidth = $width * $percent;
$requiredHeight = $height * $percent;

// pull the image file in to a variable
// for manipulation
$thumbnailCanvas = imagecreatetruecolor($requiredWidth, $requiredHeight
);
$originalCanvas = imagecreatefromjpeg($fileileSource);

// Duplicate and resize (under the bonnet stuff)
imagecopyresized($thumbnailCanvas, $originalCanvas, 0, 0, 0, 0, $requiredWidth, $requiredHeight, $width, $height);

// Rotate the image by the desired degrees and set the created space to white
$final = imagerotate($thumbnailCanvas, $rotation, 16777215);

// Print the image
imagejpeg($final);
?>
1236942643.gif no match in extensions for --
Copyright 2011 CodersAdvocate ©