//嵌入水印程序, 文字水印和图片水印请分别添加!!!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"); ?> <img src="temp.jpg" alt="PHP添加水印后,图像变全黑,请高手指教!" > <? */ if($srcim==false) echo "打开失败!"; break; case 3: $srcim =@imagecreatefrompng($imgSrc); if($srcim==false) echo "打开失败!"; break; default: die("不支持的图片文件类型"); exit; } if(!strcmp($markType,"img")) { //print_r("</br> 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("</br> 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("</br> hello ! text"); //$fontSize = 86; //middle if(!empty($markText)) { //print_r($fontType); if(!file_exists($fontType)) { //print_r("</br> hello ! no fonttype"); return; } } else { //print_r("</br> hello ! no marktext"); return; } //print_r("</br> hello ! i am before imagettfbbox"); $box = imagettfbbox($fontSize, 0, $fontType,$markText); //print_r("</br> 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("</br> hello ! I AM AT 5"); $x = ($srcImg_w - $logow) / 2; $y = ($srcImg_h - $logoh) / 2; break; case 6: // print_r("</br> 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("</br> Hi, i am at markpos 9 "); $x = $srcImg_w - $logow - 5; $y = $srcImg_h - $logoh -5; break; default: die("此位置不支持"); exit; } /* print_r("</br> 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("</br> hello i am after imagecreatetruecolor!!"); */ imagecopy ( $dst_img, $srcim, 0, 0, 0, 0, $srcImg_w, $srcImg_h); //print_r("</br> hello!"); if(!strcmp($markType,"img")) { imagecopy($dst_img, $markim, $x, $y, 0, 0, $logow, $logoh); imagedestroy($markim); } if(!strcmp($markType,"text")) { //print_r("</br> 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;}
该问题已解决!