php 图片上传代码,具有生成缩略图与增加水印功能

这款图片上传源代码是一款可以上传图片并且还具有给上传的图片生成缩略图与增加水印功能,可以说是一款完美的图片上传类,实例代码如下:

  1. class upfile {
  2. public $filepath = "www.phpfensi.com/"; //上传文件存放文件夹
  3. public $filesize = 1000000; //允许上传的大小
  4. //如果要修改允许上传文件的类型 请搜索 【 switch ($upfiletype) { //文件类型 】
  5. public $reimagesize = array (
  6. true, //是否生成缩略图
  7. 400, //缩略图宽
  8. 300,//缩略图高
  9. "" //缩略图存放文件夹 如果为空和当前要生成缩略图的文件在同一目录 文件前缀r_
  10. ); //是否生成缩略图 array(生成或不生成,缩略图宽,缩略图高,存放文件夹); 注意:存放文件夹后跟 '/'
  11. public $india = true; //是否打水印 true打 false不打
  12. public $indiaimage = ""; //水印图片地址为空则不打图片水印 如果有文字水印建议不要开启图片水印
  13. public $indiaimagex = 100; //图片距离图片左边距离
  14. public $indiaimagey = 10; //图片距离图片上面距离
  15. public $indiatext = "www.phpfensi.com"; //水印文字
  16. public $fontsize = 6; //水印文字大小,1最小6最大
  17. public $indiatextx = 10; //文字距离图片左边距离
  18. public $indiatexty = 10; //文字距离图片上面距离
  19. public $r = 250; //图片颜色三原色 $r红
  20. public $g = 250; //$g绿
  21. public $b = 250; //$b蓝
  22. public $indiapath = ""; //加了水印的图片保存路径,如果为空就直接替代原来的图片
  23. //开始上传处理
  24. function uploadfile($upfile) {
  25. if ($upfile == "") {
  26. die("uploadfile:参数不足");
  27. }
  28. if (!file_exists($this->filepath)) {
  29. mkdir($this->filepath);
  30. }
  31. $upfiletype = $upfile['type'];
  32. $upfilesize = $upfile['size'];
  33. $upfiletmpname = $upfile['tmp_name'];
  34. $upfilename = $upfile['name'];
  35. $upfileerror = $upfile['error'];
  36. if ($upfilesize > $this->filesize) {
  37. return false; //文件过大
  38. }
  39. switch ($upfiletype) { //文件类型
  40. case 'image/jpeg' :
  41. $type = 'jpg';
  42. break;
  43. case 'image/pjpeg' :
  44. $type = 'jpg';
  45. break;
  46. case 'image/png' :
  47. $type = 'png';
  48. break;
  49. case 'image/gif' :
  50. $type = 'gif';
  51. break;
  52. }
  53. if (!isset ($type)) {
  54. return false; //不支持此类型
  55. }
  56. if (!is_uploaded_file($upfiletmpname) or !is_file($upfiletmpname)) {
  57. return false;
  58. ; //文件不是经过正规上传的;
  59. }
  60. if ($this->upfileerror != 0) {
  61. return false; //其他错误
  62. }
  63. if ($this->upfileerror == 0) {
  64. if (!file_exists($upfiletmpname)) {
  65. return false; //临时文件不存在
  66. } else {
  67. $filename = date("ymdhis", time() + 3600 * 8); //图片已当前时间命名
  68. $filename = $this->filepath . $filename . "." . $type;
  69. if (!move_uploaded_file($upfiletmpname, $filename)) {
  70. return false; //文件在移动中丢失
  71. } else {
  72. if ($this->india == true) {
  73. $this->goindia($filename, $type,true);
  74. } else {
  75. if ($this->reimagesize[0] == true) {
  76. $this->goreimagesize($filename, $type);
  77. } else {
  78. return true; //上传成功!
  79. unlink($upfiletmpname);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. //添加水印处理
  87. function goindia($filename, $filetype,$reimage=false) {
  88. if (!file_exists($filename)) {
  89. $this->reerror(7); //要添加水印的文件不存在
  90. } else {
  91. if ($filetype == "jpg") {
  92. $im = imagecreatefromjpeg($filename);
  93. } else
  94. if ($filetype == "gif") {
  95. $im = imagecreatefromgif($filename);
  96. } else
  97. if ($filetype == "png") {
  98. $im = imagecreatefrompng($filename);
  99. }
  100. if ($this->indiatext != "") { //如果水印文字不为空
  101. $textcolor = imagecolorallocate($im, $this->r, $this->g, $this->b); //设置文字颜色
  102. imagestring($im, $this->fontsize, $this->indiatextx, $this->indiatexty, $this->indiatext, $textcolor); //将文字写入图片
  103. }
  104. if ($this->indiaimage != "") {//如果水印图片不为空
  105. $indiaimagetype = getimagesize($this->indiaimage);
  106. $logow = $indiaimagetype[0]; //得到水印图片的宽
  107. $logoh = $indiaimagetype[1]; //得到水印图片的高
  108. switch ($indiaimagetype[2]) { //判断水印图片的格式
  109. case 1 :
  110. $indiaimagetype = "gif";
  111. $logo = imagecreatefromgif($this->indiaimage);
  112. break;
  113. case 2 :
  114. $indiaimagetype = "jpg";
  115. $logo = imagecreatefromjpeg($this->indiaimage);
  116. break;
  117. case 3 :
  118. $indiaimagetype = "png";
  119. $logo = imagecreatefrompng($this->indiaimage);
  120. break;
  121. }
  122. imagealphablending($im, true); //打开混色模式
  123. imagecopy($im, $logo, $this->indiaimagex, $this->indiaimagey, 0, 0, $logow, $logoh);
  124. imagedestroy($im);
  125. imagedestroy($logo);
  126. }
  127. }
  128. if ($this->indiapath == "") { //如果水印存放地址不为空
  129. if ($filetype == "jpg") {
  130. imagejpeg($im, $filename);
  131. } else
  132. if ($filetype == "gif") {
  133. imagegif($im, $filename);
  134. } else
  135. if ($filetype == "png") {
  136. imagepng($im, $filename);
  137. }
  138. if($reimage == true){
  139. $this->goreimagesize($filename,$filetype);
  140. }else{
  141. return true; //添加水印成功
  142. }
  143. } else {
  144. if (!file_exists($this->indiapath)) {
  145. mkdir($this->indiapath);
  146. return false; //请重新上传
  147. } else {
  148. $indianame = basename($filename);
  149. $indianame = $this->indiapath . $indianame;
  150. if ($filetype == "jpg") {
  151. imagejpeg($im, $indianame);
  152. } else
  153. if ($filetype == "gif") {
  154. imagegif($im, $indianame);
  155. } else
  156. if ($filetype == "png") {
  157. imagepng($im, $indianame);
  158. }
  159. if($reimage == true){
  160. $this->goreimagesize($indianame,$filetype);
  161. echo $indianame;
  162. }else{
  163. return true; //添加水印成功
  164. }
  165. }
  166. }
  167. }
  168. function goreimagesize($filename, $filetype) {
  169. if (!file_exists($filename)) {
  170. return false; //要生成缩略图的图片不存在
  171. } else {
  172. if ($filetype == 'jpg') {
  173. $reimage = imagecreatefromjpeg($filename);
  174. }
  175. elseif ($filetype == 'png') {
  176. $reimage = imagecreatefrompng($filename);
  177. } else
  178. if ($filetype == 'gif') {
  179. $reimage = imagecreatefromgif($filename);
  180. }
  181. if (isset ($reimage)) {
  182. $srcimagetype = getimagesize($filename);
  183. $srcimagetypew = $srcimagetype[0]; //得到原始图片宽度
  184. $srcimagetypeh = $srcimagetype[1]; //得到原始图片高度
  185. $reim = imagecreatetruecolor($this->reimagesize[1], $this->reimagesize[2]);
  186. imagecopyresized($reim, $reimage, 0, 0, 0, 0, $this->reimagesize[1], $this->reimagesize[2], $srcimagetypew, $srcimagetypeh);
  187. $reimagepath = $this->reimagesize[3];
  188. if ($reimagepath != "") { //如果存放水印地址不为空
  189. if (!file_exists($reimagepath)) {
  190. mkdir($reimagepath);
  191. } else {
  192. $reimagename = basename($filename);
  193. $reimagename = $reimagepath . "r_" . $reimagename;
  194. if ($filetype == "gif")
  195. imagegif($reim, $reimagename);
  196. else
  197. if ($filetype == "jpg")
  198. imagejpeg($reim, $reimagename);
  199. else
  200. if ($filetype == "png")
  201. imagepng($reim, $reimagename);
  202. return true;
  203. }
  204. } else {
  205. $filename = basename($filename);
  206. if($this->indiapath == ""){
  207. $filename = $this->filepath."r_" . $filename;
  208. }else{
  209. $filename = $this->indiapath."r_" . $filename;
  210. }
  211. if ($filetype == "gif")
  212. imagegif($reim, $filename);
  213. else
  214. if ($filetype == "jpg")
  215. imagejpeg($reim, $filename);
  216. else
  217. if ($filetype == "png")
  218. imagepng($reim, $filename);
  219. return true;
  220. }
  221. }
  222. }
  223. }
  224. }
  225. if ($_post["submit"]) {
  226. $file = $_files['uploadfile'];
  227. $upfile = new upfile();
  228. echo $upfile->uploadfile($file);
  229. }//开源代码phpfensi.com
  230. ?>
  231. ""
    action= method="post" enctype="multipart/form-data">
  232. "file" type= name="uploadfile"/>
  233. "submit" type= value="上传" name="submit"/>