Discuz教程网

PHP图片上传存储源码并且可以预览

[复制链接]
authicon dly 发表于 2011-8-31 10:46:35 | 显示全部楼层 |阅读模式
代码如下:

  1. <?php
  2. header("content-Type: text/html; charset=gb2312");
  3. $uptypes=array('image/jpg', //上传文件类型列表
  4. 'image/jpeg',
  5. 'image/png',
  6. 'image/pjpeg',
  7. 'image/gif',
  8. 'image/bmp',
  9. 'application/x-shockwave-flash',
  10. 'image/x-png',
  11. 'application/msword',
  12. 'audio/x-ms-wma',
  13. 'audio/mp3',
  14. 'application/vnd.rn-realmedia',
  15. 'application/x-zip-compressed',
  16. 'application/octet-stream');
  17. $max_file_size=10000000; //上传文件大小限制, 单位BYTE
  18. $path_parts=pathinfo($_SERVER['PHP_SELF']); //取得当前路径
  19. $destination_folder="up/"; //上传文件路径
  20. $watermark=0; //是否附加水印(1为加水印,0为不加水印);
  21. $watertype=1; //水印类型(1为文字,2为图片)
  22. $waterposition=2; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
  23. $waterstring="www.yinao.tk"; //水印字符串
  24. $waterimg="xplore.gif"; //水印图片
  25. $imgpreview=1; //是否生成预览图(1为生成,0为不生成);
  26. $imgpreviewsize=1/1; //缩略图比例
  27. ?>
  28. <html xmlns="undefined">
  29. <head>
  30. <title>图片上传储存</title>
  31. <LINK rel="Bookmark" href="http://www.yinao.tk/ico.ico" >
  32. <LINK rel="Shortcut Icon" href="http://www.yinao.tk/ico.ico" />
  33. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  34. <meta name="description" content="亿脑网盘_免费网络无限大图片上传储存" />
  35. <style type="text/css">
  36. body,td{font-family:tahoma,verdana,arial;font-size:11px;line-height:15px;background-color:white;color:#666666;
  37. strong{font-size:12px;}
  38. a:link{color:#0066CC;}
  39. a:hover{color:#FF6600;}
  40. a:visited{color:#003366;}
  41. a:active{color:#9DCC00;}
  42. a{TEXT-DECORATION:none}
  43. td.irows{height:20px;background:url("index.php?i=dots") repeat-x bottom}
  44. </style>
  45. </head>
  46. <script type="text/javascript">function oCopy(obj){obj.select();js=obj.createTextRange();js.execCommand("Copy");};function sendtof(url){window.clipboardData.setData('Text',url);alert('复制地址成功,粘贴给你好友一起分享。');};function select_format(){var on=document.getElementById('fmt').checked;document.getElementById('site').style.display=on?'none':'';document.getElementById('sited').style.display=!on?'none':'';};var flag=false;function DrawImage(ImgD){var image=new Image();image.src=ImgD.src;if(image.width>0&&image.height>0){flag=true;if(image.width/image.height>=120/80){if(image.width>120){ImgD.width=120;ImgD.height=(image.height*120)/image.width;}else {ImgD.width=image.width;ImgD.height=image.height;};ImgD.alt=image.width+"×"+image.height;}else {if(image.height>80){ImgD.height=80;ImgD.width=(image.width*80)/image.height;}else {ImgD.width=image.width;ImgD.height=image.height;};ImgD.alt=image.width+"×"+image.height;}};};function FileChange(Value){flag=false;document.all.uploadimage.width=10;document.all.uploadimage.height=10;document.all.uploadimage.alt="";document.all.uploadimage.src=Value;};</script>
  47. <body bgcolor="#FFFFFF">
  48. <center>
  49. <form enctype="multipart/form-data" method="post" name="upform">
  50. <table border="1" width="55%" id="table1" cellspacing=0>
  51. <tr>
  52. <td colspan="2"><p align="center">最大文件限制1M </td>
  53. </tr>
  54. <tr>
  55. <td width="10%"><div style="width:120px; height:80px;overflow:hidden;text-align: center;" ><IMG id=uploadimage height=0 width=0 src="" onload="javascript:DrawImage(this);" ></div></td>
  56. <td width="71%"><div style="width:361px; height:80px;overflow:hidden;text-align: center;padding:30px; " >
  57. <input style="width:208;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff; height:18" size="17" name=upfile type=file
  58. onchange="javascript:FileChange(this.value);">
  59. <input type="submit" value="上传" style="width:60;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff; height:18" size="17"></td>
  60. </tr>
  61. </table>
  62. 允许上传的文件类型为:jpg|jpeg|gif|bmp|png|swf|mp3|wma|zip|rar|doc</form>
  63. <?php
  64. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  65. {
  66. if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
  67. //是否存在文件
  68. {
  69. echo "<font color='red'>文件不存在!</font>";
  70. exit;
  71. }
  72. $file = $_FILES["upfile"];
  73. if($max_file_size < $file["size"])
  74. //检查文件大小
  75. {
  76. echo "<font color='red'>文件太大!</font>";
  77. exit;
  78. }
  79. if(!in_array($file["type"], $uptypes))
  80. //检查文件类型
  81. {
  82. echo "<font color='red'>不能上传此类型文件!</font>";
  83. exit;
  84. }
  85. if(!file_exists($destination_folder))
  86. mkdir($destination_folder);
  87. $filename=$file["tmp_name"];
  88. $image_size = getimagesize($filename);
  89. $pinfo=pathinfo($file["name"]);
  90. $ftype=$pinfo[extension];
  91. $destination = $destination_folder.time().".".$ftype;
  92. if (file_exists($destination) && $overwrite != true)
  93. {
  94. echo "<font color='red'>同名文件已经存在了!</a>";
  95. exit;
  96. }
  97. if(!move_uploaded_file ($filename, $destination))
  98. {
  99. echo "<font color='red'>移动文件出错!</a>";
  100. exit;
  101. }
  102. $pinfo=pathinfo($destination);
  103. $fname=$pinfo[basename];
  104. echo " <font color=red>成功上传,鼠标移动到地址栏自动复制</font><br><table width="348" cellspacing="0" cellpadding="5" border="0" class="table_decoration" align="center"><tr><td><input type="checkbox" id="fmt" onclick="select_format()"/>图片UBB代码<br/><div id="site"><table border="0"><tr><td valign="top">文件地址:</td><td><input type="text" onclick="sendtof(this.value)" onmouseover="oCopy(this)" style=font-size=9pt;color:blue size="44" value="http://".$_SERVER['SERVER_NAME'].$path_parts["dirname"]."/".$destination_folder.$fname.""/>
  105. </td></tr></table></div><div id="sited" style="display:none"><table border="0"><tr><td valign="top">文件地址:</td><td><input type="text" onclick="sendtof(this.value)" onmouseover="oCopy(this)" style=font-size=9pt;color:blue size="44" value="[img]http://".$_SERVER['SERVER_NAME'].$path_parts["dirname"]."/".$destination_folder.$fname."[/img]"/></td></tr></table></div></td></tr></table>";
  106. echo " 宽度:".$image_size[0];
  107. echo " 长度:".$image_size[1];
  108. if($watermark==1)
  109. {
  110. $iinfo=getimagesize($destination,$iinfo);
  111. $nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
  112. $white=imagecolorallocate($nimage,255,255,255);
  113. $black=imagecolorallocate($nimage,0,0,0);
  114. $red=imagecolorallocate($nimage,255,0,0);
  115. imagefill($nimage,0,0,$white);
  116. switch ($iinfo[2])
  117. {
  118. case 1:
  119. $simage =imagecreatefromgif($destination);
  120. break;
  121. case 2:
  122. $simage =imagecreatefromjpeg($destination);
  123. break;
  124. case 3:
  125. $simage =imagecreatefrompng($destination);
  126. break;
  127. case 6:
  128. $simage =imagecreatefromwbmp($destination);
  129. break;
  130. default:
  131. die("<font color='red'>不能上传此类型文件!</a>");
  132. exit;
  133. }
  134. imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
  135. imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);
  136. switch($watertype)
  137. {
  138. case 1: //加水印字符串
  139. imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
  140. break;
  141. case 2: //加水印图片
  142. $simage1 =imagecreatefromgif("xplore.gif");
  143. imagecopy($nimage,$simage1,0,0,0,0,85,15);
  144. imagedestroy($simage1);
  145. break;
  146. }
  147. switch ($iinfo[2])
  148. {
  149. case 1:
  150. //imagegif($nimage, $destination);
  151. imagejpeg($nimage, $destination);
  152. break;
  153. case 2:
  154. imagejpeg($nimage, $destination);
  155. break;
  156. case 3:
  157. imagepng($nimage, $destination);
  158. break;
  159. case 6:
  160. imagewbmp($nimage, $destination);
  161. //imagejpeg($nimage, $destination);
  162. break;
  163. }
  164. //覆盖原上传文件
  165. imagedestroy($nimage);
  166. imagedestroy($simage);
  167. }
  168. if($imgpreview==1)
  169. {
  170. echo "<br>图片预览:<br>";
  171. echo "<a href="".$destination."" target='_blank'><img src="".$destination."" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
  172. echo " alt="图片预览:\r文件名:".$fname."\r上传时间:".date('m/d/Y h:i')."" border='0'></a>";
  173. }
  174. }
  175. ?>
  176. </center>
  177. </body>
  178. </html>
复制代码







上一篇:PHP url 加密解密函数代码
下一篇:PHP 无限分类三种方式 非函数的递归调用!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 07:02

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表