Skip to content

Instantly share code, notes, and snippets.

@yetti
Created April 20, 2010 19:49
Show Gist options
  • Save yetti/372967 to your computer and use it in GitHub Desktop.
Save yetti/372967 to your computer and use it in GitHub Desktop.
//echo $imagefullpath;
$img = ImageCreateFromJPEG($imagefullpath);
$canvas = imagecreatetruecolor($thumb_w,$thumb_h);
// following image calculation based on
// http://return-true.com/2009/02/making-cropping-thumbnails-square-using-php-gd/
$orig_w = imagesx($img);
$orig_h = imagesy($img);
$w_ratio = ($thumb_w / $orig_w);
$h_ratio = ($thumb_h / $orig_h);
if ($orig_w > $orig_h) { // horizontal image
$crop_w = $orig_w * $h_ratio;
$crop_h = $thumb_h;
} else if ($orig_w < $orig_h) { // vertical image
$crop_w = $thumb_w;
$crop_h = $orig_h * $w_ratio;
} else { // already square
$crop_w = $thumb_w;
$crop_h = $thumb_h;
}
if (imagecopyresampled($canvas,$img,0,0,0,0,$crop_w,$crop_h,$orig_w,$orig_h)) {
imagejpeg($canvas,$thumbfullpath, 100);
}
imagedestroy($canvas);
imagedestroy($img);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment