2012-09-20 160 views
14

我正在为数字资产管理器创建缩略图,使用imagemagick完成此操作的最佳方法是什么?使用imagick将.psd和.ai转换为PNG/JPG

那里有很好的资源吗?

+0

命令行的ImageMagick具有'convert'。尝试'convert orig.psd output.jpg',看看它是否会这样做 - 如果是这样,那么你可以开始搞乱调整大小的选项。如果没有,那么你就不会浪费时间咆哮错误的树。 –

+0

有没有办法从php(imagick)做命令行的东西? –

+0

这就是为什么你可以测试转换,看看它是否会工作。一个简单的命令v.s.几个小时试图轰炸一个PHP脚本来做同样的事情。 –

回答

17

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

这里是一个函数,采用4个PARAMS:

$ DIR - 目录保存到。
$ tmpName - 命名文件的名称,不包括扩展名。
$ fileType - 不言自明。
$尺寸 - 大或小。

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);; 
    } 
} 
+1

如果它工作,然后接受你的答案。干杯! –

4

@Jason - 感谢分享。这里有一些提示更简洁,更易于维护/扩展代码。同样,这很大程度上取决于您的要求。另外,我没有真正运行这个代码,所以请原谅任何错误。

$ dir - 要保存到的目录。
$ tmpName - 命名文件的名称,不包括扩展名。
$ fileType - 不言自明。
$尺寸 - 大或小。 您可以考虑缩略图的像素宽度值,而不是预定义宽度的字符串。假设您将来需要在网页的新部分放置更大的缩略图(例如,对于“小”缩略图,带有500px的视网膜就绪图标)。应该优选地限定在代码的新部分的尺寸,而不是在共享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);; 
} 
+0

谢谢,反馈。很快你能学到一些东西,一个月后回来,而且你永远不会这么做。其中一些您注释为重复的功能实际上是必要的,因为它们是特定于应用程序的。我赞赏批评。谢谢! –

+0

你真的测试过这段代码吗?它不适合我,特别是'list($ width,$ height)= $ image-> getImageGeometry()'部分。因为结果不是大小为2的数组,而是关联的数组('width'=> 23,'height'=> 42)' –