PHP头条
热点:

PHP图片加水印代码示例解析


一个正规的网站,在需要上传图片时,往往都会需要在图片上增加自己网站的LOGO水印。那么如何实现这一步骤呢?首先让我们来了解PHP图片加水印的原理。

  • 正确应用PHP foreach循环
  • PHP文件函数手册概览
  • 正确理解PHP抽象类应用
  • PHP搜索数组的实现方法介绍
  • 详细解读PHP类的封装

通过判断文件类型建立图形,然后把其复制到原建立的图形上,填充并建立rectangle,以备写入imagestring()或是在原已经定好的图像程序当中判断水印类型:一是字符串,另是增加一个图形对象在上面。以下是PHP图片加水印的转载!

参数说明:

$max_file_size : 上传文件大小限制, 单位BYTE
$destination_folder : 上传文件路径
$watermark : 是否附加水印(1为加水印,其他为不加水印);

PHP图片加水印使用说明:

1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用到GD库;
2. 将extension_dir =改为你的php_gd2.dll所在目录;

PHP图片加水印的代码示例:

  1. //上传文件类型列表     
  2. $uptypes=array(     
  3. 'image/jpg',     
  4. 'image/jpeg',     
  5. 'image/png',     
  6. 'image/pjpeg',     
  7. 'image/gif',     
  8. 'image/bmp',     
  9. 'image/x-png'     
  10. );     
  11. $max_file_size=2000000; //上传文件大小限制, 单位BYTE     
  12. $destination_folder="uploadimg/"; //上传文件路径     
  13. $watermark=1; //是否附加水印(1为加水印,其他为不加水印);     
  14. $watertype=1; //水印类型(1为文字,2为图片)     
  15. $waterposition=1; //水印位置(1为左下角,2为右下角  
  16. ,3为左上角,4为右上角,5为居中);     
  17. $waterstring="< A href="http://www.xplore.cn/"> 
  18. http://www.xplore.cn/< /A>"; //水印字符串     
  19. $waterimg="xplore.gif"; //水印图片     
  20. $imgpreview=1; //是否生成预览图(1为生成,其他为不生成);     
  21. $imgpreviewsize=1/2; //缩略图比例     
  22. ?>     
  23. < html>     
  24. < head>     
  25. < title>ZwelL图片上传程序</title>     
  26. < style type="text/css">     
  27. <!--     
  28. body     
  29. {     
  30. font-size: 9pt;     
  31. }     
  32. input     
  33. {     
  34. background-color: #66CCFF;     
  35. border: 1px inset #CCCCCC;     
  36. }     
  37. -->     
  38. < /style>     
  39. < /head>     
  40. < body>     
  41. < form enctype="multipart/form-data"   
  42. method="post" name="upform">     
  43. 上传文件:     
  44. <input name="upfile" type="file">     
  45. < input type="submit" value="上传">< br>     
  46. 允许上传的文件类型为:<?=implode(', ',$uptypes)?>     
  47. < /form>     
  48. < ?php     
  49. if ($_SERVER['REQUEST_METHOD'] == 'POST')     
  50. {     
  51. if (!is_uploaded_file($_FILES["upfile"]  
  52. [tmp_name]))     
  53. //是否存在文件     
  54. {     
  55. echo "图片不存在!";     
  56. exit;     
  57. }     
  58. $file = $_FILES["upfile"];     
  59. if($max_file_size < $file["size"])     
  60. //检查文件大小     
  61. {     
  62. echo "文件太大!";     
  63. exit;     
  64. }     
  65. if(!in_array($file["type"], $uptypes))     
  66. //检查文件类型     
  67. {     
  68. echo "文件类型不符!".$file["type"];     
  69. exit;     
  70. }     
  71. if(!file_exists($destination_folder))     
  72. {     
  73. mkdir($destination_folder);     
  74. }     
  75. $filename=$file["tmp_name"];     
  76. $image_size = getimagesize($filename);     
  77. $pinfo=pathinfo($file["name"]);     
  78. $ftype=$pinfo['extension'];     
  79. $destination = $destination_folder.  
  80. time().".".$ftype;     
  81. if (file_exists($destination) &&  
  82.  $overwrite != true)     
  83. {     
  84. echo "同名文件已经存在了";     
  85. exit;     
  86. }     
  87. if(!move_uploaded_file ($filename,  
  88.  $destination))     
  89. {     
  90. echo "移动文件出错";     
  91. exit;     
  92. }     
  93. $pinfo=pathinfo($destination);     
  94. $fname=$pinfo[basename];     
  95. echo " <font color=red>已经成功上传  
  96. < /font><br>文件名:     
  97. < font color=blue>".$destination_folder.  
  98. $fname."< /font>< br>";     
  99. echo " 宽度:".$image_size[0];     
  100. echo " 长度:".$image_size[1];     
  101. echo "<br> 大小:".$file["size"]." bytes";     
  102. if($watermark==1)     
  103. {     
  104. $iinfo=getimagesize($destination,$iinfo);     
  105. $nimage=imagecreatetruecolor($image_size[0]  
  106. ,$image_size[1]);     
  107. $white=imagecolorallocate($nimage,255,255,255);     
  108. $black=imagecolorallocate($nimage,0,0,0);     
  109. $red=imagecolorallocate($nimage,255,0,0);     
  110. imagefill($nimage,0,0,$white);     
  111. switch ($iinfo[2])     
  112. {     
  113. case 1:     
  114. $simage =imagecreatefromgif($destination);     
  115. break;     
  116. case 2:     
  117. $simage =imagecreatefromjpeg($destination);     
  118. break;     
  119. case 3:     
  120. $simage =imagecreatefrompng($destination);     
  121. break;     
  122. case 6:     
  123. $simage =imagecreatefromwbmp($destination);     
  124. break;     
  125. default:     
  126. die("不支持的文件类型");     
  127. exit;     
  128. }     
  129. imagecopy($nimage,$simage,0,0,0,0,  
  130. $image_size[0],$image_size[1]);     
  131. imagefilledrectangle($nimage,1,  
  132. $image_size[1]-15,80,$image_size[1],$white);     
  133. switch($watertype)     
  134. {     
  135. case 1: //加水印字符串     
  136. imagestring($nimage,2,3,$image_size[1]-15,  
  137. $waterstring,$black);     
  138. break;     
  139. case 2: //加水印图片     
  140. $simage1 =imagecreatefromgif("xplore.gif");     
  141. imagecopy($nimage,$simage1,0,0,0,0,85,15);     
  142. imagedestroy($simage1);     
  143. break;     
  144. }     
  145. switch ($iinfo[2])     
  146. {     
  147. case 1:     
  148. //imagegif($nimage, $destination);     
  149. imagejpeg($nimage, $destination);     
  150. break;     
  151. case 2:     
  152. imagejpeg($nimage, $destination);     
  153. break;     
  154. case 3:     
  155. imagepng($nimage, $destination);     
  156. break;     
  157. case 6:     
  158. imagewbmp($nimage, $destination);     
  159. //imagejpeg($nimage, $destination);     
  160. break;     
  161. }     
  162. //覆盖原上传文件     
  163. imagedestroy($nimage);     
  164. imagedestroy($simage);     
  165. }     
  166. if($imgpreview==1)     
  167. {     
  168. echo "< br>图片预览:<br>";     
  169. echo "< ccid_file values="\" width=".  
  170. ($image_size[0]*$imgpreviewsize)."     
  171. height=".($image_size[1]*$imgpreviewsize);"     
  172. echo " alt=\"图片预览:\r文件名:".  
  173. $destination."\r上传时间:\" />";     
  174. }     
  175. }     
  176. ?>     
  177. < /body>     
  178. < /html>  

以上这段代码范例就是PHP图片加水印的具体实现方法。

www.phpzy.comtrue/php/12462.htmlTechArticlePHP图片加水印代码示例解析 一个正规的网站,在需要上传图片时,往往都会需要在图片上增加自己网站的LOGO水印。那么如何实现这一步骤呢?首先让我们来了解PHP图片加水印的原理。...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐