| <?php /** * [创建图片文字水印] * @param [string] $imagename [需要添加水印的值] * @param [string] $string [图片上添加的文字] * @param [string] $locate [水印位置,center,left_buttom,right_buttom三选一] * @return [null] [description] */ function create_words_watermark($imagename,$string,$locate){ list($width,$height,$type)=getimagesize($imagename); $types=array(1 => "GIF",2 => "JPEG",3 => "PNG", 4 => "SWF",5 => "PSD",6 => "BMP", 7 => "TIFF",8 => "TIFF",9 => "JPC", 10 => "JP2",11 => "JPX",12 => "JB2", 13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM"); $type=strtolower($types[$type]); $create="imagecreatefrom".$type; $img=$create($imagename);//imagecreatefromjpeg() 成功后返回图象资源,失败后返回 FALSE $string_color=imagecolorallocate($img,200, 200, 200); $fontsize=4; // 图片的宽和高也可用下面两个函数获得 // $width=imagesx($img); // $height=imagesy($img); switch($locate){ case 'center': $x=($width-imagefontwidth($fontsize)*strlen($string))/2; $y=($height-imagefontheight($fontsize))/2; break; case 'left_buttom': $x=5; $y=($height-imagefontheight($fontsize)-3); break; case 'right_buttom': $x=($width-imagefontwidth($fontsize)*strlen($string)-3); $y=($height-imagefontheight($fontsize)-3); break; default: die("未指定水印位置!"); break; } imagestring($img,$fontsize,$x,$y,$string,$string_color); imagestring($img,$fontsize,$x+1,$y+1,$string,$string_color); //imagegif()、imagejpeg()、imagepng() 和 imagewbmp() 函数分别允许以 GIF、JPEG、PNG 和 WBMP 格式将图像输出到浏览器或文件 $save="image".$type; //保存 //$save($img,"new_".$imagename); //显示 header("content-type:image/".$type); $save($img); imagedestroy($img); //$img资源使用结束,销毁 } // create_words_watermark("test.png","hello world","right_buttom"); /** * [create_pic_watermark 添加图片水印] * @param [string] $dest_image [需要添加图片水印的图片名] * @param [string] $watermark [水印图片名] * @param [string] $locate [水印位置,center,left_buttom,right_buttom三选一] * @return [type] [description] */ function create_pic_watermark($dest_image,$watermark,$locate){ list($dwidth,$dheight,$dtype)=getimagesize($dest_image); list($wwidth,$wheight,$wtype)=getimagesize($watermark); $types=array(1 => "GIF",2 => "JPEG",3 => "PNG", 4 => "SWF",5 => "PSD",6 => "BMP", 7 => "TIFF",8 => "TIFF",9 => "JPC", 10 => "JP2",11 => "JPX",12 => "JB2", 13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM"); $dtype=strtolower($types[$dtype]);//原图类型 $wtype=strtolower($types[$wtype]);//水印图片类型 $created="imagecreatefrom".$dtype; $createw="imagecreatefrom".$wtype; $imgd=$created($dest_image); $imgw=$createw($watermark); switch($locate){ case 'center': $x=($dwidth-$wwidth)/2; $y=($dheight-$wheight)/2; break; case 'left_buttom': $x=1; $y=($dheight-$wheight-2); break; case 'right_buttom': $x=($dwidth-$wwidth-1); $y=($dheight-$wheight-2); break; default: die("未指定水印位置!"); break; } imagecopy($imgd,$imgw,$x,$y,0,0, $wwidth,$wheight); $save="image".$dtype; //显示 header("content-type:image/".$dtype); $save($imgd); imagedestroy($imgw); imagedestroy($imgd); } create_pic_watermark("ganlixin.jpg","test.png","left_buttom"); ?> |