imagescreatetruecolor有白色背景

Jea*_*net 15 php image

我很确定我需要使用imagefilledrectangle来获得白色背景而不是黑色......只是不确定如何.我尝试了几种方法.

$targetImage = imagecreatetruecolor($thumbw,$thumbh);
imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbHeight,imagesx($sourceImage),imagesy($sourceImage));
Run Code Online (Sandbox Code Playgroud)

Gus*_*ram 35

图像填充PHP手册条目:

$image = imagecreatetruecolor(100, 100);

// set background to white
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);
Run Code Online (Sandbox Code Playgroud)


KIK*_*are 6

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);
Run Code Online (Sandbox Code Playgroud)

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

  • 这应该被接受为正确答案。与此解决方案相比,“imagefill()”确实很慢,并且可能会在内存限制较低的服务器上导致问题。 (2认同)