2012-09-20 162 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)' –