Crop an Image with PHP
I spent a few minutes working on this function that lets me crop the center of an image by width and height. Sometimes you want to create a photo gallery and use album covers for the image that links to the gallery page. Most of the time you want these images to be of a uniform size, i.e. thumbnails. This function (crop_image()) crops an image equally proportioned in the center depending on the size you give it.
Side note: If the spacing in the code is still FUBAR’d when you read this, sorry, I don’t know wtf’s up with my syntax highlighter, but I’m working on it.
Requirements for this to work properly (strictly recommended)
- PHP 5.2.x (at least, however I’m pretty sure it will work in PHP 4)
- GD 1.8
The function
/**
* @copyright crainbandy.com
* @since November 6th, 2008
* @author bryan maxwell - www.crainbandy.com
* --------------------------
* @name crop_image
* @package image editing
* @subpackage cropping
* --------------------------
* @link http://www.crainbandy.com/2008/11/06/crop-an-image-with-php/434
*
* This is a function that allows you to take any image $image and crop it to a specific size.
*
* @example crop_image('images/something.jpg', 'images/cropped.jpg', 125, 125);
*
* $_SERVER['DOCUMENT_ROOT'] is included on all image paths.
*
* @param string $src_image
* @param int $width
* @param int $height
* @param bool $cropped_image
* @param bool $link
* @return either true if it worked, or false if it didn't. or a string link to the image that has been cropped depending on $link's value.
*/
function crop_image($src_image, $width, $height, $cropped_image = false, $link = false) {
/**
* Setup some variables
*/
$dir = dirname($src_image);
$image_filename = $src_image;
/**
* Get the extension
*/
$len = strlen($image_filename);
$extension = substr($image_filename, $len - 4, $len);
/**
* Create the handles
*/
$src_file = $_SERVER['DOCUMENT_ROOT'] . $image_filename;
/**
* What kind of file is it?
* --
* Clean it up a bit, then instantiate the image.
*/
$clean_extension = str_replace('.', '', $extension);
switch ($extension) {
case '.jpg':
case '.gif':
case '.png':
/**
* Use a variable as a function name to call it, dynamically.
*/
$type = ($clean_extension == 'jpg') ? 'jpeg' : $clean_extension;
$dynamic_function = "imagecreatefrom$type";
break;
}
$src = $dynamic_function($_SERVER['DOCUMENT_ROOT'] . $image_filename);
$dest = imagecreatetruecolor($width, $height);
/**
* Are we renaming this? or just following suit with $src_image appending to it?
*/
if (!$cropped_image) {
$cropped_image = $_SERVER['DOCUMENT_ROOT'] . str_replace($extension, '_cropped' . $extension, $image_filename);
} else {
$cropped_image = $_SERVER['DOCUMENT_ROOT'] . "/$dir/$cropped_image";
}
/**
* Get size of the $src_file
*/
$size_array = getimagesize($src_file);
/**
* Set the size of the crop from the image.
*/
$crop_width = 125;
$crop_x = $crop_width / 2;
$crop_height = 125;
$crop_y = $crop_height / 2;
/**
* Set original sizes
*/
$original_width = $size_array[0];
$original_height = $size_array[1];
/**
* Logic
*/
if ($crop_width > $original_width || $crop_height > $original_height) {
die('Image dimensions are too big for the crop x-y you provided.');
}
/**
* Output the image
*/
imagecopy($dest, $src, 0, 0, $crop_x, $crop_y, $original_width, $original_height);
switch ($extension) {
case '.jpg':
case '.gif':
case '.png':
$dynamic_function = "image$type";
$result = $dynamic_function($dest, $cropped_image);
break;
}
/**
* Little bit of error control.
*/
if ($result) {
if ($link) {
return $cropped_image;
} else {
return true;
}
}
/**
* Trash.
*/
imagedestroy($dest);
}
How to use it
The function has five basic parameters. Three of which are required.
- src_image
This is the relative path, including file name of the image you want to crop. $_SERVER['DOCUMENT_ROOT'] is appended already, so you can leave that out. - width
This is the width of the crop.
- height
- This is the height of the crop.
Two that are optional
- cropped_image
This is the relative path to the output file including file name.
Note: If you leave this blank it will append cropped_ to the src_image. image.jpg becomes cropped_image.jpg.
- link
If yes, this will return the path to the cropped image.
Examples
This file is included with the download at the end of this article. You can reference these again in examples.php if you need.
/
**
* @copyright www.crainbandy.com
* @author bryan maxwell
*
* Crop image examples
*/
include('crop_image.function.php');
/**
* Crop an image to 125px by 125px.
*/
crop_image('uncropped_images/crop_me.jpg', 125, 152, 'cropped_images/i_got_cropped.jpg');
/**
* Crop an image 50x50 using the functions default naming convention
*/
crop_image('images/something.jpg', 50, 50);
/**
* Crop an image 25x25 using all properties.
*/
$cropped_image = crop_image('image.jpg', 25, 25, 'cropped.jpg', true);
echo "<img src="" alt="" />";
/**
* Crop an image 100x100 using the link function, but default naming.
*/
echo crop_image('image.jpg', 100, 100, false, true);
There you go, I hope you’re getting the hang of it.
Download
RAR Format (.rar) | Zip format (.zip)
Enjoi. If you have any questions please post in the comments. Spanks. Oh yeah, and DIGG IT.
Bryan

