Home > Article > Backend Development > Use GD library to generate images with shadow text_PHP tutorial
This article mainly introduces the method of using the GD library to generate images with shadow text. It is very detailed and recommended to everyone. Friends in need can refer to it
I recently used the GD library to generate images for WeChat public accounts. I studied the generation of text shadow effects in the GD library and also discovered the power of the GD library.
The GD library is an extension library for PHP to process graphics. The GD library provides a series of APIs for processing images. You can use the GD library to process images or generate images. On websites, the GD library is usually used to generate thumbnails, add watermarks to images, generate Chinese character verification codes, or generate reports on website data.
The installation of the GD library is available online, and many virtual spaces now support it, so I won’t go into details here. Below, I will introduce how to use the GD library through examples of my actual application code and related comments.
Original picture:
Generate renderings:
The code is as follows:
?
2 3 4 5 6 7 8 9 10 11
12
13
21 22
|
$str="Beijing"; $str2= "Air Quality: Mild Pollution"; // Generate an object $im from the image $im = imagecreatefromjpeg("images/3.jpg"); //Load font zt.ttf $fnt = "zt.ttf"; //Create colors, white for text font and black for shadow $white=imagecolorallocate($im,222,229,207); $black=imagecolorallocate($im,50,50,50); //Create a function about relative image position for easy calling $top=100; $left=60; $top2=170; //Add text to the image, imagettftext (image, size, angle, x, y, color, fontfile, text) imagettftext($im,41, 0, $left 1, $top 1, $black, $fnt, $str); imagettftext($im,41, 0, $left, $top, $white, $fnt, $str); imagettftext($im,43, 0, $left 1,$top2 1 , $black, $fnt, $str2); imagettftext($im,43, 0, $left,$top2, $white, $fnt, $str2); //Output $im ImageJpeg($im); //Destroy the $im object ImageDestroy($im); |