I'm creating thumbnails for a Digital asset manager, what is the best way to do this with imagemagick?
is there good resource out there?
I solved it and will share with the WORLD! it will convert .ai, .psd, .jpg, .png, .gif into thumbnails.
Here is a function that takes 4 params:
$dir - directory to save to.
$tmpName - the name to name the file excluding the extension.
$fileType - self explanatory.
$size - Large or small.
function thumbGenerator($dir,$tmpName,$fileType,$size){
$saveFileType = "png";
$imagePath = $dir.$tmpName.".".$fileType;
$image = new Imagick();
$image->readimage($imagePath);
if($fileType == "psd"){
$image->setIteratorIndex(0);
}
$dimensions = $image->getImageGeometry();
$width = $dimensions['width'];
$height = $dimensions['height'];
if($size == "large"){
$maxWidth = 720;
$maxHeight =720;
}
if($size == "small"){
$maxWidth = 250;
$maxHeight =250;
}
if($height > $width){
//Portrait
if($height > $maxHeight)
$image->thumbnailImage(0, $maxHeight);
$dimensions = $image->getImageGeometry();
if($dimensions['width'] > $maxWidth){
$image->thumbnailImage($maxWidth, 0);
}
}elseif($height < $width){
//Landscape
$image->thumbnailImage($maxWidth, 0);
}else{
//square
$image->thumbnailImage($maxWidth, 0);
}
if($size == "large"){
$image->writeImage($dir . $tmpName."-lg.".$saveFileType);
}
if($size == "small"){
$image->writeImage($dir . $tmpName."-sm.".$saveFileType);;
}
}
@Jason - Thanks for sharing. Here are a few tips for cleaner and easier to maintain/extend code. Again, a lot of it depends on your requirements. Also, I didn't actually run this code, so forgive any typos.
$dir - directory to save to.
$tmpName - the name to name the file excluding the extension.
$fileType - self explanatory.
$size - Large or small. You may consider taking a pixel width value for the thumbnail rather than a string for a predefined width. Let's say you will have the need for a larger thumbnail in a new section of your page in the future (i.e. Retina-ready icons with 500px for "small" thumbnails). You should preferably define the size in the new part of the code rather than in the shared thumbGenerator function
function thumbGenerator($dir,$tmpName,$fileType,$size){
$saveFileType = "png";
$imagePath = $dir.$tmpName.".".$fileType;
$image = new Imagick();
$image->readimage($imagePath);
if($fileType == "psd"){
$image->setIteratorIndex(0);
}
/* Simplify this code section below
$dimensions = $image->getImageGeometry();
$width = $dimensions['width'];
$height = $dimensions['height'];
*/
list($width,$height) = $image->getImageGeometry(); // <--- new code
/* Use $size for the pixel width/height instead and remove the code below
if($size == "large"){
$maxWidth = 720;
$maxHeight =720;
}
if($size == "small"){
$maxWidth = 250;
$maxHeight =250;
}
*/
if($height > $width){
//Portrait
if($height > $size)
$image->thumbnailImage(0, $size);
$dimensions = $image->getImageGeometry();
if($width > $size){ // <--- use the previously created $width variable
$image->thumbnailImage($size, 0);
}
/* Don't need this duplicate code.
}elseif($height < $width){
//Landscape
$image->thumbnailImage($maxWidth, 0);
*/
}else{
// square or landscape
$image->thumbnailImage($maxWidth, 0);
}
/* DRY - do not repeat yourself - Simplify it and use the pixel width in the image name
if($size == "large"){
$image->writeImage($dir . $tmpName."-lg.".$saveFileType);
}
if($size == "small"){
$image->writeImage($dir . $tmpName."-sm.".$saveFileType);;
}
*/
$image->writeImage($dir . $tmpName."-".$size.".".$saveFileType);;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With