1.header指令
header('content-type:image/png')
header ( 'Content-Type: image/gif' );
header ( 'Content-Type: image/jpeg' );
2.创建画布(在内存中存放)
关键字:imagecreatetruecolor 新建一个真彩色图像
200 图像的宽度
100 图像的高度
$img = imagecreatetruecolor(200,100);
3.输出图像
关键字:imagepng 以PNG格式将图像输出到浏览器上
imagepng($img);
4.销毁图像(释放占用的资源)
关键字:imagedestroy 销毁图像
imagedestroy($img);
5.颜色管理
关键字:imagecolorallocate 为一个图像分配颜色
$color = imagecolorallocate($img,0xcc,0xcc,0xcc);
6.填充颜色
关键字:imagefill 颜色填充
0代表坐标
imagefill($img,0,0,$color);
7.绘制图形
关键字:imagesetpixel 画一个点
先绘制一个黑色的点
$color = imagecolorallocate($img, 0, 0, 0);
imagesetpixel($img, 100, 50, $color);
随机画10个点
$color = imagecolorallocate($img, 0, 0, 0);
//随机画10个点
for ($i=0; $i <10 ; $i++) {
$x = rand(0,200);
$y = rand(0,100);
imagesetpixel($img, $x, $y, $color);
}
imagesetpixel($img, 100, 50, $color);
imageline 画一条线
$color = imagecolorallocate($img, 0, 0,255); 画一条蓝色的线
imageline ($img,0,0,200,100,$color)
随机画10条线
$color = imagecolorallocate($img, 0, 0, 255);
for ($i=0; $i <10; $i++) {
$x1 = rand(0,200);
$y1= rand(0,100);
$x2 = rand(0,200);
$y2 = rand(0,100);
imageline($img, $x1, $y1, $x2, $y2, $color);
}
画矩形
关键字: imagecolorallocate( 画一个矩形) imagefilledrectangl( 画一个矩形并填充)
$color = imagecolorallocate($img, 0, 255, 0);
//imagerectangle($img, 50, 50, 100, 100, $color);
imagefilledrectangle($img, 50, 50, 100, 100, $color);
8.绘制文字
关键字:imagettftext 用true type 字体向图像写入文本
$text = "hello";
$color = imagecolorallocate($img, 255, 0, 255);
$font = "simsun.ttc";
imagettftext($img, 20, 0, 10, 50, $color, $font, $text);