//嵌入水印程序, 文字水印和图片水印请分别添加!!!function setwater($imgsrc,$markimg,$marktext,$textcolor,$markpos,$fonttype,$marktype,$fontsize){ /*参数说明:$imgsrc:目标图片,可带相对目录地址,$markimg:水印图片,可带相对目录地址,支持png和gif两种格式,如水印图片在执行文件mark目录下,可写成:mark/mark.gif$marktext:给图片添加的水印文字$textcolor:水印文字的字体颜色$markpos:图片水印添加的位置,取值范围:0~90:随机位置,在1~8之间随机选取一个位置1:顶部居左 2:顶部居中 3:顶部居右 4:左边居中5:图片中心 6:右边居中 7:底部居左 8:底部居中 9:底部居右$fonttype:具体的字体库,可带相对目录地址$marktype:图片添加水印的方式,img代表以图片方式,text代表以文字方式添加水印*/ $srcinfo = @getimagesize($imgsrc); $srcimg_w = $srcinfo[0]; $srcimg_h = $srcinfo[1]; switch ($srcinfo[2]) { case 1: $srcim =@imagecreatefromgif($imgsrc); if($srcim==false) echo 打开失败!; break; case 2: //header(content-type: image/jpeg); $srcim =@imagecreatefromjpeg($imgsrc); //print_r(hi, i am jpg file); //imagejpeg($srcim); /*imagejpeg($srcim,temp.jpg); ?> */ if($srcim==false) echo 打开失败!; break; case 3: $srcim =@imagecreatefrompng($imgsrc); if($srcim==false) echo 打开失败!; break; default: die(不支持的图片文件类型); exit; } if(!strcmp($marktype,img)) { //print_r( hello ! img); if(!file_exists($markimg) || empty($markimg)) { return; } $markimginfo = @getimagesize($markimg); $markimg_w = $markimginfo[0]; $markimg_h = $markimginfo[1]; if($srcimg_w < $markimg_w || $srcimg_h < $markimg_h) { return; } //print_r( the markimginfo is); //print_r($markimginfo[2]); switch ($markimginfo[2]) { case 1: $markim =@imagecreatefromgif($markimg); if($markim==false) echo 打开失败!; break; case 2: $markim =@imagecreatefromjpeg($markimg); if($markim==false) echo 打开失败!; break; case 3: $markim =@imagecreatefrompng($markimg); if($markim==false) echo 打开失败!; break; default: die(不支持的水印图片文件类型); exit; } $logow = $markimg_w; $logoh = $markimg_h; } if(!strcmp($marktype,text)) { //print_r( hello ! text); //$fontsize = 86; //middle if(!empty($marktext)) { //print_r($fonttype); if(!file_exists($fonttype)) { //print_r( hello ! no fonttype); return; } } else { //print_r( hello ! no marktext); return; } //print_r( hello ! i am before imagettfbbox); $box = imagettfbbox($fontsize, 0, $fonttype,$marktext); //print_r( hello ! i am after imagettfbbox); $logow = max($box[2], $box[4]) - min($box[0], $box[6]); $logoh = max($box[1], $box[3]) - min($box[5], $box[7]); } if($markpos == 0) { $markpos = rand(1, 9); } switch($markpos) { case 1: $x = +25; $y = +125; break; case 2: $x = ($srcimg_w - $logow) / 2; $y = +5; break; case 3: $x = $srcimg_w - $logow - 135; $y = +115; break; case 4: $x = +5; $y = ($srcimg_h - $logoh) / 2; break; case 5: // print_r( hello ! i am at 5); $x = ($srcimg_w - $logow) / 2; $y = ($srcimg_h - $logoh) / 2; break; case 6: // print_r( hello ! i am at 6); $x = $srcimg_w - $logow - 5; $y = ($srcimg_h - $logoh) / 2; break; case 7: $x = +25; $y = $srcimg_h - $logoh - 115; break; case 8: $x = ($srcimg_w - $logow) / 2; $y = $srcimg_h - $logoh - 5; break; case 9: //print_r( hi, i am at markpos 9 ); $x = $srcimg_w - $logow - 5; $y = $srcimg_h - $logoh -5; break; default: die(此位置不支持); exit; } /* print_r( hello i am before imagecreatetruecolor!); print_r($srcimg_w); print_r($srcimg_h);*/ $dst_img=@imagecreatetruecolor($srcimg_w, $srcimg_h); //建立空白背景,真彩色图片 //$color = imagecolorallocate($img,200,200,200); //分配一个灰色 //imagefill($img,0,0,$color); // 从左上角开始填充灰色 $trans_colour = imagecolorallocatealpha($dst_img, 0, 0, 0, 127); //透明图片 imagefill($dst_img, 0, 0, $trans_colour); //$dst_img=imagecreate($srcimg_w, $srcimg_h); /*print_r($dst_img); print_r( hello i am after imagecreatetruecolor!!); */ imagecopy ( $dst_img, $srcim, 0, 0, 0, 0, $srcimg_w, $srcimg_h); //print_r( hello!); if(!strcmp($marktype,img)) { imagecopy($dst_img, $markim, $x, $y, 0, 0, $logow, $logoh); imagedestroy($markim); } if(!strcmp($marktype,text)) { //print_r( hello ! text2); $rgb = explode(',', $textcolor); $color = imagecolorallocate($dst_img, $rgb[0], $rgb[1], $rgb[2]); imagettftext($dst_img, $fontsize, 0, $x, $y, $color, $fonttype,$marktext); } switch ($srcinfo[2]) { case 1: imagegif($dst_img, $imgsrc); break; case 2: //print_r(hello!); imagejpeg($dst_img, $imgsrc,97); //bool imagejpeg ( resource image [, string filename [, int quality]] ) quality 是imagejpeg独有的参数(0~100),默认是75,0是最差,文件最小,100是最佳,文件最大。 break; case 3: imagepng($dst_img, $imgsrc); break; default: die(不支持的水印图片文件类型); exit; } /* $marked_img=; switch ($srcinfo[2]) { case 1: imagegif($dst_img, $marked_img); break; case 2: //print_r(hello!); imagejpeg($dst_img, $marked_img); break; case 3: imagepng($dst_img, $marked_img); break; default: die(不支持的水印图片文件类型); exit; }*/ imagedestroy($dst_img); imagedestroy($srcim); //return $marked_img;}
回复讨论(解决方案) 该问题已解决!