PHPCookbook Reading Notes & ndash; chapter 2 graphics

Source: Internet
Author: User
Tags transparent color
Overview: Drawing Images in PHP is mostly implemented using the GD library. The function of GD varies greatly depending on the GD version you are using and the options enabled during configuration. Commonly used GD function ImageCreate (): Creates a color palette-based image ImageColorAllocate (): allocates color to a pair of images. The odd thing is: the first call

Overview: Drawing Images in PHP is mostly implemented using the GD library. The function of GD varies greatly depending on the GD version you are using and the options enabled during configuration. Commonly used GD function ImageCreate (): Creates a color palette-based image ImageColorAllocate (): allocates color to a pair of images. The odd thing is: the first call

Overview

Draw in PHPGraphicsThe GD library is used for implementation. The function of GD varies greatly depending on the GD version you use and the options enabled during configuration.

Common GD Functions

ImageCreate (): Creates a palette-based image.
ImageColorAllocate (): assign color to a pair of images. The odd thing is that the background color of the image is allocated during the first call, and the subsequent call is used to allocate lines,GraphicsAnd so on
ImagePNG (): Convert GDGraphicsThe stream is output to the standard output (usually a browser) in PNG format, and can be output to the file through optional parameters.
ImageCreateFromPNG (): Creates a PNG image from a local PNG file or URL.
ImageCreateFromJPEG (): Creates a JPG image from a local JPG file or URL (the URL creates the allow_url_fopen that requires php. ini and sets it to true)
ImageLine (): Draw line
ImageFilledRectangle (): draws a rectangle and fills the color.

ImageRectangle (): draws a rectangle.
ImagePolygon (): Draw a polygon
ImageFilledPolygon (): Draw a polygon
ImageArc (): draws an elliptical arc.
ImageFillToBorder (): Fill the area to the border (if there is no border color, the entire image will be filled)
ImageSetStyle (): sets the underlined style. For details, refer to another blog article (interesting imagesetstyle In the GD library)
ImageString (): horizontal string (character size is supported, and font cannot be set. The coordinate base point is the top left)
ImageTTFText (): Use the TrueType font to write text to the image (The. ttf font file is installed on the server, and the coordinate base point is left bottom)
ImagePSText (): Draw a text string in the PostScript Type1 font on the image (the coordinates of the string are on the right)
ImageStringUp (): Draw a vertical string (90 degrees counterclockwise)
Imagecreatetruecolor (): creates a real-color black background image of a specified size.
ImageColorTransparent (): defines Mo color as transparent color.
ImageColorsForIndex (): gets the associated array of the key names of red, green, blue, and alpha at a certain point in the image.
ImageSX (): Get the x-axis length of the image
ImageSY (): obtains the Y axis length of the image.
ImagePSBBox (): obtains the upper-left and lower-right coordinates of the PostScript Type1 string.
ImageTTFBBox (): obtains the definition of the base point of the Four Coordinate libraries of the TureType string at the bottom left, bottom right, top right, and top left.

Built-in character function base point: Top left

TureType font base point: Bottom left

PostScript Type1 base point: Bottom Right

Create centered text

PHP built-in Fonts

Function pc_ImageStringCenter ($ image, $ text, $ font) {// width and height of the font size $ width = array (1 => 5, 6, 7, 8, 9 ); $ height = array (1 => 6, 8, 13, 15, 15); // get the width and height of the image $ xi = ImageSX ($ image ); $ yi = ImageSY ($ image); // get the length and height of the text $ xr = $ width [$ font] * strlen ($ text ); $ yr = $ height [$ font]; // calculates the center point $ x = intval ($ xi-$ xr)/2 ); $ y = intval ($ yi-$ yr)/2); return array ($ x, $ y );}


For PostScript Fonts

Function pc_ImagePSCenter ($ image, $ text, $ font, $ size, $ space = 0, $ tightness = 0, $ angle = 0) {// obtain the length and width of an image $ xi = ImageSX ($ image); $ yi = ImageSY ($ image); // obtain the coordinate list ($ xl, $ yl, $ xr, $ yr) = ImagePSBBox ($ text, $ font, $ size, $ space, $ tightness, $ angle ); // calculate the center point $ x = intval ($ xi-$ xr)/2); $ y = intval ($ yi + $ yr)/2 ); return array ($ x, $ y );}


For TrueType fonts

Function pc_ImageTTFCenter ($ image, $ text, $ font, $ size) {// calculate the image length and width $ xi = ImageSX ($ image); $ yi = ImageSY ($ image ); // calculate the coordinates of the left and right vertices of the string. $ box = ImageTTFBBox ($ size, $ angle, $ font, $ text ); $ xr = abs (max ($ box [2], $ box [4]); // position X in the upper right and lower right corner $ yr = abs (max ($ box [5], $ box [7]); // Y position in the upper right and lower right corner // calculation center $ x = intval ($ xi-$ xr)/2); $ y = intval ($ yi + $ yr) /2); return array ($ x, $ y );}
Draw Graphics

Common GD functions are used in this example.

 $ X, 'y' => $ Y);} imageline ($ image, $ point [0] ['X'], $ point [0] ['y'], $ point [1] ['X'], $ point [1] ['y'], $ color); imageline ($ image, $ point [1] ['X'], $ point [1] ['y'], $ point [2] ['X'], $ point [2] ['y'], $ color); imageline ($ image, $ point [2] ['X'], $ point [2] ['y'], $ point [3] ['X'], $ point [3] ['y'], $ color ); imageline ($ image, $ point [3] ['X'], $ point [3] ['y'], $ point [4] ['X'], $ point [4] ['y'], $ color); imageline ($ image, $ point [4] ['X'], $ point [ 4] ['y'], $ point [0] ['X'], $ point [0] ['y'], $ color ); // imageline ($ image, $ point [0] ['X'], $ point [0] ['y'], $ point [2] ['X'], $ point [2] ['y'], $ color); imageline ($ image, $ point [1] ['X'], $ point [1] ['y'], $ point [3] ['X'], $ point [3] ['y'], $ color); imageline ($ image, $ point [2] ['X'], $ point [2] ['y'], $ point [4] ['X'], $ point [4] ['y'], $ color ); imageline ($ image, $ point [3] ['X'], $ point [3] ['y'], $ point [0] ['X'], $ point [0] ['y'], $ color ); Imageline ($ image, $ point [4] ['X'], $ point [4] ['y'], $ point [1] ['X'], $ point [1] ['y'], $ color); header ('content-type: image/png '); imagepng ($ image);?>
Read EXIF data

You can use PHP built-in functions to obtain exif data.

 


The obtained data is:

Array(    [FileName] => beth-and-seth.jpg    [FileDateTime] => 1096055414    [FileSize] => 182080    [FileType] => 2    [MimeType] => image/jpeg    [psFound] => APP12    [COMPUTED] => Array        (            [html] => width="642" height="855"            [Height] => 855            [Width] => 642            [IsColor] => 1        )    [Company] => Ducky    [Info] =>)

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.