图像函数 imagecreatetruecolor()和imagecreate()的异同点

共同点:这两个函数都是用于创建画布

区别:

1.不同的是创建画布和为画布填充颜色的流程不一样;

用imagecreatetruecolor(int x,int y)创建的是一幅大小为 x和 y的图像(默认为黑色),如想改变背景颜色则需要为画布分配颜色imagecolorallcollate(resource:image,red:int,green:int,blue:int),然后为画布填充颜色函数imagefill(resource:image,int x, int y,$color); 

具体代码:

<?php

//设置文件类型为图像
header('Content-Type:image/png');

//创建画布
$image = imagecreatetruecolor(200,200);

//为画布分配颜色
$color = imagecolorallocate($image,174,48,96);

//填充颜色
imagefill($image,0,0,$color);

//生成图像
imagepng($image);

//保存图像,生成图像和保存图像需要分为两步,要么只能生成,要么只能保存
imagepng($image,'./1.png');
?>

用imagecreate(int x,int y)创建的也是一幅大小为 x和 y的图像(默认没有颜色,需要指定颜色),如想改变背景颜色则需要为画布分配颜色imagecolorallcollate(resource:image,red: int,green:int ,blue:int),和上面不同的是不需要填充,因为imagecolorallcollate()在imagecreate()函数创建画布的情况下自动填充.

具体代码:

<?php
//设置文件类型为图像
header('Content-Type:image/png');

//创建画布
$image = imagecreate(200,200);

//为画布分配颜色并填充画布
$color = imagecolorallocate($image,174,48,96);

//生成图像
imagepng($image);

//保存图像,生成图像和保存图像需要分为两步,要么只能生成,要么只能保存
imgaepng($image,'./1.png');
?>

2.支持的颜色数不同,imagecreatetruecolor()支持的颜色更多一些.

原文地址:https://www.cnblogs.com/fantianlong/p/9898579.html