李涛自留地
回到顶部
首页 > php学习 > PHP如何更改imagecreatetruecolor函数背景色?

PHP如何更改imagecreatetruecolor函数背景色?

文章目录

在前几天的《PHP使用imagecreatetruecolor函数颜色一直为黑色怎么办?PHP创建真彩色图像为黑色怎么办?》一文中,当时在实例代码中使用imagecreatetruecolor函数创建了一张图片,一直修改不了这张新建图片的黑色背景色,后来将imagecreatetruecolor函数换成了imagecreate()函数使用后,再给图像分配颜色就能正常更改颜色了,但是这并不是在使用imagecreatetruecolor函数的情况下,昨日无意中发现了更改imagecreatetruecolor函数背景色的方法。

我们如果使用imagecreatetruecolor函数之后再使用imagecolorallocate()函数,是无法直接对imagecreatetruecolor函数新建的图像进行处理的,这个时候我们需要使用imagefill()函数再做进一步处理即可改变imagecolorallocate()函数生成的图像黑色背景。

imagefill()函数用法

1、imagefill()函数语法格式

imagefill(
    resource $image,
    int $x,
    int $y,
    int $color
): bool

2、imagefill()函数作用

对图像的区域颜色进行填充。

3、imagefill()函数参数说明

imagefill()在image图像的坐标x,y(图像左上角为 0, 0)处用color颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。

更改imagecreatetruecolor函数新建图像的背景色实例

<?php
header('Content-type: image/png');
//创建一个宽高为200像素的真彩色图像
$pic = imagecreatetruecolor(200, 200);
// 将背景设为白色
$white = imagecolorallocate($pic, 255, 255, 255);
//从图像的X、Y轴坐标为0的位置开始填充白色背景
imagefill($pic, 0, 0, $white);
//保存图像
imagepng($pic);
//输出图像
echo "<img src=\"$pic\">";
//销毁图像是否内存
imagedestroy($pic);
?> 
PHP如何更改imagecreatetruecolor函数背景色?

在《PHP使用imagecreatetruecolor函数颜色一直为黑色怎么办?PHP创建真彩色图像为黑色怎么办?》一文,虽然最终也实现了自己想要的图像背景颜色,但是在PHP手册中关于imagecreate()函数的说明中建议我们在PHP中使用imagecreatetruecolor()函数来新建图像。

若非作者标注皆为李涛自留地原创文章,转载或复制请以超链接形式并注明出处。