2012-04-29 8 views
1

私は現在、GD phpライブラリを使用していくつかのイメージレターボックススタイルをスケーリングし、その結果得られる空白を黒で塗りつぶします。私は別のイメージファイル内にあるパターンを使用して空のスペースを埋める必要があります。これを行う方法に関するアイデア?レターボックスイメージと空白を別のイメージで埋めてください

これは私が画像を縮小するために使用していたコードです:

function resize_image($source_image, $destination_width, $destination_height, $type = 0) { 
    // $type (1=crop to fit, 2=letterbox) 
    $source_width = imagesx($source_image); 
    $source_height = imagesy($source_image); 
    $source_ratio = $source_width/$source_height; 
    $destination_ratio = $destination_width/$destination_height; 
    if ($type == 1) { 
     // crop to fit 
     if ($source_ratio > $destination_ratio) { 
      // source has a wider ratio 
      $temp_width = (int)($source_height * $destination_ratio); 
      $temp_height = $source_height; 
      $source_x = (int)(($source_width - $temp_width)/2); 
      $source_y = 0; 
     } else { 
      // source has a taller ratio 
      $temp_width = $source_width; 
      $temp_height = (int)($source_width/$destination_ratio); 
      $source_x = 0; 
      $source_y = (int)(($source_height - $temp_height)/2); 
     } 
     $destination_x = 0; 
     $destination_y = 0; 
     $source_width = $temp_width; 
     $source_height = $temp_height; 
     $new_destination_width = $destination_width; 
     $new_destination_height = $destination_height; 
    } else { 
     // letterbox 
     if ($source_ratio < $destination_ratio) { 
      // source has a taller ratio 
      $temp_width = (int)($destination_height * $source_ratio); 
      $temp_height = $destination_height; 
      $destination_x = (int)(($destination_width - $temp_width)/2); 
      $destination_y = 0; 
     } else { 
      // source has a wider ratio 
      $temp_width = $destination_width; 
      $temp_height = (int)($destination_width/$source_ratio); 
      $destination_x = 0; 
      $destination_y = (int)(($destination_height - $temp_height)/2); 
     } 
     $source_x = 0; 
     $source_y = 0; 
     $new_destination_width = $temp_width; 
     $new_destination_height = $temp_height; 
    } 
    $destination_image = imagecreatetruecolor($destination_width, $destination_height); 
    if ($type > 1) { 
     imagefill($destination_image, 0, 0, imagecolorallocate($destination_image, 0, 0, 0)); 
    } 
    imagecopyresampled($destination_image, $source_image, $destination_x, $destination_y, $source_x, $source_y, $new_destination_width, $new_destination_height, $source_width, $source_height); 
    return $destination_image; 
} 

は------------------

あなたに

EDITありがとうございました私は今、完璧に動作し、このコードを使用しています

、方法は以下の提案使用しています:

 $destination_image = imagecreatetruecolor($destination_width, $destination_height); 

    if ($type > 1) { 
     if ($pattern != NULL) { 
      $pattern = imagecreatefrompng($pattern); 
      imagesettile($destination_image, $pattern); 
      imagefill($destination_image, 0, 0, IMG_COLOR_TILED); 
     } else { 
      imagefill($destination_image, 0, 0, imagecolorallocate($destination_image, 0, 0, 0)); 
     } 
    } 
    imagecopyresampled($destination_image, $source_image, $destination_x, $destination_y, $source_x, $source_y, $new_destination_width, $new_destination_height, $source_width, $source_height); 
    return $destination_image; 

$パターンが背景へのファイルパスであるI手紙を記入する魔法使い!

+0

私はimagecopyresampledのようなものを使用したいと考えていると推測していますが、どうすればよいのか分かりません。 – Nath5

答えて

0

まず次いでimagecopy()又はimagecopyresampled()のsource_x/Y/W/Hパラメータを使用して、このリソースへの画像のレターボックス一部をコピーし、上にあなたのレターボックスをオーバーレイするようにタイル張りキャンバスを作成するimagesettile()と背景を準備します。

+0

ありがとう、私はそれらの方法を読んで、私がそれを理解することができるかどうかを見ます! – Nath5

関連する問題