Home > Backend Development > PHP Tutorial > PHP adds text watermark code to images_PHP tutorial

PHP adds text watermark code to images_PHP tutorial

WBOY
Release: 2016-07-13 10:45:01
Original
761 people have browsed it

Adding watermarks to pictures is something that many websites do. The code below only supports adding text watermarks to pictures. It only supports English for now. If you want this program to support Chinese characters, just put $font= 'verdana.ttf'; It's OK to call a Chinese font.

php tutorial to add text watermark code to images
/*
Adding watermarks to pictures is something that many websites do. The following code only supports adding text watermarks to pictures. Currently, it only supports English. If you want this program to support Chinese characters, just $font='verdana. ttf';Call a Chinese font and it will be ok.
*/

$image=$_get[id];
$im=getimagesize($image);
//print_r($im);
//The following is the width and height of the read text
$font='verdana.ttf';//Font
$font_size = $im[0]/20;//Text size
$size=imagettfbbox($font_size,0,$font,'bKjia.c0m');//Text width and height
$textwidth = $size [4];//Get the width
$textheight = abs( $size [7] );//Get the height

$pic_w=abs(($im[0]-$textwidth)/2);//The position where the text is stored width
$pic_h=abs(($im[1]-$textheight)/2);//The high position where text is stored

switch($im[2])                                            {
case 1:
$im=imagecreatefromgif($image);
break;

case 2:

$im=imagecreatefromjpeg($image);
break;

case 3:

$im=imagecreatefrompng($image);
break;
}
//$logo="2.png";//Trademark image
//$ing=getimagesize($logo);
//
// switch($ing[2])                                                    //
//{
//case 1:
//$in=imagecreatefromgif($logo);
//break;
//
//case 2:
//$in=imagecreatefromjpeg($logo);
//break;
//
//case 3:
//$in=imagecreatefrompng($logo);
//break;
//}
//imagecopy($im,$in,200,3,0,0,'120','110');//Image merge
$te = imagecolorclosestalpha($im,255,255,255,60);//The last value is the greater the transparency, the more transparent it is
$str=iconv("gbk","utf-8","bKjia.c0m"); //Set the text content and encoding, it's very simple

imagettftext($im,$font_size,0,$pic_w,$pic_h,$te,'verdana.ttf',$str);

header("content-type: image/jpeg");

imagejpeg($im);


?>


http://www.bkjia.com/PHPjc/633046.html

truehttp: //www.bkjia.com/PHPjc/633046.htmlTechArticleAdding watermarks to images is something that many websites do. The following code only supports adding text watermarks to images. Oh, it only supports English for now. If you want this program to support Chinese...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template