1. 程式人生 > >影象函式 imagecreatetruecolor()和imagecreate()的異同點

影象函式 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()支援的顏色更多一些.