2011-07-01 21 views
1

私はimagick拡張を使用してPHPスクリプトを作成しています。私がスクリプトにして欲しいのは、ユーザーがアップロードした画像を撮って、そこから200x128のサムネイルを作成することです。PHP Imagickは黒い背景でサイズを変更します

これだけではありません。明らかに、すべての画像が200x128の縦横比に合うわけではありません。だから私がスクリプトにしたいのは、黒い背景でギャップを埋めることです。

イメージはサイズ変更されますが、黒い背景がなく、サイズが正しくありません。基本的には、イメージは常に200x128でなければなりません。サイズ変更された画像は中央に表示され、残りのコンテンツは黒で塗りつぶされます。

アイデア?

は、ここに私のコードです:

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

PS:リサイズ文の後に '$ IM-> setImageBackgroundColor(" black ");を入れてみてください – afaolek

答えて

0

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

あなたがImageMagickをインストールする必要があります。

sudo apt-get install imagemagick

を見てみましょう:それは別の例を示し、どのようにお好みの背景色でパッドアウトサムネイルをへ http://www.imagemagick.org/Usage/thumbnails/#creation

を。

+0

質問はimagemagick用のPHPラッパーであるiMagickに固有のものだと思います – kaese

2

私はその昔を知っているが、私は長い間しようとした後の答えを見つけた:

あなたがそうのような真埋めるの$ BESTFITと$の両方でサムネイル画像 (http://php.net/manual/en/imagick.thumbnailimage.php

を使用する必要があります。

$image->thumbnailImage(200, 128,true,true); 
関連する問題