Editing Images – Resizing, Cropping, and more!
Published: January 29th, 2009 by: Kurtis
We've seen the graphics PHP can create for you, but how extensive is PHP's ability to edit preexisiting images? In this article, I will explore some common functions that image editing software provides, but I'll only use PHP. We'll focus mainly on cropping, resizing, flipping, and adding some effects to photos.
First of all, let’s talk about cropping and resizing images as the two use the same function, just in a slightly different way. The function is imagecopyresampled().
<?php imagecopyresampled($new,$orig,$newX,$newY,$oldX,$oldY,$newWidth,$newHeight,$oldWidth,$oldHeight); ?>
It’s quite a doozy, but all of the parameters are self-explanatory. The variables ‘newX’ and ‘oldX’ refer to the new starting x-coordinates and old coordinate, respectively, and so on for the y-coordinates as well.
To crop an image, the idea is that we select a rectangular section of the original image and copy it to a new image. Realize that in this case the ‘oldWidth’ and ‘oldHeight’ variables refer to the selection cropped, not the entire original image. The process looks like this:
<?php //Cropping an image header("Content-type: image/jpeg"); $top = ...; //Define upper-most pixel of selection $bottom = ...; //Define lower-most pixel of selection $left = ...; //Define left-most pixel of selection $right = ...; //Define right-most pixel of selection $image = imagecreatefromjpeg("/path/to/image.jpg"); //Function matches file type $new = imagecreatetruecolor(($right-$left),($bottom-$top)); //Becomes width,height imagecopyresampled($new,$image,0,0,$left,$top,($right-$left),($bottom-$top),($right-$left),($bottom-$top)); imagejpeg($new,null,100); imagedestroy($new); imagedestroy($image); ?>
Next is the very similar resizing function. The only difference with resizing is that you don’t need to pick a certain area of the image; the entire image will be copied, simply at a different width and height.
<?php //Resizing an image header("Content-type: image/jpeg"); $newWidth = ...; //Define new image's width $newHeight = ...; //Define new image's height $image = imagecreatefromjpeg("/path/to/image.jpg"); //Function matches file type list($width,$height) = getimagesize("/path/to/image.jpg"); //Get old width and height $new = imagecreatetruecolor($newWidth,$newHeight); imagecopyresampled($new,$image,0,0,0,0,$newWidth,$newHeight,$width,$height); imagejpeg($new,null,100); imagedestroy($new); imagedestroy($image); ?>
You can see that these two ideas are very closely related, so there isn’t much difference between the two functions. Next, let’s talk about flipping images, both horizontally and vertically.
There are two new functions we’ll talk about here, one that gets the color of the image at a specified pixel, and the other sets the color of the image at a specified pixel.
<?php $color = imagecolorat($image,$x,$y); //Get color imagesetpixel($image,$x,$y,$color); //Set color ?>
The only other thing we need to talk about is the idea of rolling through each pixel, and we’ll do this using two simultaneous ‘for’ statements, one that runs through the image pixel-by-pixel horizontally, and another one that runs through each vertically at the same time. This will allow us to get and set the color of each pixel in the image.
To flip an image horizontally, the left-most pixel becomes the right-most and vice-versa. The top-most and bottom-most pixels switch when we flip vertically. The process looks like this:
<?php $type = ...; //Define flip type (horizontal or vertical) $image = imagecreatefromjpeg("/path/to/image.jpg"); //Function matches file type list($width,$height) = getimagesize($imagepath); //Get width and height $new = imagecreatetruecolor($width,$height); //Becomes new width and height for ($x=0; $x<$width; $x++) { for ($y=0; $y<$height; $y++) { $color = imagecolorat($image,$x,$y); //Get pixel color if ($type == "horizontal") { imagesetpixel($new,(($width-1)-$x),$y,$color); } else { //Type is vertical imagesetpixel($new,$x,(($height-1)-$y),$color); } } } imagejpeg($new,null,100); imagedestroy($new); imagedestroy($image); ?>
Last, but not least, I’ll show you how to apply several different types of effects to an image, and this doesn’t require any special calculations, just one function, imagefilter(). You can read about all of the different filters available, but the main ones are blurring, converting to gray scale, and changing contrast/brightness.
With these GD functions in tow, there is very little you can’t do with PHP. Alright, so there’s still quite a bit that PHP can’t do with images practically, but it’s only a matter of time before programmers are able to apply even the most complex Photoshop functions to PHP.