PHP的类,是用来给图片加水印的

PHP代码
  1. <?php   
  2. /*  
  3. * 功能:PHP图片水印 (水印支持图片或文字)  
  4. * 参数:  
  5. *     $groundImage   背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;  
  6. *     $waterPos     水印位置,有10种状态,0为随机位置;  
  7. *                 1为顶端居左,2为顶端居中,3为顶端居右;  
  8. *                 4为中部居左,5为中部居中,6为中部居右;  
  9. *                 7为底端居左,8为底端居中,9为底端居右;  
  10. *     $waterImage     图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;  
  11. *     $waterText     文字水印,即把文字作为为水印,支持ASCII码,不支持中文;  
  12. *     $textFont     文字大小,值为1、2、3、4或5,默认为5;  
  13. *     $textColor     文字颜色,值为十六进制颜色值,默认为#FF0000(红色);  
  14. *  
  15. * 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG  
  16. *     $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。  
  17. *     当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。  
  18. *     加水印后的图片的文件名和 $groundImage 一样。  
  19. * 作者:longware @ 2004-11-3 14:15:13  
  20. */  
  21. function imageWaterMark($groundImage,$waterPos=0,$waterImage=”",$waterText=”",$textFont=5,$textColor=”#FF0000″)   
  22. {   
  23.   $isWaterImage = FALSE;   
  24.   $formatMsg = “暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。”;   
  25.   
  26.   //读取水印文件   
  27.   if(!emptyempty($waterImage) && file_exists($waterImage))   
  28.   {   
  29.     $isWaterImage = TRUE;   
  30.     $water_info = getimagesize($waterImage);   
  31.     $water_w   = $water_info[0];//取得水印图片的宽   
  32.     $water_h   = $water_info[1];//取得水印图片的高   
  33.   
  34.     switch($water_info[2])//取得水印图片的格式   
  35.     {   
  36.         case 1:$water_im = imagecreatefromgif($waterImage);break;   
  37.         case 2:$water_im = imagecreatefromjpeg($waterImage);break;   
  38.         case 3:$water_im = imagecreatefrompng($waterImage);break;   
  39.         default:die($formatMsg);   
  40.     }   
  41.   }   
  42.   
  43.   //读取背景图片   
  44.   if(!emptyempty($groundImage) && file_exists($groundImage))   
  45.   {   
  46.     $ground_info = getimagesize($groundImage);   
  47.     $ground_w   = $ground_info[0];//取得背景图片的宽   
  48.     $ground_h   = $ground_info[1];//取得背景图片的高   
  49.   
  50.     switch($ground_info[2])//取得背景图片的格式   
  51.     {   
  52.         case 1:$ground_im = imagecreatefromgif($groundImage);break;   
  53.         case 2:$ground_im = imagecreatefromjpeg($groundImage);break;   
  54.         case 3:$ground_im = imagecreatefrompng($groundImage);break;   
  55.         default:die($formatMsg);   
  56.     }   
  57.   }   
  58.   else  
  59.   {   
  60.     die(”需要加水印的图片不存在!”);   
  61.   }   
  62.   
  63.   //水印位置   
  64.   if($isWaterImage)//图片水印   
  65.   {   
  66.     $w = $water_w;   
  67.     $h = $water_h;   
  68.     $label = “图片的”;   
  69.   }   
  70.   else//文字水印   
  71.   {   
  72.     $temp = imagettfbbox(ceil($textFont*5),0,”./cour.ttf”,$waterText);//取得使用 TrueType 字体的文本的范围   
  73.     $w = $temp[2] - $temp[6];   
  74.     $h = $temp[3] - $temp[7];   
  75.     unset($temp);   
  76.     $label = “文字区域”;   
  77.   }   
  78.   if( ($ground_w<$w) || ($ground_h<$h) )   
  79.   {   
  80.     echo “需要加水印的图片的长度或宽度比水印”.$label.”还小,无法生成水印!”;   
  81.     return;   
  82.   }   
  83.   switch($waterPos)   
  84.   {   
  85.     case 0://随机   
  86.         $posX = rand(0,($ground_w - $w));   
  87.         $posY = rand(0,($ground_h - $h));   
  88.         break;   
  89.     case 1://1为顶端居左   
  90.         $posX = 0;   
  91.         $posY = 0;   
  92.         break;   
  93.     case 2://2为顶端居中   
  94.         $posX = ($ground_w - $w) / 2;   
  95.         $posY = 0;   
  96.         break;   
  97.     case 3://3为顶端居右   
  98.         $posX = $ground_w - $w;   
  99.         $posY = 0;   
  100.         break;   
  101.     case 4://4为中部居左   
  102.         $posX = 0;   
  103.         $posY = ($ground_h - $h) / 2;   
  104.         break;   
  105.     case 5://5为中部居中   
  106.         $posX = ($ground_w - $w) / 2;   
  107.         $posY = ($ground_h - $h) / 2;   
  108.         break;   
  109.     case 6://6为中部居右   
  110.         $posX = $ground_w - $w;   
  111.         $posY = ($ground_h - $h) / 2;   
  112.         break;   
  113.     case 7://7为底端居左   
  114.         $posX = 0;   
  115.         $posY = $ground_h - $h;   
  116.         break;   
  117.     case 8://8为底端居中   
  118.         $posX = ($ground_w - $w) / 2;   
  119.         $posY = $ground_h - $h;   
  120.         break;   
  121.     case 9://9为底端居右   
  122.         $posX = $ground_w - $w;   
  123.         $posY = $ground_h - $h;   
  124.         break;   
  125.     default://随机   
  126.         $posX = rand(0,($ground_w - $w));   
  127.         $posY = rand(0,($ground_h - $h));   
  128.         break;     
  129.   }   
  130.   
  131.   //设定图像的混色模式   
  132.   imagealphablending($ground_im, true);   
  133.   
  134.   if($isWaterImage)//图片水印   
  135.   {   
  136.     imagecopy($ground_im$water_im$posX$posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件         
  137.   }   
  138.   else//文字水印   
  139.   {   
  140.     if( !emptyempty($textColor) && (strlen($textColor)==7) )   
  141.     {   
  142.         $R = hexdec(substr($textColor,1,2));   
  143.         $G = hexdec(substr($textColor,3,2));   
  144.         $B = hexdec(substr($textColor,5));   
  145.     }   
  146.     else  
  147.     {   
  148.         die(”水印文字颜色格式不正确!”);   
  149.     }   
  150.     imagestring ( $ground_im$textFont$posX$posY$waterText, imagecolorallocate($ground_im$R$G$B));         
  151.   }   
  152.   
  153.   //生成水印后的图片   
  154.   @unlink($groundImage);   
  155.   switch($ground_info[2])//取得背景图片的格式   
  156.   {   
  157.     case 1:imagegif($ground_im,$groundImage);break;   
  158.     case 2:imagejpeg($ground_im,$groundImage);break;   
  159.     case 3:imagepng($ground_im,$groundImage);break;   
  160.     default:die($errorMsg);   
  161.   }   
  162.   
  163.   //释放内存   
  164.   if(isset($water_info)) unset($water_info);   
  165.   if(isset($water_im)) imagedestroy($water_im);   
  166.   unset($ground_info);   
  167.   imagedestroy($ground_im);   
  168. }   
  169. //—————————————————————————————   
  170. $id=$_REQUEST[’id’];   
  171. $num = count($_FILES[’userfile’][’name’]);   
  172. print_r($_FILES[’userfile’]);   
  173. print_r($_FILES[’userfile’][’name’]);   
  174.   
  175. echo $num;   
  176. echo “<bR>”;   
  177. if(isset($id)){   
  178.  for($i=0;$i<$id;$i++){   
  179.      
  180.   if(isset($_FILES) && !emptyempty($_FILES[’userfile’]) && $_FILES[’userfile’][’size’]>0)   
  181. {   
  182.   $uploadfile = “./”.time().”_”.$_FILES[’userfile’][name][$i];   
  183.   echo “<br>”;   
  184.   echo $uploadfile;   
  185.   if (copy($_FILES[’userfile’][’tmp_name’][$i], $uploadfile))   
  186.   {   
  187.     echo “OK<br>”;   
  188.   
  189.     //文字水印   
  190.     //imageWaterMark($uploadfile,5,”",”HTTP://www.lvye.info”,5,”#cccccc“);   
  191.   
  192.     //图片水印   
  193.     $waterImage=”logo_ok1.gif”;//水印图片路径   
  194.     imageWaterMark($uploadfile,9,$waterImage);   
  195.   
  196.     echo “<img src=\”".$uploadfile.”\” border=\”0\”>”;   
  197.   }   
  198.   else  
  199.   {   
  200.     echo “Fail<br>”;   
  201.   }   
  202. }   
  203.  }   
  204. }   
  205.   
  206. ?>   
  207. <form enctype=”multipart/form-data” method=”POST”>   
  208. <?php   
  209. for($a=0;$a<$id;$a++){   
  210.  echo “文件: <input name=\”userfile[]\” type=\”file\”><br>”;   
  211.     
  212. }   
  213. ?>   
  214. <input type=”submit” value=”上传”>   
  215. </form>   
posted @ 2008-04-02 20:40  二宝的博客  阅读(287)  评论(0编辑  收藏  举报