Search code examples
phpimagemagickimagickpsd

Convert .psd and .ai to PNG/JPG with imagick


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?


Solution

  • 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);;
        }
    }