天天看點

9-3. PHP圖像水印

文字水印 

1)添加字元串

$colorString=imagecolorallocate($img,255,255,255);
imagettftext($img,20,0,200,100,$colorString,'font/Elements.ttf','ROBIN.FANG');
           
9-3. PHP圖像水印

2)擷取圖檔長度和寬度,以及字元串水印所占的長度和寬度

$width=imagesx($img);
$height=imagesy($img);

$position=imagettfbbox(20,0,'font/Elements.ttf','ROBIN.FANG');
$stringWidth=$position[2]-$position[0];
           

3)将文字水印移至圖檔右下角

代碼彙總:

header('Content-type:image/jpeg');
$img=imagecreatefromjpeg('images/mina.jpg');

$width=imagesx($img);
$height=imagesy($img);

$position=imagettfbbox(20,0,'font/Elements.ttf','ROBIN.FANG');
$stringWidth=$position[2]-$position[0];

$colorString=imagecolorallocate($img,255,255,255);
imagettftext($img,20,0,$width-1-$stringWidth-($width/30),$height-1-($height/30),$colorString,'font/Elements.ttf','ROBIN.FANG');

imagejpeg($img);
imagedestroy($img);
           
9-3. PHP圖像水印

圖檔水印

header('Content-type:image/jpeg');
$img=imagecreatefromjpeg('images/mina.jpg');
$waterMark=imagecreatefrompng('images/watermark.png');

$width=imagesx($img);
$height=imagesy($img);

$waterMarkWidth=imagesx($waterMark);
$waterMarkHeight=imagesy($waterMark);

imagecopy($img,$waterMark,100,100,0,0,$waterMarkWidth,$waterMarkHeight);

imagejpeg($img);
imagedestroy($img);
           
9-3. PHP圖像水印