


Examples of creating images and drawing text in PHP, _PHP tutorial
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:
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:
$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
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:
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:
$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);
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
