PHP中的GD库可用于创建和处理图片,一般通过以下4个步骤对图像进行操作。
(1)创建画布。
(2)在画布上绘制图形。
(3)保存并输出结果图像。
(4)销毁图像资源。
12.2.1 创建画布
使用imagecreate()函数可创建一个基于调色板的图像。语法如下:
function imagecreate ($width, $height) false|resource
imagecreate() 返回一个图像标识符,代表了一幅大小为x_size和y_size的空白图像。
下面的示例演示如何用imagecreate()创建一个空白的画布并输出一个PNG格式的图片。代码如下:
<?php
header("Content-type:image/png");
$im=@imagecreate(100,50)
or die("Cannot Initialize new GD image stream");
$background_color=imagecolorallocate($im,255,255,0);//定义背景颜色
imagepng($im);//输出png格式图像
imagedestroy($im);//销毁图像资源,释放内存
执行以上代码,在浏览器中的显示结果
也可以使用imagecreatetruecolor()创建画布资源。语法如下:
其和imagecreate一样都是返回一个图像画布资源。以下实例演示如何用imagecreatetruecolor()创建画布,代码如下:
<?php
header("Content-type:image/png");
$im=@imagecreatetruecolor(120,50)
or die("Cannot Initialize new GD image stream");
$text_color=imagecolorallocate($im,233,14,91);
imagepng($im);//输出png格式图像
imagedestroy($im);//销毁图像资源,释放内存
执行以上代码,在浏览器中的显示结果
+
12.2.2 定义颜色
给图像的边框背景和文字等元素指定颜色可用imagecolorallocate(),语法如下:
function imagecolorallocate ($image, $red, $green, $blue) false|int
Allocate a color for an image
imagecolorallocate()返回一个标识符,代表由给定的RGB成分组成的颜色。red、green和blue分别是所需要的颜色的红、绿、蓝成分。这些参数是0到255的整数或者十六进制的0x00到0xFF。imagecolorallocate()必须被调用,以创建每一种用在image所代表的图像中的颜色。
在12.2.1小节的两个例子中已经使用到了imagecolorallocate()两个函数。除了imagecolorallocate()函数之外,还可以使用imagecolorallocatealpha()给图像分配颜色,其语法如下:
imagecolorallocatealpha()的行为和imagecolorallocate()相同,但多了一个额外的透明度参数alpha,其值从0到127。0表示完全不透明,127表示完全透明。如果图像分配颜色失败,就返回false。
function imagecolorallocatealpha ($image, $red, $green, $blue, $alpha) false|int
使用示例如下:
<?php
$size = 300;
$image=imagecreatetruecolor($size,$size);
//用白色背景加黑色边框画个方框
$back=imagecolorallocate($image,255,255,255);
$border = imagecolorallocate($image,0,0,0);
imagefilledrectangle($image,0,0,$size-1,$size-1,$back);
imagerectangle($image,0,0,$size-1,$size-1,$border);
$yellow_x=100;
$yellow_y=75;
$red_x=120;
$red_y=165;
$blue_x=187;
$blue_y=125;
$radius=150;
//用alpha值分配一些颜色
$yellow=imagecolorallocatealpha($image,255,255,0,75);
$red=imagecolorallocatealpha($image,255,0,0,75);
$blue=imagecolorallocatealpha($image,0,0,255,75);
//画三个盗抢的圆
imagefilledellipse($image,$yellow_x,$yellow_y,$radius,$radius,$yellow);
imagefilledellipse($image,$red_x,$red_y,$radius,$radius,$red);
imagefilledellipse($image,$blue_x,$blue_y,$radius,$radius,$blue);
//不要忘记输出正确的header!
header("Content-type:image/png");
//最后输出结果
imagepng($image);
imagedestroy($image);
执行以上代码,在浏览器中的输出结果
12.2.3 绘制图形
PHP的GD函数库提供了许多绘制图形的函数,可以绘制椭圆、矩形、多边形等。
1.绘制椭圆
使用imageellipse()画一个椭圆,语法如下:
function imageellipse ($image, $cx, $cy, $width, $height, $color) bool
image是由图像创建函数(例如imagecreatetruecolor())返回的图像资源。cx是中间的x坐标。cy是中间的y坐标。width表示椭圆的宽度。height表示椭圆的高度。color表示椭圆的颜色。颜色标识符由imagecolorallocate() 创建。使用该函数成功时返回true,失败时返回false,使用示例如下:
<?php
//新建一个空白图像
$image = imagecreatetruecolor(400,300);
//填充背景色
$bg =imagecolorallocate($image,0,0,0);
//选择椭圆的颜色
$col_ellipse = imagecolorallocate($image,255,255,255);
//画一个椭圆
imageellipse($image,200,150,300,200,$col_ellipse);
//输出图像
header("Content-type:image/png");
imagepng($image);
imagedestroy($image);
执行以上代码,在浏览器中的显示结果
2.绘制多边形
PHP中使用imagefilledpolygon绘制多边形,语法如下:
function imagefilledpolygon ($image, array $points, $num_points, $color) bool
imagefilledpolygon()在image图像中画一个填充了的多边形。points参数是一个按顺序包含有多边形各顶点的x和y坐标的数组。num_points参数是顶点的总数,必须大于3。
使用示例如下:
<?php
//建立多边形各顶点坐标的数组
$values=array(
40,50,//Point 1 (x,y)
20,240,//Point 2 (x,y)
60,60,//Point 3 (x,y)
240,20,//Point 4 (x,y)
50,40,//Point 5 (x,y)
10,10//Point 6 (x,y)
);
//创建图像
$image = imagecreatetruecolor(250,250);
//设定颜色
$bg = imagecolorallocate($image,200,200,200);
$blue=imagecolorallocate($image,0,0,255);
//画一个多边形
imagefilledpolygon($image,$values,6,$blue);
//输出图像
header("Content-type:image/png");
imagepng($image);
imagedestroy($image);
执行以上程序,在浏览器中的显示结果:
3.绘制矩形
PHP中使用imagefilledrectangle()函数绘制矩形,语法如下:
function imagefilledrectangle ($image, $x1, $y1, $x2, $y2, $color) bool
imagefilledrectangle()在image图像中画一个用color颜色填充了的矩形,其左上角坐标为x1、y1,右下角坐标为x2、y2。(0, 0)是图像的最左上角。
<?php
//创建图像
$image = imagecreate(250,250);
$bg = imagecolorallocate($image,10,110,25);
$blue = imagecolorallocate($image,0,9,255);
imagefilledrectangle($image,50,50,100,200,$blue);
//输出图像
header("Content-type:image/png");
imagepng($image);
imagedestroy($image);
执行以上代码,在浏览器中的输出结果:
4.绘制椭圆弧
PHP中使用imagearc()绘制椭圆弧。语法如下:
function imagearc ($image, $cx, $cy, $width, $height, $start, $end, $color) bool
imagearc()以(cx, cy)(图像左上角为(0, 0))为中心在image所代表的图像中画一个椭圆弧。w和h分别指定了椭圆的宽度和高度,起始和结束点以s和e参数用角度指定。0°位于三点钟位置,以顺时针方向绘画。
使用示例如下:
<?php
//创建一个200*200的图像
$img = imagecreatetruecolor(200,200);
//分配颜色
$color = imagecolorallocate($img,200,100,0);
//画一个弧
imagearc($img,100,100,150,100,0,260,$color);
//输出图像
header("Content-type:image/png");
imagepng($img);
imagedestroy($img);
执行以上程序,在浏览器中的输出结果:
12.2.4 绘制文字
PHP中还提供了多个绘制文字的函数。
1. imagechar水平地画一个字符
imagechar语法如下:
function imagechar ($image, $font, $x, $y, $c, $color) bool
imagechar() 将字符串c的第一个字符画在image指定的图像中,其左上角位于(x,y)(图像左上角为(0, 0)),颜色为color。如果font是1、2、3、4或5,就使用内置的字体(更大的数字对应于更大的字体)。
该函数的使用示例如下:
<?php
$im = imagecreate(100,100);
$string = 'PHP';
$bg= imagecolorallocate($im,0,255,255);
$black= imagecolorallocate($im,0,0,0);
//print a black "P" in the top left corner
imagechar($im,5,40,40,$string,$black);
//输出图像
header("Content-type:image/png");
imagepng($im);
imagedestroy($im);
执行以上代码,在浏览器中的输出结果:
2. imagecharup垂直地画一个字符
imagecharup可以垂直地画一个字符,语法如下:
function imagecharup ($image, $font, $x, $y, $c, $color) bool
imagecharup()将字符c垂直地画在image指定的图像上,位于(x, y)(图像左上角为(0, 0)),颜色为color。如果font为1、2、3、4或5,就使用内置的字体。
使用示例如下:
<?php
$im = imagecreate(100,100);
$string = 'PHP';
$bg= imagecolorallocate($im,255,255,255);
$black= imagecolorallocate($im,0,0,0);
//prints a black "Z" on a white backgroud
imagecharup($im,5,40,40,$string,$black);
//输出图像
header("Content-type:image/png");
imagepng($im);
imagedestroy($im);
执行以上代码,在浏览器中的显示结果:
3. imagefttext将文本写入图像
imagefttext语法如下:
function imagefttext ($image, $size, $angle, $x, $y, $color, $fontfile, $text, $extrainfo = null ) array|false
其中,image是图像创建函数返回的图像资源,size是使用的字体大小,angle是角度,如果为0就表示从左到右写入文本,按照逆时针旋转,此值就是旋转的角度。(x, y)是起始坐标,color是写入字体的颜色,fontfile是字体文件的路径。
该函数使用示例如下:
<?php
//Create a 300*100 image
$im = imagecreatetruecolor(300,100);
$red = imagecolorallocate($im,0xFF,0x00,0x00);
$black = imagecolorallocate($im,0x00,0x00,0x00);
//Make the background red
imagefilledrectangle($im,0,0,299,99,$red);
//Path to our tff.font file
$font_file = 'realprizes-italic.ttf';
//Draw the text 'PHP Manual' using font size 13
imagefttext($im,30,20,105,55,$black,$font_file,'PHP Manual');
//输出图像
header("Content-type:image/png");
imagepng($im);
imagedestroy($im);
执行以上程序,在浏览器中的输出结果: