2013-07-16 59 views
6

我有一个调整大小的图像脚本,需要一个130x81的图像,并将其添加到130x130的图像,该imagecopyresampled功能运行时,它增加了一个黑色的背景成剩下的空间,即使基本图像是白色的。下面的代码,我真的很感谢一些帮助。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 
?>