2013-07-16 38 views
6

imagecopyresampled関数が実行されるときに、130x81の画像を130x130の画像に追加するリサイズ画像スクリプトがあります。これは、ベース画像白です。下のコードで、私は本当にいくつかの助けを感謝することができます。php imagecopyresampledは黒の背景を追加します

私が作成した130x130ファイルPHPに合流しようとしている画像は、次のとおりです。 130x81 image

$width = 130; 
$height = 130; 
$filename = 'process-add.jpg'; //130x81px jpg 
$this->_image = imagecreatefromjpeg($filename); 

$background = imagecreatetruecolor(130,130);//create the background 130x130 
$whiteBackground = imagecolorallocate($background, 255, 255, 255); 
imagefill($background,0,0,$whiteBackground); // fill the background with white 

imagecopyresampled($background, $this->_image,(130-$width)/2,(130-$height)/2, 0, 0, $width, $height, $width, $height); // copy the image to the background 

ImageJpeg ($background,null,100); //display 

私は追加するには、複数の記事に読んだ:

imagealphablending($background, false); 

修正する必要があるコードにそれは何の違いもありません。

ありがとうございます!

答えて

10

これが解決されました。問題は、imagecopyresampled呼び出しの幅と高さで発生しました。以下のコードブロックを参照してください。

<? 

ini_set('allow_url_fopen', true); 
$filename = 'http://img.yessy.com/1402152287-17201a.jpg'; // 130x81 
$image = imagecreatefromjpeg($filename); 
list($originalWidth, $originalHeight) = getimagesize($filename); 

// Size of image to create 
$width = 130; 
$height = 130; 

$background = imagecreatetruecolor($width, $height);//create the background 130x130 
$whiteBackground = imagecolorallocate($background, 255, 255, 255); 
imagefill($background,0,0,$whiteBackground); // fill the background with white 

imagecopyresampled($background, $image, 0, ($height - $originalHeight)/2, 0, 0, $originalWidth, $originalHeight, $originalWidth, $originalHeight); // copy the image to the background 

header("Content-type: image/jpeg"); 
ImageJpeg ($background,null,100); //display 
?> 
関連する問題