<?php $dir = 'D:\Serious\phpdev\test\images'; //设置环境变量 putenv('GDFONTPATH='.'C:\Windows\Fonts'); $font = "arial"; if(isset($_GET['id']) && ctype_digit($_GET['id']) && file_exists($dir.'https://www.jb51.net/'.$_GET['id'].'.jpg')) { $image = imagecreatefromjpeg($dir.'https://www.jb51.net/'.$_GET['id'].'.jpg'); } else { die('invalid image specified'); } $effect = (isset($_GET['e'])) ? $_GET['e'] : -1; switch($effect) { case IMG_FILTER_NEGATE: imagefilter($image , IMG_FILTER_NEGATE); break; case IMG_FILTER_GRAYSCALE: imagefilter($image , IMG_FILTER_GRAYSCALE); break; case IMG_FILTER_EMBOSS: imagefilter($image , IMG_FILTER_EMBOSS); break; case IMG_FILTER_GAUSSIAN_BLUR: imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR); break; } if(isset($_GET['capt'])) { //echo $_GET['capt']; imagettftext($image, 12, 0, 20, 20, 0, $font, $_GET['capt']); } if(isset($_GET['logo'])) { list($widht , $height) = getimagesize($dir.'https://www.jb51.net/'.$_GET['id'].'.jpg'); list($wmk_width , $wmk_height) = getimagesize('images/logo.png'); $x = ($widht-$wmk_width) / 2; $y = ($height-$wmk_height) / 2; $wmk = imagecreatefrompng('images/logo.png'); imagecopymerge($image , $wmk , $x , $y , 0 , 0 , $wmk_width , $wmk_height , 20); imagedestroy($wmk); } header('Content-Type:image/jpeg'); imagejpeg($image , '' , 100); ?>
最后上传的水印图片效果如下:
注意主要的逻辑就是通过 imagecopymerge() 方法把两个图片合并在一起造成水印效果。来看看这个方法的方法原型和参数:
bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int$src_x , int $src_y , int $src_w , int $src_h , int $pct )