VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 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. if(!emptyempty($groundImage) && file_exists($groundImage))  
  43. {  
  44. $ground_info = getimagesize($groundImage);  
  45. $ground_w = $ground_info[0];//取得背景图片的宽  
  46. $ground_h = $ground_info[1];//取得背景图片的高 
  47.  
  48. switch($ground_info[2])//取得背景图片的格式  
  49. {  
  50. case 1:$ground_im = imagecreatefromgif($groundImage);break;  
  51. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;  
  52. case 3:$ground_im = imagecreatefrompng($groundImage);break;  
  53. default:die($formatMsg);  
  54. }  
  55. }  
  56. else  
  57. {  
  58. die(”需要加水印的图片不存在!”);  
  59.  
  60. //水印位置  
  61. if($isWaterImage)//图片水印  
  62. {  
  63. $w = $water_w;  
  64. $h = $water_h;  
  65. $label = “图片的”;  
  66. }  
  67. else//文字水印  
  68. {  
  69. $temp = imagettfbbox(ceil($textFont*5),0,”./cour.ttf”,$waterText);//取得使用 TrueType 字体的文本的范围  
  70. $w = $temp[2] - $temp[6];  
  71. $h = $temp[3] - $temp[7];  
  72. unset($temp);  
  73. $label = “文字区域”;  
  74. }  
  75. if( ($ground_w<$w) || ($ground_h<$h) )  
  76. {  
  77. echo “需要加水印的图片的长度或宽度比水印”.$label.”还小,无法生成水印!”;  
  78. return;  
  79. }  
  80. switch($waterPos)  
  81. {  
  82. case 0://随机  
  83. $posX = rand(0,($ground_w - $w));  
  84. $posY = rand(0,($ground_h - $h));  
  85. break;  
  86. case 1://1为顶端居左  
  87. $posX = 0;  
  88. $posY = 0;  
  89. break;  
  90. case 2://2为顶端居中  
  91. $posX = ($ground_w - $w) / 2;  
  92. $posY = 0;  
  93. break;  
  94. case 3://3为顶端居右  
  95. $posX = $ground_w - $w;  
  96. $posY = 0;  
  97. break;  
  98. case 4://4为中部居左  
  99. $posX = 0;  
  100. $posY = ($ground_h - $h) / 2;  
  101. break;  
  102. case 5://5为中部居中  
  103. $posX = ($ground_w - $w) / 2;  
  104. $posY = ($ground_h - $h) / 2;  
  105. break;  
  106. case 6://6为中部居右  
  107. $posX = $ground_w - $w;  
  108. $posY = ($ground_h - $h) / 2;  
  109. break;  
  110. case 7://7为底端居左  
  111. $posX = 0;  
  112. $posY = $ground_h - $h;  
  113. break;  
  114. case 8://8为底端居中  
  115. $posX = ($ground_w - $w) / 2;  
  116. $posY = $ground_h - $h;  
  117. break;  
  118. case 9://9为底端居右  
  119. $posX = $ground_w - $w;  
  120. $posY = $ground_h - $h;  
  121. break;  
  122. default://随机  
  123. $posX = rand(0,($ground_w - $w));  
  124. $posY = rand(0,($ground_h - $h));  
  125. break;  
  126.  
  127. //设定图像的混色模式  
  128. imagealphablending($ground_im, true); 
  129.  
  130. if($isWaterImage)//图片水印  
  131. {  
  132. imagecopy($ground_im$water_im$posX$posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件  
  133. }  
  134. else//文字水印  
  135. {  
  136. if( !emptyempty($textColor) && (strlen($textColor)==7) )  
  137. {  
  138. $R = hexdec(substr($textColor,1,2));  
  139. $G = hexdec(substr($textColor,3,2));  
  140. $B = hexdec(substr($textColor,5));  
  141. }  
  142. else  
  143. {  
  144. die(”水印文字颜色格式不正确!”);  
  145. }  
  146. imagestring ( $ground_im$textFont$posX$posY$waterText, imagecolorallocate($ground_im$R$G$B));  
  147.  
  148. //生成水印后的图片  
  149. @unlink($groundImage);  
  150. switch($ground_info[2])//取得背景图片的格式  
  151. {  
  152. case 1:imagegif($ground_im,$groundImage);break;  
  153. case 2:imagejpeg($ground_im,$groundImage);break;  
  154. case 3:imagepng($ground_im,$groundImage);break;  
  155. default:die($errorMsg);  
  156.  
  157. //释放内存  
  158. if(isset($water_info)) unset($water_info);  
  159. if(isset($water_im)) imagedestroy($water_im);  
  160. unset($ground_info);  
  161. imagedestroy($ground_im);  
  162. }  
  163. //—————————————————————————————  
  164. $id=$_REQUEST['id'];  
  165. $num = count($_FILES['userfile']['name']);  
  166. print_r($_FILES['userfile']);  
  167. print_r($_FILES['userfile']['name']); 
  168.  
  169. echo $num;  
  170. echo “<bR>”;  
  171. if(isset($id)){  
  172. for($i=0;$i<$id;$i++){ 
  173.  
  174. if(isset($_FILES) && !emptyempty($_FILES['userfile']) && $_FILES['userfile']['size']>0)  
  175. {  
  176. $uploadfile = “./”.time().”_”.$_FILES['userfile'][name][$i];  
  177. echo “<br>”;  
  178. echo $uploadfile;  
  179. if (copy($_FILES['userfile']['tmp_name'][$i], $uploadfile))  
  180. {  
  181. echo “OK<br>”; 
  182.  
  183. //文字水印  
  184. //imageWaterMark($uploadfile,5,”",”HTTP://www.lvye.info”,5,”#cccccc“); 
  185.  
  186. //图片水印  
  187. $waterImage=”logo_ok1.gif”;//水印图片路径  
  188. imageWaterMark($uploadfile,9,$waterImage); 
  189.  
  190. echo “<img src=”".$uploadfile.”” border=”0”>”;  
  191. }  
  192. else  
  193. {  
  194. echo “Fail<br>”;  
  195. }  
  196. }  
  197. }  
  198.  
  199. ?>  
  200. <form enctype=”multipart/form-data” method=”POST”>  
  201. <?php  
  202. for($a=0;$a<$id;$a++){  
  203. echo “文件: <input name=”userfile[]” type=”file”><br>”; 
  204.  
  205. }  
  206. ?>  
  207. <input type=”submit” value=”上传”>  
  208. </form>  
  209. ?> 
  210.  
  211. //下面这段代码是一段图片上传并且给上传的图片加水印 
  212.  
  213. 参数说明:  
  214. $max_file_size  : 上传文件大小限制, 单位BYTE  
  215. $destination_folder : 上传文件路径  
  216. $watermark   : 是否附加水印(1为加水印,其他为不加水印);  
  217. 使用说明:  
  218. 1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库;  
  219. 2. 将extension_dir =改为你的php_gd2.dll所在目录;  
  220. 3. http://www.111cn.net/php.asp;  
  221. ****************************************************/  
  222. //上传文件类型列表  
  223. $uptypes=array(  
  224.     'image/jpg',   
  225.     'image/jpeg',  
  226.     'image/png',  
  227.     'image/pjpeg',  
  228.     'image/gif',  
  229.     'image/bmp',  
  230.     'image/x-png'  
  231. );  
  232. $max_file_size=2000000;     //上传文件大小限制, 单位BYTE  
  233. $destination_folder="uploadimg/"//上传文件路径  
  234. $watermark=1;      //是否附加水印(1为加水印,其他为不加水印);  
  235. $watertype=1;      //水印类型(1为文字,2为图片)  
  236. $waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);  
  237. $waterstring="http://www.xplore.cn/";  //水印字符串  
  238. $waterimg="xplore.gif";    //水印图片  
  239. $imgpreview=1;      //是否生成预览图(1为生成,其他为不生成);  
  240. $imgpreviewsize=1/2;    //缩略图比例  
  241. ?>  
  242. <html>  
  243. <head>  
  244. <title>ZwelL图片上传程序</title>  
  245. <style type="text/css教程">  
  246. <!--  
  247. body  
  248. {  
  249.      font-size: 9pt;  
  250. }  
  251. input  
  252. {  
  253.      background-color: #66CCFF;  
  254.      border: 1px inset #CCCCCC;  
  255. }  
  256. -->  
  257. </style>  
  258. </head>  
  259. <body>  
  260. <form enctype="multipart/form-data" method="post" name="upform">  
  261.   上传文件:  
  262.   <input name="upfile" type="file">  
  263.   <input type="submit" value="上传"><br>  
  264.   允许上传的文件类型为:<?=implode(', ',$uptypes)?>  
  265. </form>  
  266. <?php  
  267. if ($_SERVER['REQUEST_METHOD'] == 'POST')  
  268. {  
  269.     if (!is_uploaded_file($_FILES["upfile"][tmp_name]))  
  270.     //是否存在文件  
  271.     {  
  272.          echo "图片不存在www.111cn.net!";  
  273.          exit;  
  274.     }  
  275.     $file = $_FILES["upfile"];  
  276.     if($max_file_size < $file["size"])  
  277.     //检查文件大小  
  278.     {  
  279.         echo "文件太大!";  
  280.         exit;  
  281.     }  
  282.     if(!in_array($file["type"], $uptypes))  
  283.     //检查文件类型  
  284.     {  
  285.         echo "文件类型不符!".$file["type"];  
  286.         exit;  
  287.     }  
  288.     if(!file_exists($destination_folder))  
  289.     {  
  290.         mkdir($destination_folder);  
  291.     }  
  292.     $filename=$file["tmp_name"];  
  293.     $image_size = getimagesize($filename);  
  294.     $pinfo=pathinfo($file["name"]);  
  295.     $ftype=$pinfo['extension'];  
  296.     $destination = $destination_folder.time().".".$ftype;  
  297.     if (file_exists($destination) && $overwrite != true)  
  298.     {  
  299.         echo "同名文件已经存在了";  
  300.         exit;  
  301.     }  
  302.     if(!move_uploaded_file ($filename$destination))  
  303.     {  
  304.         echo "移动文件出错";  
  305.         exit;  
  306.     }  
  307.     $pinfo=pathinfo($destination);  
  308.     $fname=$pinfo[basename];  
  309.     echo " <font color=red>已经成功上传</font><br>文件名:  <font color=blue>".$destination_folder.$fname."</font><br>";  
  310.     echo " 宽度:".$image_size[0];  
  311.     echo " 长度:".$image_size[1];  
  312.     echo "<br> 大小:".$file["size"]." bytes";  
  313.     if($watermark==1)  
  314.     {  
  315.         $iinfo=getimagesize($destination,$iinfo);  
  316.         $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);  
  317.         $white=imagecolorallocate($nimage,255,255,255);  
  318.         $black=imagecolorallocate($nimage,0,0,0);  
  319.         $red=imagecolorallocate($nimage,255,0,0);  
  320.         imagefill($nimage,0,0,$white);  
  321.         switch ($iinfo[2])  
  322.         {  
  323.             case 1:  
  324.             $simage =imagecreatefromgif($destination);  
  325.             break;  
  326.             case 2:  
  327.             $simage =imagecreatefromjpeg($destination);  
  328.             break;  
  329.             case 3:  
  330.             $simage =imagecreatefrompng($destination);  
  331.             break;  
  332.             case 6:  
  333.             $simage =imagecreatefromwbmp($destination);  
  334.             break;  
  335.             default:  
  336.             die("不支持的文件类型");  
  337.             exit;  
  338.         }  
  339.         imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);  
  340.         imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);  
  341.         switch($watertype)  
  342.         {  
  343.             case 1:   //加水印字符串  
  344.             imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);  
  345.             break;  
  346.             case 2:   //加水印图片  
  347.             $simage1 =imagecreatefromgif("xplore.gif");  
  348.             imagecopy($nimage,$simage1,0,0,0,0,85,15);  
  349.             imagedestroy($simage1);  
  350.             break;  
  351.         }  
  352.         switch ($iinfo[2])  
  353.         {  
  354.             case 1:  
  355.             //imagegif($nimage, $destination);  
  356.             imagejpeg($nimage$destination);  
  357.             break;  
  358.             case 2:  
  359.             imagejpeg($nimage$destination);  
  360.             break;  
  361.             case 3:  
  362.             imagepng($nimage$destination);  
  363.             break;  
  364.             case 6:  
  365.             imagewbmp($nimage$destination);  
  366.             //imagejpeg($nimage, $destination);  
  367.             break;  
  368.         }  
  369.         //覆盖原上传文件  
  370.         imagedestroy($nimage);  
  371.         imagedestroy($simage);  
  372.     } //开源代码phpfensi.com 
  373.     if($imgpreview==1)  
  374.     {  
  375.     echo "<br>图片预览:<br>";  
  376.     echo "<img src="".$destination."" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);  
  377.     echo " alt="图片预览: 文件名:".$destination." www.111cn.net上传时间:">";  
  378.     }  
  379. }
  380.  

出处:http://www.phpfensi.com/php/20140909/5158.html


相关教程