Skip to content

Instantly share code, notes, and snippets.

@Lammerink
Created October 20, 2012 22:49
Show Gist options
  • Save Lammerink/3925106 to your computer and use it in GitHub Desktop.
Save Lammerink/3925106 to your computer and use it in GitHub Desktop.
PHP : Image Resize and Crop Using ImageMagick
<?php
// Image Resize and Crop using ImageMagick
// the Function
//imageresize to Thumb
function imageresize($src,$dest,$size=75){
list($w,$h) = getimagesize($src);
if($w > $h){
exec("convert ".$src." -resize x".$size." -quality 100 ".$dest);
}else{
exec("convert ".$src." -resize ".$size." -quality 100 ".$dest);
}
exec("convert ".$dest." -gravity Center -crop ".$size."x".$size."+0+0 ".$dest);
}
?>
<?php
// Set paraters
$imageurl = 'http://blog.hillaryfox.com/wp-content/uploads/2012/08/ArchesPano.jpg'; // Original Image
$imagename = 'image.jpg'; // final image filename
$thumbdir = 'thumbs/'; // where to save the thumbnails
// Checks if there is a $thumbdir if not it creates it
if(!is_dir($thumbdir)){
mkdir($thumbdir);
}
// Activating the function
imageresize($imageurl,$thumbdir.$imagename,300);
// Print result
print "
<p>Here is your squared image.</p>
<p><img src='$thumbdir$imagename' border='0'></p>
";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment