2012-09-20 6 views
14

디지털 자산 관리자에 대한 축소판을 생성하고 있는데, imagemagick으로 이것을 수행하는 가장 좋은 방법은 무엇입니까?imagick으로 .psd 및 .ai를 PNG/JPG로 변환하십시오.

저기에 좋은 자원이 있습니까?

+0

명령 줄 ImageMagick이이'convert'가 공유 thumbGenerator 기능보다 코드의 새로운 부분의 크기를 정의해야합니다. 'convert orig.psd output.jpg'을 시도해보십시오. 그렇다면 그렇게 할지도 모릅니다. 그렇다면 크기 조절 옵션을 사용하여 엉망이 될 수 있습니다. 그렇지 않으면, 잘못된 나무를 짖는 시간을 낭비하지 않을 것입니다. –

+0

거기에 PHP에서 명령 줄 물건을 할 수있는 방법이 있나요 (imagick)? –

+0

그렇기 때문에 변환을 테스트하여 작동 여부를 확인할 수 있습니다. 하나의 간단한 명령 v.s. 몇 시간은 PHP 스크립트를 똑같이하기 위해 노력하고 있습니다. –

답변

17

나는 그것을 풀어서 세상과 나눌 것입니다! .ai, .psd, .jpg, .png, .gif를 미리보기 이미지로 변환합니다. -에 저장하는 디렉토리

$ 해줄 : 여기

4 PARAMS를받는 함수이다.
$ 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의 망막 준비 아이콘). 당신은 바람직하지 않고

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의 배열이 아니고 연관'array ('width'=> 23, 'height'=> 42)' –