用imagick将.psd和.ai转换为PNG/JPG

Jas*_*ick 17 php psd imagemagick imagick

我正在为数字资产管理器创建缩略图,使用imagemagick执行此操作的最佳方法是什么?

那里有好的资源吗?

Jas*_*ick 20

我解决了它,并将与世界分享!它会将.ai,.psd,.jpg,.png,.gif转换为缩略图.

这是一个需要4个参数的函数:

$ dir - 要保存的目录.
$ tmpName - 命名文件的名称,不包括扩展名.
$ fileType - 自我解释.
$ size - 大或小.

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);;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

@Jason - 感谢分享.以下是一些更清晰,更易于维护/扩展代码的技巧.同样,很多都取决于您的要求.另外,我实际上没有运行此代码,所以原谅任何错别字.

$ dir - 要保存的目录.
$ tmpName - 命名文件的名称,不包括扩展名.
$ fileType - 自我解释.
$ size - 大或小.您可以考虑为缩略图而不是字符串获取预定义宽度的像素宽度值.假设您将来需要在页面的新部分中使用更大的缩略图(例如,使用500px的Retina-ready图标用于"小"缩略图).您最好在代码的新部分中定义大小,而不是在共享的thumbGenerator函数中定义大小

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);;
}
Run Code Online (Sandbox Code Playgroud)