imagecreatetruecolor有白色背景

时间:2011-11-15 11:00:03

标签: php image

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

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

3 个答案:

答案 0 :(得分:33)

来自PHP manual entry for imagefill

$image = imagecreatetruecolor(100, 100);

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

答案 1 :(得分:7)

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);

编写代码时通常需要考虑速度。

答案 2 :(得分:3)

$targetImage = imagecreatetruecolor($thumbw,$thumbh);

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

// fill entire image
imagefill($targetImage, 0, 0, $color);

imagecolorallocate:http://www.php.net/manual/en/function.imagecolorallocate.php

imagefill:http://php.net/manual/en/function.imagefill.php