共同點:這兩個函數都是用於創建畫布
區別:
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()支持的顏色更多一些.