2012-05-14 14 views
10

私は画像操作に関するPHP + GDについてウェブ上でいくつかのことを見つけましたが、私には何も探していないようです。php GD画像に余白を追加

誰かが任意のサイズの画像をアップロードしていますが、私が作成したスクリプトは、アスペクト比を維持したまま200px幅以上200px以下に画像のサイズを変更します。たとえば、最終的な画像は150ピクセルx 200ピクセルです。私がやりたいことは、画像をさらに操作して、画像の周りにマットを付けて、元の画像に影響を与えずに200ピクセル×200ピクセルに塗りつぶすことです。たとえば:

Unpadded Image 143x200

Padded image 200x200

私は画像のサイズを変更を取得する必要があるコードはここにある、私はいくつかのことを試してみましたが、確かにパディングを追加の二次処理を実現する問題が生じています。

list($imagewidth, $imageheight, $imageType) = getimagesize($image); 
$imageType = image_type_to_mime_type($imageType); 
$newImageWidth = ceil($width * $scale); 
$newImageHeight = ceil($height * $scale); 
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); 
switch($imageType) { 
    case "image/gif": 
     $source=imagecreatefromgif($image); 
     break; 
    case "image/pjpeg": 
    case "image/jpeg": 
    case "image/jpg": 
     $source=imagecreatefromjpeg($image); 
     break; 
    case "image/png": 
    case "image/x-png": 
     $source=imagecreatefrompng($image); 
     break; 
} 
imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height); 
imagejpeg($newImage,$image,80); 
chmod($image, 0777); 

私はimagecopyresampled()呼び出しの後imagecopy()権利を使用する必要があります考えています。イメージが既に私が望むサイズになっているように、ちょうど200×200のイメージを作成し、$ newImageをその中心(vertとhoriz)に貼り付けます。まったく新しいイメージを作成して2つをマージする必要がありますか、それとも私が作成したイメージを埋め込む方法がありますか($newImage)?事前のおかげで、すべてのチュートリアル私はどこにも私を導いていない発見した、と私はSOに見つけのみ適用1は

答えて

13
  1. オープン
  2. は、新しい空白のイメージを作成し、元の画像:(アンドロイドのためだった。
  3. &が新しいイメージ
  4. 保存新しい画像上に中心にオリジナル画像をコピーサイズを変更する背景色ご希望
  5. 使用ImageCopyResampledで新しいイメージを塗りつぶし

代わりにあなたのswitch文のあなたも、この意志の自動画像の種類を検出

$img = imagecreatefromstring(file_get_contents ("path/to/image")); 

使用することができます(にi​​magetypeがでサポートされている場合、あなたのインストール)

コードサンプルを更新

$orig_filename = 'c:\temp\380x253.jpg'; 
$new_filename = 'c:\temp\test.jpg'; 

list($orig_w, $orig_h) = getimagesize($orig_filename); 

$orig_img = imagecreatefromstring(file_get_contents($orig_filename)); 

$output_w = 200; 
$output_h = 200; 

// determine scale based on the longest edge 
if ($orig_h > $orig_w) { 
    $scale = $output_h/$orig_h; 
} else { 
    $scale = $output_w/$orig_w; 
} 

    // calc new image dimensions 
$new_w = $orig_w * $scale; 
$new_h = $orig_h * $scale; 

echo "Scale: $scale<br />"; 
echo "New W: $new_w<br />"; 
echo "New H: $new_h<br />"; 

// determine offset coords so that new image is centered 
$offest_x = ($output_w - $new_w)/2; 
$offest_y = ($output_h - $new_h)/2; 

    // create new image and fill with background colour 
$new_img = imagecreatetruecolor($output_w, $output_h); 
$bgcolor = imagecolorallocate($new_img, 255, 0, 0); // red 
imagefill($new_img, 0, 0, $bgcolor); // fill background colour 

    // copy and resize original image into center of new image 
imagecopyresampled($new_img, $orig_img, $offest_x, $offest_y, 0, 0, $new_w, $new_h, $orig_w, $orig_h); 

    //save it 
imagejpeg($new_img, $new_filename, 80); 
+0

switch文の洞察は素晴らしく、switch文よりもはるかに効率的です。私は想像されたimagecopyについて混乱している。元の画像のサイズ変更は完了しており、200×200ピクセルの背景に貼り付けています。私の理解は、imagecopyresampledは元の画像のサイズを再調整するということですか? – MaurerPower

+0

更新された応答を参照してください – bumperbox

+0

それは素晴らしい仕事!私は、私が望んだ正確な結果を得るために少し変更されたバージョンを使用して終わったが、これは間違いなく正しい道に私を得た!ありがとうbumberbox!受け入れられupvoted! – MaurerPower