2011-07-01 7 views
1

imagick 확장자를 사용하여 PHP 스크립트를 작성하고 있습니다. 스크립트에서 사용자가 업로드 한 이미지를 가져 와서 200x128 크기의 미리보기 이미지를 만들면됩니다.PHP Imagick의 크기가 검은 색으로 변경되었습니다.

그게 유일한 것이 아닙니다. 분명히 모든 이미지가 200x128의 화면비에 맞지는 않습니다. 따라서 스크립트에서 수행하고자하는 작업은 검정색 배경의 틈을 채우는 것입니다.

지금은 이미지 크기가 조정되지만 검은 색 배경이없고 크기가 올바르지 않습니다. 기본적으로 이미지는 항상 200x128이어야합니다. 크기가 조정 된 이미지는 가운데로 이동하고 나머지 내용은 검은 색으로 채워집니다.

아이디어가 있으십니까? 당신은 ImageMagick를 설치해야합니다

function portfolio_image_search_resize($image) { 

    // Check if imagick is loaded. If not, return false. 
    if(!extension_loaded('imagick')) { return false; } 

    // Set the dimensions of the search result thumbnail 
    $search_thumb_width = 200; 
    $search_thumb_height = 128; 

    // Instantiate class. Then, read the image. 
    $IM = new Imagick(); 
    $IM->readImage($image); 

    // Obtain image height and width 
    $image_height = $IM->getImageHeight(); 
    $image_width = $IM->getImageWidth(); 

    // Determine if the picture is portrait or landscape 
    $orientation = ($image_height > $image_width) ? 'portrait' : 'landscape'; 

    // Set compression and file type 
    $IM->setImageCompression(Imagick::COMPRESSION_JPEG); 
    $IM->setImageCompressionQuality(100); 
    $IM->setResolution(72,72); 
    $IM->setImageFormat('jpg'); 

    switch($orientation) { 

     case 'portrait': 

      // Since the image must maintain its aspect ratio, the rest of the image must appear as black 
      $IM->setImageBackgroundColor("black"); 

      $IM->scaleImage(0, $search_thumb_height); 

      $filename = 'user_search_thumbnail.jpg'; 

      // Write the image 
      if($IM->writeImage($filename) == true) { 
       return true; 
      } 
      else { 
       return false; 
      } 
      break; 

     case 'landscape': 

      // The aspect ratio of the image might not match the search result thumbnail (1.5625) 
      $IM->setImageBackgroundColor("black"); 

      $calc_image_rsz_height = ($image_height/$image_width) * $search_thumb_width; 

      if($calc_image_rsz_height > $search_thumb_height) { 
       $IM->scaleImage(0, $search_thumb_height); 
      } 
      else { 
       $IM->scaleImage($search_thumb_width, 0); 
      } 

      $filename = 'user_search_thumbnail.jpg'; 

      if($IM->writeImage($filename) == true) { 
       return true; 
      } 
      else { 
       return false; 
      } 

     break; 

    } 

} 
+0

200x128px 캔버스 중간에 128x128px라고하는 작은 이미지를 그릴 수 있습니까? –

+0

Imagick에 대해 많이 알지는 못하지만 그래픽에 대한 지식이 부족하기 때문에 검정색 채우기로 사각형을 만들고 너비에 미리보기 이미지를 오버레이해야한다고 생각합니다. Imagick에서 그렇게 어려워서는 안됩니다. 맞습니까? – afaolek

+1

추신 : 크기 조정 문 다음에'$ IM-> setImageBackgroundColor ("black");'줄을 넣으십시오. – afaolek

답변

0

exec('convert -define jpeg:size=400x436 big_image.jpg -auto-orient -thumbnail 200x218 -unsharp 0x.5 thumbnail.gif');

:

여기 내 코드입니다.

sudo apt-get install imagemagick

는에서보세요 : http://www.imagemagick.org/Usage/thumbnails/#creation

그것은 더 예와 방법의 선택의 배경 색상으로 미리 밖으로 패드를 보여줍니다.

+0

질문이 imagemagick 용 PHP 래퍼 인 iMagick에만 해당한다고 생각합니다. – kaese

2

나는 옛 알고 있지만 나는 오랫동안 노력 후 답을 발견

$image->thumbnailImage(200, 128,true,true); 
:

당신이 모두 $ BESTFIT와 thumbnailimage (http://php.net/manual/en/imagick.thumbnailimage.php)

을 사용하고 $ 진실한과 같이 작성해야

관련 문제