宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应

[php]Kindeditor解决PHP版上传加水印的问题源代码

2016-10-13 00:16:48 点击量:14 标签: 收藏本文

[php]Kindeditor解决PHP版上传加水印的问题源代码

  1. <?php 
  2. /** 
  3. * KindEditor PHP 
  4. * 
  5.  *  
  6. 本PHP程序是演示程序,建议不要直接在实际项目中使用。 
  7. * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。 
  8. *  
  9. * 
  10. *关于加水印的地方有几个地方要设置一下啊!!!!!!! 具体在下部分 自己找一下。 
  11. * 
  12. */ 
  13.  
  14. require_once 'JSON.php'; 
  15. $php_path = dirname(__FILE__) . '/'; 
  16. $php_url = dirname($_SERVER['PHP_SELF']) . '/'; 
  17.  
  18. //文件保存目录路径 
  19. $save_path = $php_path . '../../../attached/'; 
  20. //文件保存目录 
  21. URL$save_url = $php_url . '../../../attached/'; 
  22. //定义允许上传的文件扩展名 
  23. $ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp'); 
  24. //最大文件大小 
  25. $max_size = 1000000; 
  26.  
  27. $save_path = realpath($save_path) . '/'; 
  28.  
  29. //有上传文件时 
  30. if (emptyempty($_FILES) === false) { 
  31. //原文件名 
  32. $file_name = $_FILES['imgFile']['name']; 
  33. //服务器上临时文件名 
  34. $tmp_name = $_FILES['imgFile']['tmp_name']; 
  35. //文件大小 
  36. $file_size = $_FILES['imgFile']['size']; 
  37.  
  38. $image_size = getimagesize($tmp_name); 
  39.  
  40. //检查文件名 
  41. if (!$file_name) {alert("请选择文件。"); 
  42. } 
  43. //检查目录 
  44. if (@is_dir($save_path) === false) {alert("上传目录不存在。"); 
  45. } 
  46. //检查目录写权限 
  47. if (@is_writable($save_path) === false) {alert("上传目录没有写权限。"); 
  48. } 
  49. //检查是否已上传 
  50. if (@is_uploaded_file($tmp_name) === false) {alert("临时文件可能不是上传文件。"); 
  51. } 
  52. //检查文件大小 
  53. if ($file_size > $max_size) {alert("上传文件大小超过限制。"); 
  54. } 
  55. //获得文件扩展名 
  56. $temp_arr = explode(".", $file_name); 
  57. $file_ext = array_pop($temp_arr); 
  58. $file_ext = trim($file_ext); 
  59. $file_ext = strtolower($file_ext); 
  60. //检查扩展名 
  61. if (in_array($file_ext, $ext_arr) === false) {alert("上传文件扩展名是不允许的扩展名。"); 
  62. } 
  63. //创建文件夹 
  64. $ymd = date("Ymd");$save_path .= $ymd . "/"; 
  65. $save_url .= $ymd . "/"; 
  66. if (!file_exists($save_path)) {mkdir($save_path); 
  67. } 
  68. //新文件名 
  69. $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext; 
  70. //移动文件 
  71. $file_path = $save_path . $new_file_name; 
  72. if (move_uploaded_file($tmp_name, $file_path) === false) {alert("上传文件失败。"); 
  73. } 
  74.  
  75. //@chmod($file_path, 0644); 
  76. $file_url = $save_url . $new_file_name;  
  77.  
  78. $watermark=1; //是否附加水印(1为加水印,其他为不加水印); 
  79. $watertype=2; //水印类型(1为文字,2为图片) 
  80. $waterstring="http://www.168hs.cn/"; //水印字符串 
  81. $waterimg="player_img_path/20161202/1g3rny2ilm1.gif"; //水印图片,试了相对路不行,只好用绝对路径。 
  82.  
  83. if($watermark==1) 
  84. { 
  85. $myfilename="../../../attached/".$ymd."/".$new_file_name;  
  86. $iinfo=getimagesize($myfilename,$iinfo);  
  87. $nimage=imagecreatetruecolor($image_size[0],$image_size[1]); 
  88. $white=imagecolorallocate($nimage,255,255,255); 
  89. $black=imagecolorallocate($nimage,0,0,0); 
  90. $red=imagecolorallocate($nimage,255,0,0); 
  91. imagefill($nimage,0,0,$white); 
  92. switch ($iinfo[2]) 
  93. { 
  94. case 1: 
  95. $simage =imagecreatefromgif($myfilename); 
  96. break; 
  97. case 2: 
  98. $simage =imagecreatefromjpeg($myfilename); 
  99. break; 
  100. case 3: 
  101. $simage =imagecreatefrompng($myfilename); 
  102. break; 
  103. case 6: 
  104. $simage =imagecreatefromwbmp($myfilename); 
  105. break; 
  106. default:die("不支持的文件类型"); 
  107. exit; 
  108. } 
  109.  
  110. imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); 
  111.  
  112. switch($watertype) 
  113. { 
  114. case 1: //加水印字符串 
  115. imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black); 
  116. break; 
  117. case 2: //加水印图片 
  118. $simage1 =imagecreatefromgif($waterimg); 
  119. imagecopymerge($nimage,$simage1,$image_size[0]-150,$image_size[1]-50,0,0,150,50,60); //设置水印的位置  
  120. imagedestroy($simage1); 
  121. break; 
  122. } 
  123.  
  124. switch ($iinfo[2]) 
  125. { 
  126. case 1: 
  127. //imagegif($nimage, $destination); 
  128. imagejpeg($nimage, $myfilename); 
  129. break; 
  130. case 2: 
  131. imagejpeg($nimage, $myfilename); 
  132. break; 
  133. case 3: 
  134. imagepng($nimage, $myfilename); 
  135. break; 
  136. case 6: 
  137. imagewbmp($nimage,$myfilename); 
  138. //imagejpeg($nimage, $destination); 
  139. break; 
  140. } 
  141.  
  142. //覆盖原上传文件 
  143. imagedestroy($nimage); 
  144. imagedestroy($simage); 
  145. } 
  146.  
  147. header('Content-type: text/html; charset=UTF-8'); 
  148. $json = new Services_JSON(); 
  149. echo $json->encode(array('error' => 0, 'url' => $file_url)); 
  150. exit; 
  151. } 
  152.  
  153. function alert($msg) { 
  154. header('Content-type: text/html; charset=UTF-8'); 
  155. $json = new Services_JSON(); 
  156. echo $json->encode(array('error' => 1, 'message' => $msg)); 
  157. exit; 
  158. } 
  159. ?>