php图片上传加水印,自动增加水印

这是一款完美的php文件上传代码,图片上传成功后并自动给图片增加上水印,这样很好的快速的提高的了要手工一张张增加水印效果,代码如下:

  1. function upload($uploadfile,$watermark=1,$watertype=1,$content){
  2. foreach($uploadfile['name'] as $key=>$name) { //多文件上传函数
  3. uploadall($uploadfile,$key,$watermark,$watertype,$content);
  4. }
  5. }
  6. function uploadall($uploadfile,$i,$watermark,$watertype,$content) {
  7. $watermark=$watermark; //是否附加水印(1为加水印,其他为不加水印);
  8. $watertype=$watertype; //水印类型(1为文字,2为图片)
  9. $watercontent=$content; //水印的内容
  10. if(emptyempty($uploadfile['name'][$i])) {
  11. die("未选择文件上传");
  12. }
  13. if($uploadfile['error'][$i] == 2) { //验证html判断结果
  14. die("上传的文件太大了");
  15. }
  16. $allow_filemaxsize = 2048000; // 2m
  17. $filesize = $uploadfile['size'][$i];
  18. if($filesize > $allow_filemaxsize) {
  19. die("上传的文件太大了");
  20. }
  21. $allow_filetypes = array("jpeg", "gif", "png","jpg","pjpeg");//允许上传的文件统一资源类型 mimetype
  22. $allow = false; //默认都不允许
  23. $mimetype = $uploadfile['type'][$i]; //上传文件的mime文件类型
  24. foreach($allow_filetypes as $t) {
  25. if(strpos($mimetype, $t) !== false) {
  26. $allow = true; //找到了符合上传条件的文件类型
  27. break;
  28. }
  29. }
  30. if($allow == false) {
  31. die("上传的文件类型不被允许");
  32. }
  33. $result = is_uploaded_file($uploadfile['tmp_name'][$i]); //判断是否为上传动作产生的
  34. if(!$result) {
  35. die("上传的文件有误");
  36. }
  37. $uploaddir = "img/"; //上传文件保存目录
  38. if(!file_exists($uploaddir)) mkdir($uploaddir, 0777, true);//若上传保存目录不存在,则递归创建
  39. /**重命名文件**/
  40. $filetype = explode(".", $uploadfile['name'][$i]);
  41. $filetype = array_pop($filetype);
  42. $uploadfilename = time().".".$filetype;
  43. $_session['filename'] = $uploadfilename;
  44. //end
  45. $result = move_uploaded_file($uploadfile['tmp_name'][$i], $uploaddir.$uploadfilename);
  46. if($result) {
  47. echo "文件上传成功";
  48. }else{
  49. switch($uploadfile['error'][$i]) {
  50. case 1:return "上传的文件超出了php.ini中设定的最大值";break;
  51. case 2:return "上传的文件超出了html中设定的最大值";break;
  52. case 3:return "文件只有部分被上传";break;
  53. case 4;return "没有文件被上传";break;
  54. default:die("文件上传失败");
  55. }
  56. }
  57. if($watermark==1) {
  58. $iinfo=getimagesize($uploaddir.$uploadfilename); //获取图片的相关信息,得到数组
  59. $nimage=imagecreatetruecolor($iinfo[0],$iinfo[1]);
  60. $white=imagecolorallocate($nimage,255,255,255); //设置背景颜色为白色
  61. $black=imagecolorallocate($nimage,0,0,0); //设置背景颜色为黑色
  62. $red=imagecolorallocate($nimage,255,0,0); //设置背景颜色为红色
  63. imagefill($nimage,0,0,$white); //背景填充为白色
  64. switch ($iinfo[2]) {
  65. case 1:
  66. $simage =imagecreatefromgif($uploaddir.$uploadfilename);
  67. break;
  68. case 2:
  69. $simage =imagecreatefromjpeg($uploaddir.$uploadfilename);
  70. break;
  71. case 3:
  72. $simage =imagecreatefrompng($uploaddir.$uploadfilename);
  73. break;
  74. case 6:
  75. $simage =imagecreatefromwbmp($uploaddir.$uploadfilename);
  76. break;
  77. default:
  78. die("不支持的文件类型");
  79. exit;
  80. }
  81. imagecopy($nimage,$simage,0,0,0,0,$iinfo['0'],$iinfo['1']);
  82. switch($watertype) {
  83. case 1: //加水印字符串
  84. imagestring($nimage,5,$iinfo['0']/2-50,$iinfo['1']-30,$watercontent,$black);
  85. break;
  86. case 2: //加水印图片
  87. $simage1 =imagecreatefromgif($watercontent);
  88. $size = getimagesize($watercontent);
  89. imagecopy($nimage,$simage1,$iinfo['0']/2+50,$iinfo['1']-100,0,0,$size[0],$size[1]);
  90. imagedestroy($simage1);
  91. break;
  92. }
  93. switch ($iinfo[2]) {
  94. case 1:
  95. imagejpeg($nimage, $uploaddir.$uploadfilename); //将图像$nimage以$destination文件名创建一个jpeg的格式文件
  96. break;
  97. case 2:
  98. imagejpeg($nimage, $uploaddir.$uploadfilename);
  99. break;
  100. case 3:
  101. imagepng($nimage, $uploaddir.$uploadfilename);
  102. break;
  103. case 6:
  104. imagewbmp($nimage, $uploaddir.$uploadfilename);
  105. break;
  106. }
  107. imagedestroy($nimage); //覆盖原上传文件
  108. imagedestroy($simage);
  109. } //开源代码phpfensi.com
  110. }
  111. if(@$_get['act'] == "insert") { //未作参数校验
  112. //该函数的四个参数分别是:上传控件的name值;是否加水印(1为加,其他数字为不佳);
  113. //水印的类型(1为字符串,2为图片);水印的内容,字符串时写数据,图片时写图片的地址;
  114. $picture = upload($_files['picture'],1,2,"img/watermark.gif");//上传文件,并返回上传后的文件路径名
  115. }

上传代码如下:

  1. <form method="post" action="" enctype="multipart/form-data">
  2. <table cellpadding="5" cellspacing="0">
  3. <tr>
  4. <td>产品图片:</td>
  5. <td><input type="hidden" name="max_file_size" value="1024000">
  6. <input type="file" name="picture[]">
  7. </td>
  8. </tr>
  9. <tr>
  10. <td colspan="2" align="center"><input type="submit" value="提交"></td>
  11. </tr>
  12. </table>
  13. </form>