Examples of creating images and drawing text in PHP _php tips

Source: Internet
Author: User
Tags font identifier

The text displayed in the image also needs to be painted in a coordinate position. In PHP not only support a lot of font library, but also provides a very flexible method of text rendering. For example, draw scaled, italic, rotated text, and so on in the diagram. 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 Code code as follows:

BOOL Imagestring (resource $image, int $font, int $x, int $y, string $s, int $color)//horizontally draw a line of string
BOOL Imagestringup (resource $image, int $font, int $x, int $y, string $s, int $color)//Vertical line of string
BOOL Imagechar (resource $image, int $font, int $x, int $y, char $c, int $color)//horizontal Draw a character
BOOL Imagecharup (resource $image, int $font, int $x, int $y, char $c, int $color)//To draw a character vertically

Of the four functions listed above, the first two functions imagestring () and Imagestringup () are used to output a single line of string to the image horizontally and vertically, and then two functions Imagechar () and Imagecharup () Used to output one character horizontally and vertically in the image, respectively. Although these four functions are different, they are called in a similar way. They all draw the characters specified by the fifth argument in the $image image, and the positions are drawn from the coordinates ($x, $y) to start the output. If it is horizontal, a line of string is output from left to right, while a vertical line of string is bottom-up output. These functions can be given the color of the text by $color the last argument. The second parameter, $font, gives the literal font identifier, whose value is integers 1, 2, 3, 4, or 5, using the built-in font, and the larger the number, the larger the text size. The following is an example of outputting text in an image:

Copy 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 printed in the image

Imagestring ($im, 3, $string, $black); To output the string to the image horizontally
Imagestringup ($im, 3,, $string, $black); Vertically from bottom to image
For ($i =0, $j =strlen ($string), $i <strlen ($string), $i + +, $j-) {//loop single character output to image
Imagechar ($im, 3, 10* ($i + 1), 10* ($j +2), $string [$i], $black); Tilt the output down to each character
Imagecharup ($im, 3, 10* ($i + 1), 10* ($j +2), $string [$i], $black); Tilt up output per 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 by outputting the built-in fonts with the four functions described above. A TrueType is a mathematical function that describes the shape of a font outline, which can be used as a print font, and can be used as a screen display, which can be compatible with all operating systems. Because it is described by an instruction to a glyph, it is independent of the resolution and always outputs according to the printer's resolution. Regardless of zooming in or out, the font is always smooth and will not appear jagged. For example, in WINDOWS systems, the folder where the font library is located is C:\WINDOWS\Fonts, with a label for TrueType fonts, such as simsun.ttf as "Arial" in a TrueType font. The prototype of the Imagettftext () function looks like this:

Copy 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 and, depending on the version of the GD library, the pixel size (GD1) or point size (GD2) should be specified. The argument $angle is the angle representation, 0º is read from left to right, and a higher value indicates a counter-clockwise rotation. For example, 90º represents text that is read from the bottom up. And by the coordinates represented by ($x, $y) two parameters, the basic point of a character is defined, probably 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. Parameter $color 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, ". TTF" will be added to the file name when Fontfil does not begin with "/" and will attempt to search for the file name in the library definition font path. The last parameter $text specifies the text string that needs to be output, and can contain a decimal digitized character representation (in the form of €) to access characters that exceed position 127 in the font. UTF-8 encoded strings can be passed directly. If one of the characters used in the string is not supported by the font, a hollow rectangle replaces the character.

The Imagettftext () function returns an array of 8 cells that represents the four corners of the text's outer frame, in the lower-left corner, the lower-right corner, the upper-right corner, and the upper-left corner. These points are independent of the angle relative to the text, so "upper left corner" refers to the upper-left corner of the text 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 a black (shaded) "XXFarEastFont-Arial" font written in "Recall classics!" "The code looks like this:

Copy Code code as follows:

<?php
$im = Imagecreatetruecolor (400, 30); Create a canvas of 400 30 pixel size

$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 filled with white as the background

If you have a Chinese output, you need to convert the code to a UTF-8 string before you can pass it directly
$text = Iconv ("GB2312", "UTF-8", "Recollection classics");

Sets the font to copy the fonts 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);

?>

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.