首先让我们来看下官方文档的说明:
imagecreate — Create a new palette based image
resource imagecreate ( int$width
, int$height
)imagecreate() returns an image identifier representing a blank image of specified size.
We recommend the use of imagecreatetruecolor().
imagecreatetruecolor — Create a new true color image
resource imagecreatetruecolor ( int
$width
, int$height
)imagecreatetruecolor() returns an image identifier representing a black image of the specified size.
imagecolorallocate — Allocate a color for an image
int imagecolorallocate ( resource$image
, int$red
, int$green
, int$blue
)Returns a color identifier representing the color composed of the given RGB components.
imagecolorallocate() must be called to create each color that is to be used in the image represented by
image
.Note:
The first call to imagecolorallocate() fills the background color in palette-based images - images created using imagecreate().
意思是使用imagecreate()创建图像,第一次使用Imagecolorallcote时,所赋给的颜色会自动认为是背景色)
- 用imagecreatetruecolor()创建图像(jpeg,png等,不支持gif)时,图片默认背景颜色是黑色,要想改变背景色,必须使用别的指令如 imagefill() 填充背景。步骤如下:
- 1.创建真彩图片
- 2.分配颜色
- 3.填充颜色。
<?php
header("Content-type:image/png");
$image=imagecreatetruecolor(120,20) or die(" can not");
$background_color=imagecolorallocate($image,0,255,0);
imagefill($image,0,0,$background_color);
$text_color=imagecolorallocate($image,255,0,0);
imagestring($image,1,5,5,"a simple text string",$text_color);
imagepng($image);
imagedestroy($image);
?>
- 用Imagecreate()创建图像时,默认是空白,只要第一次分配颜色,所分配的颜色就是背景色了。
<?php
header ('Content-Type: image/png');
$im = imagecreate(120, 20)
or die('Cannot Initialize new GD image stream');
$background=imagecolorallocate($im,255,0,255);
$text_color = imagecolorallocate($im, 255, 0, 0);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
?>