railroadman
10/21/2012 - 10:38 AM

PHP image resize

PHP image resize

$image_w = imagesx($image);               
$image_h = imagesy($image);               

//figure out if we are going to resize by the width or the height
//if the original image is wider than the new dimension, resize by the width
// or else resize by the height
if (($image_w / $image_h) >= ($new_w / $new_h)) {
  $h = $image_h * $new_w / $image_w;
  $w = $new_w;
} else {
  $h = $new_h;
  $w = $image_w * $new_h / $image_h;
}

//create a blank image with the new calculated dimensions
$tmp = imagecreatetruecolor($w, $h);

//copy the original image to the new image while resizing
imagecopyresampled($tmp,$image,0,0,0,0,$w,$h,$image_w,$image_h);