20

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?

Jason Spick
  • 6,028
  • 13
  • 40
  • 59
  • command line imagemagick has `convert`. try `convert orig.psd output.jpg` and see if it'll even do that - if it does, then you can start messing with the resizing options. If it doesn't, then you won't waste time barking up the wrong tree. – Marc B Sep 20 '12 at 16:53
  • is there a way I can do command line stuff from php(imagick)? – Jason Spick Sep 20 '12 at 17:09
  • That's just so you can test the conversion and see if it'll even work. One simple command v.s. a few hours trying to bang out a php script to do the same. – Marc B Sep 20 '12 at 19:06

2 Answers2

23

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 Spick
  • 6,028
  • 13
  • 40
  • 59
6

@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);;
}
manu3569
  • 183
  • 2
  • 3
  • Thanks, for the feedback. It's amazing how quickly you can learn something, come back in a month and you would never do it that way again. Some of these features you commented out as repetition are actually necessary as they are application specific. I appreciate the criticism. thanks! – Jason Spick Jul 10 '13 at 19:35
  • 1
    Have you actually tested this code? It doesn't work for me, specifically the `list($width, $height) = $image->getImageGeometry()` part. since the result is not array of size 2, but associative `array('width'=>23, 'height'=>42)` – psycho brm Sep 15 '13 at 00:11
  • Exactly: list($width, $height) = $image->getImageGeometry() doesnt works – Kakitori Oct 18 '18 at 08:36