


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);
?>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools