Dynamic image processing-php (34)

Source: Internet
Author: User
1 Use of the GD library in PHP

PHP is not limited to producing only HTML output, but it can also create and manipulate image files in many different formats. PHP provides some built-in image information functions, or you can use the GD library to create new images or work with existing images. Currently, the GD2 Library supports JPEG, PNG, and wbmp formats. GIF format is no longer supported.

JPEG is the name of a compression standard, usually used to store photos or to store images with rich color and color levels. This format uses lossy compression.

PNG is a portable network image that uses lossless compression standards for images.

wbmp is a file format designed specifically for wireless communication devices. But it has not been widely used.

2 Steps for image generation

Creating an image in PHP should complete the 4 steps shown below:
1. Create a background image (also called the canvas), and the subsequent operations are based on this background image.
2. Draw the outline of the image or enter text on the background.
3. Output Final Graphics
4. Releasing Resources

 
    PHP
 //Create a background image$height = $; $width= $; $im= Imagecreatetruecolor ($width, $height);//Create a blank background$white = Imagecolorallocate ($im,255,255,255);//Set Drawing Colors$blue = Imagecolorallocate ($im,0,0, -); Imagefill ($im,0,0, $blue);//Draw BackgroundImageline ($im,0,0, $width, $height, $white);//Draw LineImagestring ($im,4, -, Max,'Sales', $white);//Add a StringHeader'Content-type:image/png'); imagepng ($im); //Export the image in PNG formatImagedestroy ($im);?>

3 Canvas Management

imagecreate--Creating a new palette-based image
?? Resource Imagecreate (intx_size, Inty_size)
?? This function is used to create an empty new canvas, with the parameters of the image size in pixels (pixel). Supports 256 colors.
?? imagecreatetruecolor--Create a new true color image
?? Resource Imagecreatetruecolor (intx_size, Inty_size)
?? Create a new true color image canvas that requires GD 2.0.1 or later and cannot be used in GIF file format.
?? imagedestroy--destroying 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
?? 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 image width
?? Ntimagesy (resource image)---get image height

4 Setting colors

imagecolorallocate--assigning colors to an image
?? Syntax: intimagecolorallocate (resource image, intred, Intgreen, Intblue)
?? Imagecolorallocate () returns an identifier that represents a color made up of a given RGB component. The image parameter is
?? The return value of the Imagecreatetruecolor () function. Red,green and Blue are the red, green, and blue components of the desired color respectively. 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 Creating a picture

imagegif--exporting an image to a browser or file in GIF format
?? Syntax: boolimagegif (resource image [, string filename])
?? imagejpeg--exporting an image to a browser or file in JPEG format
?? Syntax: boolimagejpeg (resource image [, string filename [, intquality]])
?? imagepng--exporting images to a browser or file in PNG format
?? Syntax: boolimagepng (resource image [, string filename])
?? imagewbmp--exporting an image to a browser or file in wbmp format
?? Syntax: boolimagewbmp (resource image [, string filename [, Intforeground]])

6 Drawing Images

imagefill--Area Fill
?? Syntax: Boolimagefill (Resourceimage,intx,inty, Intcolor)
?? Imagefill () performs an area fill with a color color at the coordinates x, y (0, 0) of the image image (that is, the same as x, Y point color, and adjacent points are populated).
?? imagesetpixel--draw a single pixel
?? Syntax: Boolimagesetpixel (resource image, Intx, Inty, Intcolor)
?? Imagesetpixel () Draws a point on the image with a color in x, y coordinates (0,0 in the upper-left corner of the image).
?? imageline--Drawing a line segment
?? Syntax: Boolimageline (resource image, Intx1, Inty1, Intx2, Inty2, Intcolor)
?? Imageline () Draws a line segment from coordinates x1,y1 to X2,y2 (0, 0 in the upper left corner of the image) with a color color.

imagerectangle--Draw a rectangle

?? Syntax: Boolimagerectangle (resource image, Intx1, Inty1, Intx2, Inty2, Intcol)
?? Imagerectangle () uses the Col color to draw a rectangle in the image, whose upper-left coordinate is x1, y1, and the lower-right corner coordinates are x2, y2. The upper-left corner coordinates of the image are 0, 0.
?? imagefilledrectangle--Draw a rectangle and fill the
?? Syntax: Boolimagefilledrectangle (resource image, Intx1, Inty1, Intx2, Inty2, Intcolor)
?? Imagefilledrectangle () Draws a color-filled rectangle in the image, with the upper-left coordinate of x1,y1 and the lower-right coordinate of x2,y2. 0, 0 is the leftmost upper-left corner of the image

imagepolygon--Draw a polygon
?? Syntax: Boolimagepolygon (resource image, array points, intnum_points, Intcolor)
?? Imagepolygon () Creates a polygon in the image. Points is a PHP array that contains the coordinates of each vertex of the polygon, i.e. 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 populate
?? Syntax: Boolimagefilledpolygon (resource image, array points, intnum_points, Intcolor)
?? Imagefilledpolygon () Draws a filled polygon in the image. The points parameter is an array that contains 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--to draw an ellipse
?? Syntax: Boolimageellipse (resource image, INTCX, intcy, INTW, Inth, Intcolor)
?? Imageellipse () draws an ellipse with a center of Cx,cy (0, 0 in the upper-left corner of the image) in the image represented by image. 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 populate
?? Syntax: Boolimagefilledellipse (resource image, INTCX, intcy, INTW, Inth, Intcolor)
?? Imagefilledellipse () draws an ellipse at the center of the image represented by Cx,cy (0, 0 in the upper-left corner of the image). W and H Specify the width and height of the ellipse, respectively. The ellipse is filled with color. Returns true if successful, and false if it fails.

imagearc--Drawing Elliptical Arc
?? Boolimagearc (resource image, INTCX, intcy, INTW, Inth, INTs, inte, intcolor)
?? Imagearc () draws an elliptical arc in the image represented by 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 start and end points are specified with an angle of s and e parameters. 0° is at three o'clock ' clock and draws in a clockwise direction.
?? imagefilledarc--draw an elliptical arc and fill
?? Boolimagefilledarc (resource image, INTCX, intcy, INTW, Inth, INTs, inte, Intcolor, Intstyle)
?? Imagefilledarc () draws an elliptical arc in the image represented by the image as Cx,cy (0, 0) in the upper-left corner. Returns true if successful, and false if it fails. W and H Specify the width and height of the ellipse respectively, and the S and e parameters specify the starting and ending points with an angle. The style can be a bitwise or (or) of the following values: Img_arc_pie, Img_arc_chord, Img_arc_nofill, img_arc_edged. Where Img_arc_pie and Img_arc_chord are mutually exclusive, Img_arc_chord simply connects the starting and ending points with a straight line, and Img_arc_pie produces a circular boundary (if two are used, Img_arc_chord takes effect). Img_arc_nofill indicates that an arc or chord is only outlined, not filled. Img_arc_edged indicates that starting and ending points are connected to the center point with a straight line, and that using the Img_arc_nofill together is a good way of appeased contour (without padding)

6 drawing text in an image

imagestring--to draw a line of string horizontally

?? Syntax: boolimagestring (resource image, Intfont, Intx, Inty, string S, intcol)
?? Imagestring () uses the Col color to draw the string s to the x, y coordinates of the image represented by image (this is the upper-left coordinate of the string, and the upper-left corner of the entire image is 0,0). If the font is 1,2,3,4 or 5, the built-in font is used.
?? imagestringup--to draw a line of string
?? Syntax: Boolimagestringup (resource image, Intfont, Intx, Inty, string S, intcol)
?? Imagestring () uses the Col color to draw the string s vertically to the x, y coordinates of the image represented by the image (0, 0 in the upper-left corner). If the font is 1,2,3,4 or 5, the built-in font is used.

imagechar--to draw a character horizontally
?? Syntax: Boolimagechar (resource image, Intfont, Intx, Inty, string C, Intcolor)
?? Imagechar () draws the first character of the string C in the image specified by the images, with the upper-left corner at x, y (0, 0 in the upper-left corner of the image), and color. If the font is 1,2,3,4 or 5, the built-in font is used (larger numbers correspond to larger fonts).
?? imagecharup--to draw a character vertically
?? Syntax: Boolimagecharup (resource image, Intfont, Intx, Inty, string C, Intcolor)
?? Imagecharup () draws the character C vertically on the image specified by the images, at x, y (0, 0 in the upper-left corner of the image), and with color. If the font is 1,2,3,4 or 5, the built-in font is used.
?? imagettftext--writing text to an image with TrueType fonts
?? Syntax: Array imagettftext (resource image, float size, float angle, intx, Inty, Intcolor, String fontfile, string text)

Example:

 Php$im= Imagecreate ( Max, Max);//Create a 150*150 canvas$BG = Imagecolorallocate ($im,255,255,255);//set the background color of the canvas to white$black = Imagecolorallocate ($im,0,0,0);//set a color variable to black$string="Lampbrother";//declares a string that is used to output in an imageImagestring ($im,3, -, -, $string, $black);//to output a string $string to an image horizontallyImagestringup ($im,3, -, the, $string, $black);//vertical from bottom to top output $string to image for($i =0, $j =strlen ($string); $i
 
  <>
  string); $i + +, $j-- ) {// use loop single character output to image Imagechar ($im, 3, * ($i + 1), 
   
    10* ($i + 
    2), $ 
    string
    [$i], $black); 
    // 
    tilt down to output each character Imagecharup ($im, 
    3, * ($i + 
    1), 
    * ($j + 
     
    2
   ), $ string[$i], $black); // tilt up to output each character 
 }header ('content-type:image/png'); // 
 sets the output header identifier imagepng ($im); // output PNG format picture ?> 
  
 

The above describes the dynamic image processing-php (34), including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.