Home >Backend Development >PHP Tutorial >Examples of creating images and drawing text in PHP, _PHP tutorial
The text displayed in the image also needs to be drawn according to the coordinate position. PHP not only supports a large number of font libraries, but also provides a very flexible text drawing method. For example, draw scaled, tilted, rotated text, etc. in the picture. Text in a font can be drawn into an image using functions such as imageString(), imageStringUP(), or imageChar(). The prototypes of these functions are as follows:
Among the four functions listed above, the first two functions imageString() and imageStringUP() are used to output a line of strings to the image horizontally and vertically respectively, and the last two functions imageChar() and imageCharUP() respectively Used to output a character horizontally and vertically into the image. Although these four functions are different, they are called in similar ways. They all draw the characters specified by the fifth parameter in the $image image, and the drawn positions are output starting from coordinates ($x, $y). If a line of string is drawn horizontally, the output is from left to right, while if a line of string is drawn vertically, the output is from bottom to top. These functions can give the color of the text through the last parameter $color. The second parameter $font gives the text font identifier. Its value is an integer 1, 2, 3, 4 or 5. The built-in font is used. The larger the number, the larger the output text size. Here is an example of outputting text in an image:
In addition to outputting the built-in fonts through the four functions introduced above, you can also use the imageTtfText() function to output a scalable device-independent TrueType font. TrueType uses mathematical functions to describe the outline shape of fonts. It can be used as a printing font or as a screen display. Various operating systems are compatible with this font. Since it describes the glyphs by instructions, it has nothing to do with resolution, and the output is always based on the resolution of the printer. No matter whether you zoom in or out, the font is always smooth and there will be no jagged edges. For example, in the Windows system, the folder C:WINDOWSFonts where the font library is located has labels for TrueType fonts. For example, simsun.ttf is "Songti" in the TrueType font. The prototype of the imageTtfText() function is as follows:
This function requires multiple parameters, among which the parameter $image needs to provide an image resource. The parameter $size is used to set the font size. Depending on the GD library version, it should be specified in pixel size (GD1) or point size (GD2). The parameter $angle is the angle expressed in degrees, 0º is text read from left to right, and higher values represent counterclockwise rotation. For example, 90º represents text that reads from bottom to top. The coordinates represented by the two parameters ($x, $y) define the basic point of a character, which is probably the lower left corner of the character. This is different from the imagestring() function, whose ($x, $y) coordinates define the upper left corner of the first character. The parameter $color specifies the color index. Using negative color index values has the effect of turning off anti-aliasing. See $fontfile is the path to the TrueType font you want to use. Depending on the GD library used by PHP, when fontfil does not start with "/", ".ttf" will be added to the end of the file name, and an attempt will be made to search for the file name in the library definition font path. The last parameter $text specifies the text string to be output, which can contain decimal digitized character representation (in the form: €) to access characters beyond position 127 in the font. UTF-8 encoded strings can be passed directly. If a character used in a string is not supported by the font, a hollow rectangle will replace the character.
The imagettftext() function returns an array containing 8 cells, representing the four corners of the text frame, in order of lower left corner, lower right corner, upper right corner, and upper left corner. These points are relative to the text and have nothing to do with the angle, so the "upper left corner" refers to the upper left corner of the text when looking at the water bottle orientation. We use the script in the following example to generate a white 400X30 pixel PNG image, in which there is "Remember the Classic!" written in black (with gray shadow) "Arial" font. The code is as follows: