Home  >  Article  >  Backend Development  >  Examples of creating images and drawing text in PHP, _PHP tutorial

Examples of creating images and drawing text in PHP, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:48962browse

Examples of creating images and drawing text in PHP,

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:

Copy code The code is 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 a character horizontally
bool imagecharup(resource $image,int $font,int $x,int $y,char $c,int $color) //Draw a character vertically

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:

Copy code The code is as follows:

$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 output in the image

imagestring($im, 3, 28, 70, $string, $black); //Output the string horizontally into the image
imagestringup($im, 3, 59, 115, $string, $black); //Input vertically from bottom to top into 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 slant
imagecharup($im, 3, 10*($i+1),10*($j+2),$string[$i],$black); //Output each character in an upward tilt
}

header('Content-type:image/png');
imagepng($im);
?>

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:

Copy code 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, 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:

Copy code The code is as follows:

$im = imagecreatetruecolor(400, 30); //Create a canvas with a size of 400 30 pixels

$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);

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

//If there is Chinese output, it needs to be transcoded and converted into a UTF-8 string before it can be passed directly
$text = iconv("GB2312", "UTF-8", "Memory Classics");

//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, $grey, $font, $text); //Output a gray string as a shadow
imagettftext($im, 20, 0, 10, 20, $black, $font, $text); //Output a black string on the shadow

header("Content-type: image/png");
imagepng($im);

imagedestroy($im);

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914048.htmlTechArticleAn 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. PHP not only supports a large number of font libraries, but also provides very flexible...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn