To add watermarks to images in PHP, we need to install the GD library for PHP. Here we will not introduce the GD library installation, but only introduce how to use PHP to add text watermarks to images and add text shadow effects.
GD library is an extension library for PHP to process graphics. The GD library provides a series of APIs for processing pictures. You can use the GD library to process pictures or generate pictures. 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.
Generate renderings:
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.
The code is as follows
|
Copy code
|
||||
$str="Beijing";
$str2= "Air Quality: Mild Pollution"; // Generate an object from the image $im $im = imagecreatefromjpeg("images/3.jpg");
$fnt = "zt.ttf"; $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