Image Resize function in PHP

No Comments

Just found on the net a good Image Resize function:

// Saves an image file resized to the dimensions specified. If new_filename is specified then the output will
// be saved to that destination, if not, it will overwrite the existing image. The image will be converted to
// jpeg if the format can be opened but not saved in its native format.

function image_save_resized($image_filename, $width, $height, $new_filename = false, $jpeg_quality = 100, $centre_image = false, $background_colour = array(255, 255, 255)) {

if(!is_file($image_filename)) {
trigger_error(“image_save_resized(): Failed to locate {$image_filename}; it does not exist!”, E_USER_WARNING);
return false;
}

// Prevents accidental misordering of the jpeg_quality parameter
if(!is_int($jpeg_quality)) {
trigger_error(“image_save_reized(): jpeg_quality should be an integer between 1 and 100″, E_USER_WARNING);
return false;
}

// Validate the background colour parameter
if(!is_array($background_colour) || count($background_colour) != 3) {
trigger_error(“image_save_resized(): background_colour should be array in the form array((int) r, (int) g, (int) b)”, E_USER_WARNING);
return false;
}

// Validate the size parameters
if(($width <= 0 || $height <= 0) || (!is_int($width) || !is_int($height))) {
trigger_error(“image_save_resized(): cannot resize to ” . intval($width) . ” x ” . intval($height) . “, invalid dimensions”);
return false;
}

// Get image attributes and work out the resizing.
list($img_width, $img_height, $type, $attr) = $image_info = getimagesize($image_filename);

switch($image_info['mime']) {
// Supported formates can be opened
case “image/jpeg”:
$source = ImageCreateFromJPEG($image_filename);
break;
case “image/gif”:
$source = imagecreatefromgif($image_filename);
break;
case “image/png”:
$source = imagecreatefrompng($image_filename);
break;
case “image/bmp”:
$source = imagecreatefromwbmp($image_filename);
break;

// Unsupported format
default:
trigger_error(“image_save_resized(): Image format not supported ({$image_info['mime']})!”);
return false;
}

$ratio = 1;

$new_width = $img_width;
$new_height = $img_height;

// Loops continuously trying different sizes until the perfect fit is found.
while($new_width > $width || $new_height > $height) {
if($new_width > $width) {
$ratio = $new_width / $width;
} else if($new_height > $height) {
$ratio = $img_height / $height;
}

if($ratio === 0) {
trigger_error(“image_save_resized(): Error recalculating image size.”);
return false;
}

$new_width = floor($img_width / $ratio);
$new_height = floor($img_height / $ratio);
}

// Create our target image
if($centre_image) {
$new_image = imagecreatetruecolor($width, $height);

if($width != $new_width || $height != $new_height) {
imagefilledrectangle($new_image, 0, 0, $width, $height, imagecolorallocate($new_image, $background_colour[0], $background_colour[1], $background_colour[2]));

$centre_x = round($width / 2);
$centre_y = round($height / 2);

$image_centre_x = round($new_width / 2);
$image_centre_y = round($new_height / 2);

$new_image_x = $centre_x – $image_centre_x;
$new_image_y = $centre_y – $image_centre_y;
} else {
$new_image_x = 0;
$new_image_y = 0;
}
} else {
$new_image = imagecreatetruecolor($new_width,$new_height);
$new_image_x = 0;
$new_image_y = 0;
}

// Resize the image
imagecopyresampled($new_image, $source, $new_image_x, $new_image_y, 0, 0, $new_width, $new_height, $img_width, $img_height);

// Determine the filename to save to
$output_filename = ($new_filename) ? $new_filename : $image_filename;

if(is_file($output_filename))
unlink($output_filename);

switch($image_info['mime']) {
// Supported formats can be saved
case “image/jpeg”:
$source = imagejpeg($new_image,$output_filename,$jpeg_quality);
break;
case “image/gif”:
$source = imagegif($new_image,$output_filename);
break;
case “image/png”:
$source = imagepng($new_image,$output_filename);
break;
case “image/bmp”:
$source = imagewbmp($new_image,$output_filename);
break;

// Unsupported format
default:
$source = imagejpeg($new_image,$output_filename,$jpeg_quality);
break;
}

// Clean up
imagedestroy($new_image);

return true;

}

Leave a Reply