Home  >  Article  >  Backend Development  >  Dynamic image processing — php (34)

Dynamic image processing — php (34)

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

1 Use of GD library in PHP

PHP is not limited to only generating HTML output, but can also create and manipulate image files in a variety of different formats. PHP provides some built-in image information functions, and you can also use the GD function library to create new images or process existing images. Currently the GD2 library supports JPEG, PNG and WBMP formats. But the GIF format is no longer supported.

• JPEG is the name of a compression standard commonly used to store photos or images with rich colors and color gradations. This format uses lossy compression.

• PNG is a portable web image that uses a lossless compression standard for images.

• WBMP is a file format specially designed for wireless communication devices. But it has not been widely used.

2 Image generation steps

Creating an image in PHP should complete the following 4 steps:
1. Create a background image (also called canvas), and subsequent operations are based on this Background image.
2. Draw image outline or enter text on the background.
3. Output the final graphics
4. Release resources

<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 Canvas management

imagecreate--Create a new palette-based image
??resource imagecreate( intx_size, inty_size)
?? This function is used to create an empty new canvas. The parameter is the image size and the unit is pixel. Support 256 colors.
??imagecreatetruecolor--Create a new true color image
??resource imagecreatetruecolor(intx_size, inty_size)
??Create a new true color image canvas, which requires GD 2.0.1 or higher and cannot be used GIF file format.
??imagedestroy--Destroy an image
??boolimagedestroy( resource image )
??imagedestroy() Releases the memory associated with the image.

Other functions

resource imagecreatefrompng( string filename )
Create a new image from a PNG file or URL
??resource imagecreatefromjpeg( string filename )
??Create a new image from a JPEG file or URL Image
??resource imagecreatefromgif( string filename
??Create a new image from a GIF file or URL
??resource imagecreatefromwbmp( string filename )
Create a new image from a WBMP file or URL
? ?intimagesx( resource image ) --- Get the image width
??ntimagesy( resource image ) --- Get the image height

4 Set color

imagecolorallocate--Assign a color to an image
??Syntax: intimagecolorallocate(resource image, intred, intgreen, intblue)
??imagecolorallocate() returns an identifier that represents the color composed of the given RGB components. The image parameter is
??imagecreatetruecolor. () function's return value. red, green and blue are the red, green and blue components of the desired color. These parameters are integers from 0 to 255 or hexadecimal 0x00 to 0xFF. imagecolorallocate() must be called. To create each color used in the image represented by image

5 Generate image

imagegif--Output the image to a browser or file in GIF format
?? Syntax: boolimagegif( resource image [,string filename] )
??imagejpeg--Output the image to the browser or file in JPEG format
??Syntax: boolimagejpeg(resource image [,string filename [, intquality]] )
??imagepng--Output the image to the browser or file in PNG format
??Syntax: boolimagepng(resource image [,string filename])
??imagewbmp--Output the image to the browser in WBMP format Device or file
?? Syntax: boolimagewbmp(resource image [, string filename [, intforeground]] )

6 Draw image

imagefill--area filling
?? Syntax: boolimagefill(resourceimage , intx, inty, intcolor)
??imagefill() performs area filling with color color at the coordinates x, y of the image image (the upper left corner of the image is 0, 0) (that is, the same and similar color as the x, y point) Neighboring points will be filled in).
??imagesetpixel--draw a single pixel
??Syntax: boolimagesetpixel(resource image, intx, inty, intcolor)
??imagesetpixel() Use color color in the image image at the x, y coordinates Draw a point on (the upper left corner of the image is 0,0).
??imageline--Draw a line segment
??Syntax: boolimageline(resource image, intx1, inty1, intx2, inty2, intcolor)
??imageline() Use color color from coordinates in image image Draw a line segment from x1, y1 to x2, y2 (the upper left corner of the image is 0, 0).

imagerectangle--Draw a rectangle

??Syntax: boolimagerectangle(resource image, intx1, inty1, intx2, inty2, intcol)
??imagerectangle() uses col color to draw a rectangle in the image image, its upper left corner coordinates are x1, y1, right The coordinates of the lower corner are x2, y2. The coordinates of the upper left corner of the image are 0, 0.
??imagefilledrectangle--Draw a rectangle and fill it
??Syntax: boolimagefilledrectangle( resource image, intx1, inty1, intx2, inty2, intcolor )
??imagefilledrectangle() Draw a rectangle in the image The rectangle filled with color has the upper left corner coordinates x1, y1, and the lower right corner coordinates x2, y2. 0, 0 is the upper left corner of the image

imagepolygon--draw a polygon
?? Syntax: boolimagepolygon( resource image, array points, intnum_points, intcolor )
??imagepolygon() Creates a polygon in an image. points is a PHP array that contains the coordinates of each vertex of the polygon, that is, points[0] = x0, points[1] = y0, points[2] = x1, points[3] = y1, and so on. num_points is the total number of vertices.
??imagefilledpolygon--Draw a polygon and fill it
??Syntax: boolimagefilledpolygon(resource image, array points, intnum_points, intcolor)
??imagefilledpolygon() Draw a filled polygon in the image image . The points parameter is an array containing the x and y coordinates of each vertex of the polygon in order. The num_points parameter is the total number of vertices and must be greater than 3.

imageellipse--Draw an ellipse
?? Syntax: boolimageellipse( resource image, intcx, intcy, intw, inth, intcolor )
??imageellipse() Draw a center in the image represented by image is the ellipse of cx, cy (the upper left corner of the image is 0, 0). w and h specify the width and height of the ellipse respectively, and the color of the ellipse is specified by color.
??imagefilledellipse--draw an ellipse and fill it
??Syntax: boolimagefilledellipse( resource image, intcx, intcy, intw, inth, intcolor )
??imagefilledellipse() in the image represented by image Draw an ellipse with cx, cy (the upper left corner of the image is 0, 0) as the center. w and h specify the width and height of the ellipse respectively. The ellipse is filled with color. Returns TRUE if successful, FALSE if failed.

imagearc--Draw elliptical arc
??boolimagearc( resource image, intcx, intcy, intw, inth, ints, inte, intcolor )
??imagearc() takes cx, cy (upper left corner of the image) is 0, 0) and draws an elliptical arc in the image represented by image as the center. w and h specify the width and height of the ellipse respectively, and the start and end points are specified in angles with the s and e parameters. 0° is at three o'clock and is drawn in a clockwise direction.
??imagefilledarc--draw an elliptical arc and fill it
??boolimagefilledarc( resource image, intcx, intcy, intw, inth, ints, inte, intcolor, intstyle )
??imagefilledarc() in image In the represented image, draw an elliptical arc using cx, cy (the upper left corner of the image is 0, 0). Returns TRUE if successful, FALSE if failed. w and h specify the width and height of the ellipse respectively, and the s and e parameters specify the starting and ending points in angles. style can be the bitwise OR (OR) of the following values: IMG_ARC_PIE, IMG_ARC_CHORD, IMG_ARC_NOFILL, IMG_ARC_EDGED. Among them, IMG_ARC_PIE and IMG_ARC_CHORD are mutually exclusive; IMG_ARC_CHORD just connects the starting and ending points with a straight line, while IMG_ARC_PIE produces a circular boundary (if both are used, IMG_ARC_CHORD takes effect). IMG_ARC_NOFILL specifies that the arc or chord has only an outline, not a fill. IMG_ARC_EDGED specifies a straight line connecting the start and end points to the center point. Used together with IMG_ARC_NOFILL, it is a good way to draw the outline of the pie chart (without filling)

6 Draw text in the image

imagestring-- Draw a line of string horizontally

?? Syntax: boolimagestring(resource image, intfont, intx, inty, string s, intcol)
??imagestring() Use col color to draw the string s to the image represented The x, y coordinates of the image (this is the coordinate of the upper left corner of the string, and the upper left corner of the entire image is 0, 0). If font is 1, 2, 3, 4 or 5, the built-in font is used.
??imagestringup--draw a line of string vertically
??Syntax: boolimagestringup(resource image, intfont, intx, inty, string s, intcol)
??imagestring() uses col color to change the characters The string s is drawn vertically to the x, y coordinates of the image represented by image (the upper left corner of the image is 0, 0). If font is 1, 2, 3, 4 or 5, the built-in font is used.

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($<span>string</span>); $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>?>

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn