Examples of creating images and drawing text in PHP,
The text displayed in the image also needs to be drawn at the coordinate location. Not only does it support more font libraries in PHP, but it also provides a very flexible way to draw text. For example, draw scaled, italic, rotated text in the diagram, and so on. You can use the font text used by functions such as imagestring (), Imagestringup (), or Imagechar () to draw into the image. The prototypes for these functions are as follows:
Copy the Code code as follows:
BOOL Imagestring (resource $image, int $font, int $x, int $y, string $s, int $color)//Draw a line of string 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 one character horizontally
BOOL Imagecharup (resource $image, int $font, int $x, int $y, char $c, int $color)//vertically draw one character
In the four functions listed above, the first two functions imagestring () and Imagestringup () are used to output a line of string horizontally and vertically to the image, and then two functions Imagechar () and Imagecharup () Used to output one character horizontally and vertically to the image. Although these four functions differ, they are called in a similar manner. They all draw the characters specified by the fifth parameter in the $image image, and the plotted position is output from the coordinates ($x, $y). If a line is drawn horizontally, the string is left-to-right, and the vertical line of the string is the bottom output. These functions can be given the color of the text by the last parameter $color. The second parameter, $font, gives the literal font identifier, whose value is an integer 1, 2, 3, 4, or 5, using the built-in font, and the larger the number, the larger the output text size. The following is an example of outputting text in an image:
Copy the Code code as follows:
<?php
$im = Imagecreate (150, 150);
$BG = Imagecolorallocate ($im, 255, 255, 255); Set the background of the canvas to white
$black = imagecolorallocate ($im, 0, 0, 0); Set a color variable to black
$string = "Lampbrother"; Characters that are output in an image
Imagestring ($im, 3, $string, $black); Horizontally outputting a string to an image
Imagestringup ($im, 3, $string, $black); Vertically from bottom to image
For ($i =0, $j =strlen ($string); $i
Imagechar ($im, 3, 10* ($i + 1), 10* ($j +2), $string [$i], $black); Tilt down to output each character
Imagecharup ($im, 3, 10* ($i + 1), 10* ($j +2), $string [$i], $black); Tilt up to output each character
}
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, using the four functions described above to output a built-in font. TrueType is a mathematical function to describe the shape of the font outline, which can be used as a print font, but also as a screen display, various operating systems can be compatible with this font. Since it is described by a directive for glyphs, it is resolution-independent, and output always follows the printer's resolution. Whether zoomed in or out, the font is always smooth and does not appear jagged. For example, in a WINDOWS system, the font library is located under the folder C:\WINDOWS\Fonts, and there are labels for TrueType fonts, such as "Arial" in TrueType fonts, Simsun.ttf. The prototype of the Imagettftext () function is as follows:
Copy the Code code as follows:
Array Imagettftext (Resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, String $text)
The function requires multiple parameters, where the parameter $image needs to provide an image resource. Parameter $size is used to set the font size, depending on the GD library version, you should specify (GD1) or point size (GD2) in pixel size. The parameter $angle is the angle of the angle representation, 0º the text read from left to right, and a higher value indicates counterclockwise rotation. For example, 90º represents text that reads from the bottom up. And by the coordinates represented by ($x, $y) two parameters, the basic point of a character is defined, presumably the lower-left corner of the character. This differs from the imagestring () function, whose ($x, $y) coordinates define the upper-left corner of the first character. The $color parameter specifies the color index. Using a negative color index value 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 "/" then ". TTF" will be added to the file name, and will attempt to search for the filename in the library definition font path. The last parameter, $text, specifies the text string that needs to be output, and can contain decimal digitized character representations (in the form: €) to access characters that exceed 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 replaces the character.
The Imagettftext () function returns an array of 8 cells, representing the four corners of the text frame, in the lower-left corner, lower-right corner, upper-right corner, and upper-left corner. These points are relative to the text and are independent of the angle, so the upper left corner refers to the upper-left corner of the text when you look at it in the direction of the water bottle. We use the script in the following example to generate a white 400x30 pixel png picture, which has black (with gray shading) "Arial" font written "Memory classic!" The code is as follows:
Copy the Code code as follows:
<?php
$im = Imagecreatetruecolor (400, 30); Create a 400 30 pixel-sized canvas
$white = Imagecolorallocate ($im, 255, 255, 255);
$grey = Imagecolorallocate ($im, 128, 128, 128);
$black = imagecolorallocate ($im, 0, 0, 0);
Imagefilledrectangle ($im, 0, 0, 399, $white); Output a rectangle with white fill as the background
If there is a Chinese output, it needs to be transcoded and converted to a UTF-8 string to be passed directly
$text = Iconv ("GB2312", "UTF-8", "Memory Classics");
Set the font to copy the font corresponding to the SIMSUN.TTC in the system to the current directory
$font = ' SIMSUN.TTC ';
Imagettftext ($im, 0, $grey, $font, $text); Output a gray string as a shadow
Imagettftext ($im, 0, $black, $font, $text); Output a black string on the shadow
Header ("Content-type:image/png");
Imagepng ($im);
Imagedestroy ($im);
?>
http://www.bkjia.com/PHPjc/914048.html www.bkjia.com true http://www.bkjia.com/PHPjc/914048.html techarticle In PHP, an example of creating an image and drawing text, the text displayed in the image needs to be drawn at the coordinates. In PHP not only to support the more font library, but also provides a very flexible ...