Example of creating an image and drawing text in PHP

Source: Internet
Author: User
Tags font identifier
This article mainly introduces the example of creating an image and drawing text in PHP. the text displayed in the image also needs to be drawn according to the coordinate position. in PHP, it not only supports a large number of library fonts, it also provides a very flexible text rendering method. if you need a friend, you can refer to the text displayed in the image and draw it according to the coordinates. PHP not only supports a large number of font libraries, but also provides flexible text rendering methods. For example, you can draw scaled, skewed, and rotated text in the graph. You can use the font text used by functions such as imageString (), imageStringUP (), or imageChar () to draw the image. The prototype of these functions is as follows:

The code is as follows:


Bool imagestring (resource $ image, int $ font, int $ x, int $ y, string $ s, int $ color) // draw a row of strings horizontally.
Bool imagestringup (resource $ image, int $ font, int $ x, int $ y, string $ s, int $ color) // draw a line of string vertically
Bool imagechar (resource $ image, int $ font, int $ x, int $ y, char $ c, int $ color) // draw a character horizontally
Bool imagecharup (resource $ image, int $ font, int $ x, int $ y, char $ c, int $ color) // draw a character vertically

In the four functions listed above, the first two functions, imageString () and imageStringUP (), are used to output a line of string to both the watermark and vertical values in the image, and the last two functions, imageChar () and imageCharUP () it is used to output one character to the watermark and vertical output respectively. Although these four functions are different, the call method is similar. They all draw characters specified by the fifth parameter in the $ image, and their positions are all output starting from the coordinates ($ x, $ y. If a line of string is drawn horizontally, it is output from left to right, while a line of string is output from bottom to top. These functions can use the last parameter $ color to indicate the color of the text. The second parameter $ font provides the text font identifier. The value is an integer of 1, 2, 3, 4, or 5, and the built-in font is used, the larger the number, the larger the size of the output text. The following is an example of text output in an image:

The code is as follows:


<? Php
$ Im = imagecreate (150,150 );

$ Bg = imagecolorallocate ($ im, 255,255,255); // Set the canvas background to White
$ Black = imagecolorallocate ($ im, 0, 0, 0); // set a color variable to black.

$ String = "LAMPBrother"; // The character output in the image

Imagestring ($ im, 3, 28, 70, $ string, $ black); // horizontally output the string to the image
Imagestringup ($ im, 3, 59,115, $ string, $ black); // Vertical down to the image
For ($ I = 0, $ j = strlen ($ string); $ I Imagechar ($ im, 3, 10 * ($ I + 1), 10 * ($ j + 2), $ string [$ I], $ black ); // output each character in a downward skew
Imagecharup ($ im, 3, 10 * ($ I + 1), 10 * ($ j + 2), $ string [$ I], $ black ); // output each character in an upward skew.
}

Header ('content-type: image/png ');
Imagepng ($ im );
?>

You can also use the imageTtfText () function to output a device-independent TrueType font that can be scaled out of the built-in fonts by using the four functions described above. TrueType uses mathematical functions to describe the font contour. it can be used as a print font and screen display. TrueType is compatible with various operating systems. Since it is described by instructions on the font, it is irrelevant to the resolution, and always output according to the resolution of the printer. Whether zoomed in or out, the font is always smooth and there will be no sawtooth. For example, in Windows, the folder C: \ WINDOWS \ Fonts where the library is located is marked with TrueType Fonts, for example, simsun. ttf is the "" in the TrueType font ". The prototype of the imageTtfText () function is as follows:

The code is as follows:


Array imagettftext (resource $ image, float $ size, float $ angle, int $ x, int $ y, int $ color, string $ fontfile, string $ text)

This function requires multiple parameters, of which $ image requires an image resource. The $ size parameter is used to set the font size. the pixel size (GD1) or the dot size (GD2) should be specified based on the GD Library version ). The $ angle parameter indicates the angle. 0 ° indicates the text read from left to right. a higher value indicates the clockwise rotation. For example, 90 ° indicates the text read from the bottom up. The coordinates expressed by the two parameters ($ x, $ y) define the basic points of a character, which is probably the lower left corner of the character. This is different from the imagestring () function. its ($ x, $ y) coordinate defines the upper left corner of the first character. The $ color parameter specifies the color index. Using a negative color index value can disable anti-aliasing. See $ fontfile as the path of the TrueType font to be used. Depending on the GD library used by PHP, when fontfil does not start with "/", ". "ttf" will be added to the file name, and will try to search for the file name in the library-defined font path. The last parameter $ text specifies the text string to be output. it can contain a decimal digital character representation (in the format of: €) to access the character exceeding the position 127 in the font. A UTF-8-encoded string can be passed directly. If a character used in a string is not supported by a font, a hollow rectangle replaces the character.

The imagettftext () function returns an array containing eight cells, representing the four corners of the text box. The order is lower left, lower right, upper right, and upper left. These vertices are irrelevant to the angle of the text. Therefore, the "upper left corner" refers to the upper left corner of the text when the text is viewed in the water bottle direction. In the following example, we generate a PNG image with x 30 pixels in white, with a black (with a gray shadow) font written in "!" The code is as follows:

The code is as follows:


<? Php
$ Im = imagecreatetruecolor (400, 30); // Create a canvas of 400 30 pixels

$ White = imagecolorallocate ($ im, 255,255,255 );
$ Gray = imagecolorallocate ($ im, 128,128,128 );
$ Black = imagecolorallocate ($ im, 0, 0, 0 );

Imagefilledrectangle ($ im, 0, 0,399, 29, $ white); // outputs a rectangle filled with white as the background

// If there is a Chinese output, it needs to be transcoded, converted to a UTF-8 string can be passed directly
$ Text = iconv ("GB2312", "UTF-8", "recall classic ");

// Set the font and copy the font corresponding to simsun. ttc in the system to the current directory.
$ Font = 'simsun. ttc ';

Imagettftext ($ im, 20, 0, 12, 21, $ gray, $ font, $ text); // output a gray string as the shadow.
Imagettftext ($ im, 20, 0, 10, 20, $ black, $ font, $ text); // output a black string in the shadow.

Header ("Content-type: image/png ");
Imagepng ($ im );

Imagedestroy ($ im );

?>

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.