简体   繁体   English

php imagecopyresampled添加黑色背景

[英]php imagecopyresampled adds black background

I have a resize image script that takes a 130x81 image and adds it to a 130x130 image, when the imagecopyresampled function runs it adds a black background into the space that is left over, even though the base image is white. 我有一个调整大小的图像脚本,它采用130x81图像并将其添加到130x130图像,当imagecopyresampled函数运行时,它会在剩余的空间中添加黑色背景,即使基本图像是白色的。 Code below, I could really appreciate some help. 下面的代码,我真的很感激一些帮助。

The image I am trying to merge onto the 130x130 file php created is: 我试图合并到创建的130x130文件的图像是: 130x81图像

$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

I have read on multiple posts to add: 我已阅读多个帖子添加:

imagealphablending($background, false);

into the code which should fix it, but it doesn't make any difference. 进入应修复它的代码,但它没有任何区别。

Thanks in advance! 提前致谢!

This has been solved. 这已经解决了。 The issue was with teh width and height on the imagecopyresampled call. 问题在于imagecopyresampled调用的宽度和高度。 See the code block below: 请参阅下面的代码块:

<?

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
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM