Heim  >  Artikel  >  Backend-Entwicklung  >  动态图像处理 — php(34)

动态图像处理 — php(34)

WBOY
WBOYOriginal
2016-08-08 09:23:361062Durchsuche

1 PHP中GD库的使用

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

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

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

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

2 图像的生成步骤

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

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

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 )

例子:

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

以上就介绍了动态图像处理 — php(34),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn