·您现在的位置: 江北区云翼计算机软件开发服务部 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> 动态图像处理 — php(34)

动态图像处理 — php(34)

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
动态图像处理 — php(34)1 PHP中GD库的使用

PHP 不仅限于只产生HTML 的输出,还可以创建及操作多种不同格式的图像文件。PHP提供了一些内置的图像信息函数,也可以使用GD函数库创建新图像或处理已有的图像。目前GD2库支持JPEG、PNG和WBMP格式。但不再支持GIF格式。

• JPEG 是一种压缩标准的名字,通常是用来存储照片或者存储具有丰富色彩和色彩层次的图像。这种格式使用了有损压缩。

• PNG 是可移植的网络图像,对图像采用了无损压缩标准。

• WBMP是专门为无线通讯设备设计的文件格式。但是并没有得到广泛应用。

2 图像的生成步骤

在PHP中创建一个图像应该完成如下所示的4个步骤:1.创建一个背景图像(也叫画布),以后的操作都基于此背景图像。2.在背景上绘制图像轮廓或输入文本。3.输出最终图形4.释放资源

<?php// 创建背景图像$height = 200;$width = 200;$im = ImageCreateTrueColor($width, $height); //建立空白背景$white = ImageColorAllocate ($im, 255, 255, 255);//设置绘图颜色$blue = ImageColorAllocate ($im, 0, 0, 64);imageFill($im, 0, 0, $blue);//绘制背景imageLine($im, 0, 0, $width, $height, $white); //画线imageString($im, 4, 50, 150, 'Sales', $white); //添加字串header('Content-type: image/png');imagePng($im); //以PNG 格式将图像输出imageDestroy($im);?>
3 画布管理

imagecreate--新建一个基于调色板的图像􀂾resource imagecreate( intx_size, inty_size)􀂾本函数用来建立空新画布,参数为图片大小,单位为像素(pixel)。支持256色。􀂙imagecreatetruecolor--新建一个真彩色图像􀂾resource imagecreatetruecolor( intx_size, inty_size)􀂾新建一个真彩色图像画布,需要GD 2.0.1 或更高版本,不能用于GIF 文件格式。􀂙imagedestroy--销毁一图像􀂾boolimagedestroy( resource image )􀂾imagedestroy() 释放与image关联的内存。

其他函数

resource imagecreatefrompng( string filename )从PNG 文件或URL 新建一图像􀂙resource imagecreatefromjpeg( string filename )􀂙从JPEG 文件或URL 新建一图像􀂙resource imagecreatefromgif( string filename􀂙从GIF 文件或URL 新建一图像􀂙resource imagecreatefromwbmp( string filename )从WBMP 文件或URL 新建一图像􀂙intimagesx( resource image ) ---取得图像宽度􀂙ntimagesy( resource image ) ---取得图像高度

4 设置颜色

imagecolorallocate--为一幅图像分配颜色􀂙语法:intimagecolorallocate( resource image, intred, intgreen, intblue )􀂙imagecolorallocate() 返回一个标识符,代表了由给定的RGB 成分组成的颜色。image参数是􀂙imagecreatetruecolor()函数的返回值。red,green和blue分别是所需要的颜色的红,绿,蓝成分。这些参数是0 到255 的整数或者十六进制的0x00 到0xFF。imagecolorallocate() 必须被调用以创建每一种用在image所代表的图像中的颜色。

5 生成图片

imagegif--以GIF 格式将图像输出到浏览器或文件􀂾语法:boolimagegif(resource image [,string filename] )􀂙imagejpeg--以JPEG 格式将图像输出到浏览器或文件􀂾语法:boolimagejpeg(resource image [,string filename [, intquality]] )􀂙imagepng--以PNG 格式将图像输出到浏览器或文件􀂾语法:boolimagepng(resource image [,string filename] )􀂙imagewbmp--以WBMP 格式将图像输出到浏览器或文件􀂾语法:boolimagewbmp(resource image [, string filename [, intforeground]] )

6绘制图像

imagefill--区域填充􀂾语法:boolimagefill(resourceimage,intx,inty, intcolor)􀂾imagefill() 在image图像的坐标x,y(图像左上角为0, 0)处用color颜色执行区域填充(即与x, y 点颜色相同且相邻的点都会被填充)。􀂙imagesetpixel--画一个单一像素􀂾语法:boolimagesetpixel( resource image, intx, inty, intcolor )􀂾imagesetpixel() 在image图像中用color颜色在x,y坐标(图像左上角为0,0)上画一个点。􀂙imageline--画一条线段􀂾语法:boolimageline( resource image, intx1, inty1, intx2, inty2, intcolor )􀂾imageline() 用color颜色在图像image中从坐标x1,y1到x2,y2(图像左上角为0, 0)画一条线段。

imagerectangle--画一个矩形

􀂾语法:boolimagerectangle( resource image, intx1, inty1, intx2, inty2, intcol)􀂾imagerectangle() 用col颜色在image图像中画一个矩形,其左上角坐标为x1, y1,右下角坐标为x2, y2。图像的左上角坐标为0, 0。􀂙imagefilledrectangle--画一矩形并填充􀂾语法:boolimagefilledrectangle( resource image, intx1, inty1, intx2, inty2, intcolor )􀂾imagefilledrectangle() 在image图像中画一个用color颜色填充了的矩形,其左上角坐标为x1,y1,右下角坐标为x2,y2。0, 0 是图像的最左上角

imagepolygon--画一个多边形􀂾语法:boolimagepolygon( resource image, array points, intnum_points, intcolor )􀂾imagepolygon() 在图像中创建一个多边形。points是一个PHP 数组,包含了多边形的各个顶点坐标,即points[0] = x0,points[1] = y0,points[2] = x1,points[3] = y1,以此类推。num_points是顶点的总数。􀂙imagefilledpolygon--画一多边形并填充􀂾语法:boolimagefilledpolygon( resource image, array points, intnum_points, intcolor )􀂾imagefilledpolygon() 在image图像中画一个填充了的多边形。points参数是一个按顺序包含有多边形各顶点的x和y坐标的数组。num_points参数是顶点的总数,必须大于3。

imageellipse--画一个椭圆􀂾语法:boolimageellipse( resource image, intcx, intcy, intw, inth, intcolor )􀂾imageellipse() 在image所代表的图像中画一个中心为cx,cy(图像左上角为0, 0)的椭圆。w和h分别指定了椭圆的宽度和高度,椭圆的颜色由color指定。􀂙imagefilledellipse--画一椭圆并填充􀂾语法:boolimagefilledellipse( resource image, intcx, intcy, intw, inth, intcolor )􀂾imagefilledellipse() 在image所代表的图像中以cx,cy(图像左上角为0, 0)为中心画一个椭圆。w和h分别指定了椭圆的宽和高。椭圆用color颜色填充。如果成功则返回TRUE,失败则返回FALSE。

imagearc--画椭圆弧􀂾boolimagearc( resource image, intcx, intcy, intw, inth, ints, inte, intcolor )􀂾imagearc() 以cx,cy(图像左上角为0, 0)为中心在image所代表的图像中画一个椭圆弧。w和h分别指定了椭圆的宽度和高度,起始和结束点以s和e参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。􀂙imagefilledarc--画一椭圆弧且填充􀂾boolimagefilledarc( resource image, intcx, intcy, intw, inth, ints, inte, intcolor, intstyle )􀂾imagefilledarc() 在image所代表的图像中以cx,cy(图像左上角为0, 0)画一椭圆弧。如果成功则返回TRUE,失败则返回FALSE。w和h分别指定了椭圆的宽和高,s和e参数以角度指定了起始和结束点。style可以是下列值按位或(OR)后的值:IMG_ARC_PIE、IMG_ARC_CHORD、IMG_ARC_NOFILL、IMG_ARC_EDGED。其中IMG_ARC_PIE 和IMG_ARC_CHORD 是互斥的;IMG_ARC_CHORD 只是用直线连接了起始和结束点,IMG_ARC_PIE 则产生圆形边界(如果两个都用,IMG_ARC_CHORD 生效)。IMG_ARC_NOFILL 指明弧或弦只有轮廓,不填充。IMG_ARC_EDGED 指明用直线将起始和结束点与中心点相连,和IMG_ARC_NOFILL 一起使用是画饼状图轮廓的好方法(而不用填充)

6在图像中绘制文字

imagestring--水平地画一行字符串

􀂾语法:boolimagestring( resource image, intfont, intx, inty, string s, intcol)􀂾imagestring() 用col颜色将字符串s画到image所代表的图像的x,y坐标处(这是字符串左上角坐标,整幅图像的左上角为0,0)。如果font是1,2,3,4 或5,则使用内置字体。􀂙imagestringup--垂直地画一行字符串􀂾语法:boolimagestringup( resource image, intfont, intx, inty, string s, intcol)􀂾imagestring()用col颜色将字符串s垂直地画到image所代表的图像的x, y座标处(图像的左上角为0, 0)。如果font是1,2,3,4 或5,则使用内置字体。

imagechar--水平地画一个字符􀂾语法:boolimagechar( resource image, intfont, intx, inty, string c, intcolor )􀂾imagechar() 将字符串c的第一个字符画在image指定的图像中,其左上角位于x,y(图像左上角为0, 0),颜色为color。如果font是1,2,3,4 或5,则使用内置的字体(更大的数字对应于更大的字体)。􀂙imagecharup--垂直地画一个字符􀂾语法:boolimagecharup( resource image, intfont, intx, inty, string c, intcolor )􀂾imagecharup() 将字符c垂直地画在image指定的图像上,位于x,y(图像左上角为0, 0),颜色为color。如果font为1,2,3,4 或5,则使用内置的字体。􀂙imagettftext--用TrueType 字体向图像写入文本􀂾语法:array imagettftext( resource image, float size, float angle, intx, inty, intcolor, string fontfile, string text )

例子:

<?php$im= imagecreate(150,150); //创建一个150*150的画布$bg= imagecolorallocate($im, 255, 255, 255);//设置画布的背景颜色为白色$black = imagecolorallocate($im, 0, 0, 0); //设置一个颜色变量为黑色$string="LAMPBrother"; //声明一个用于在图像中输出的字符串imageString($im, 3, 28, 70, $string, $black);//水平将字符串$string输出到图像中imageStringUp($im, 3, 59, 115, $string, $black);//垂直由下而上输出$string到图像中for($i=0,$j=strlen($string); $i<strlen($string); $i++,$j--){//使用循环单个字符输出到图像中imageChar($im, 3, 10*($i+1), 10*($i+2), $string[$i], $black);//向下倾斜输出每个字符imageCharUp($im, 3, 10*($i+1), 10*($j+2), $string[$i], $black); //向上倾斜输出每个字符}header('Content-type: image/png'); //设置输出的头部标识符imagepng($im); //输出PNG格式的图片?>