图像创建白色背景的纯彩色

2022-08-30 20:22:20

我很确定我需要使用图像填充矩形才能获得白色背景而不是黑色......只是不知道怎么做。我尝试了几种方法。

$targetImage = imagecreatetruecolor($thumbw,$thumbh);
imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbHeight,imagesx($sourceImage),imagesy($sourceImage));

答案 1

更新(2020年):请参阅下面的答案,以获得比图像填充更快的填充速度:https://stackoverflow.com/a/32580839/1005039

源语言

图像填充的 PHP 手动条目中

$image = imagecreatetruecolor(100, 100);

// set background to white
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);

答案 2

imagefill() 使用泛洪填充,与只在矩形中绘制颜色而不考虑图像内容相比,这是非常缓慢的。所以imagefilledrectangle()会快得多。

// get size of target image
$width  = imagesx($targetImage);
$height = imagesy($targetImage);

// get the color white
$white  = imagecolorallocate($targetImage,255,255,255);

// fill entire image (quickly)
imagefilledrectangle($targetImage,0,0,$width-1,$height-1,$white);

编写代码时,速度通常是一个考虑因素。


推荐