I’ve recently been toying with a 365 photo gallery using PHP with Flickr for the backend. Today I came across a code solution for image resizing that as of yet I have not added to my code snippet library, yet I often end up rewriting to fit a specific need. Images can be resized in PHP using either the GD library or Imagemagik. Since I do not have access to Imagemagik on my server and the GD library is very common these days, that is your main requirement for using this code. Before writing this I googled several other solutions and could find nothing that met my exact requirements, all of which have been incorporated into the function:
- Resizing of an image to fit within a box of x and y values, but keeping it’s proportions.
- Cropping of an image to fit a certain size.
- The ability to save the result to a folder and/or display the image.
Also, I would liked to have added functionality to enable the source file to be a url rather than a local file, but I was having problems with the settings on my shared server and it was more important that the function works on the wide and varied servers that my clients have. Besides it isn’t hard to add a wget or curl command prior to calling the function if available.
Anyway here is the code for the function:
function imageResize($src_file, $dst_file, $size_x, $size_y, $crop = false, $display = true) {
if(!file_exists($src_file)) {
echo "Error resizing image - Source file does not exist!";
die();
}
$ext = end(explode('.', strtolower($src_file)));
switch($ext) {
case 'gif':
$src_img = imagecreatefromgif($src_file);
break;
case 'jpeg':
$src_img = imagecreatefromjpeg($src_file);
break;
case 'jpg':
$src_img = imagecreatefromjpeg($src_file);
break;
case 'bmp':
$src_img = imagecreatefromwbmp($src_file);
break;
case 'png':
$src_img = imagecreatefrompng($src_file);
break;
default:
echo "Error resizing image - This does not appear to be an image file, check extension!";
die();
}
list($orig_x,$orig_y) = getimagesize($src_file);
if($crop) {
$new_y = round( ( $size_x / $orig_x ) * $orig_y );
if($new_y > $size_y) {
$new_x = $size_x;
} else {
$new_y = $size_y;
$new_x = round( ( $size_y / $orig_y ) * $orig_x );
}
$crop_x = round(($new_x / 2) - ($size_x / 2));
$crop_y = round(($new_y / 2) - ($size_y / 2));
} else {
$new_y = round( ( $size_x / $orig_x ) * $orig_y );
if($new_y <= $size_y) {
$new_x = $size_x;
} else {
$new_y = $size_y;
$new_x = round( ( $size_y / $orig_y ) * $orig_x );
}
$crop_x = 0;
$crop_y = 0;
}
$dst_img = imagecreatetruecolor($new_x - ($crop_x * 2), $new_y - ($crop_y * 2));
imagecopyresampled($dst_img, $src_img, 0, 0, $crop_x, $crop_y, $new_x, $new_y, $orig_x, $orig_y);
if(strtolower(stristr($dst_file,".jpg")) == ".jpg") {
header("content-type: image/jpg");
imagejpeg($dst_img,$dst_file,100);
if($display) imagejpeg($dst_img,null,100);
} else {
header("content-type: image/jpg");
if($display) imagejpeg($dst_img,null,100);
}
imagedestroy($src_img);
imagedestroy($dst_img);
}
And here are a few examples:
// Takes source file and resizes it to fit within 300x200 (uncropped), the result is not displayed but is saved locally to dest.jpg.
imageResize('test.jpg','dest.jpg',300,200,false,false);
// Takes source file and resizes it to fit within 300x200 (cropped), the result is displayed but is not saved locally.
imageResize('test.jpg','',300,200,true,true);
I'm thinking about wrapping this all up in a class. It makes more sense, but at the same time I can perfom all the actions I require in one line, so maybe I'll leave it as it is.