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

20

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

有没有好的资源可以参考?


命令行ImageMagick有convert命令。尝试使用convert orig.psd output.jpg命令,看看它是否能够执行 - 如果可以,那么您可以开始尝试调整大小选项。如果不能执行,则不要浪费时间在错误的方向上努力。 - Marc B
我能否从 PHP(imagick)执行命令行操作的方法? - Jason Spick
这只是为了测试转换并查看它是否有效。一个简单的命令,相对于花费几个小时来编写一个执行相同任务的 PHP 脚本。 - Marc B
2个回答

23

我已经解决了这个问题,并将与全世界分享!它可以将 .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);;
    }
}

如果源文件是ai,我们需要将迭代索引设置为0吗? - Harry baldaniya

6

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

谢谢你的反馈。令人惊讶的是,你可以学得这么快,一个月后回来,你就不会再那样做了。你注释掉的一些功能实际上是应用程序特定的必要功能,而不是重复的。我感谢你的批评。谢谢! - Jason Spick
1
你有测试过这段代码吗?对我来说它不起作用,特别是list($width, $height) = $image->getImageGeometry()这一部分。因为结果不是大小为2的数组,而是关联数组array('width'=>23, 'height'=>42) - psycho brm
准确地说:list($width, $height) = $image->getImageGeometry() 不起作用。 - Kakitori

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接