1. 二宝博客首页
  2. php

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

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

原创文章,作者:键盘游走者,如若转载,请注明出处:http://www.708034.com/2008/04/php%e7%9a%84%e7%b1%bb%ef%bc%8c%e6%98%af%e7%94%a8%e6%9d%a5%e7%bb%99%e5%9b%be%e7%89%87%e5%8a%a0%e6%b0%b4%e5%8d%b0%e7%9a%84/

发表评论

电子邮件地址不会被公开。 必填项已用*标注