Php上传、生成缩略图、生成文字水印和图片水印

  1. <?php
  2. /*
  3. '-##########################################-|
  4. '| 程序设计:(绿竹居) |
  5. '| MSN:[email]lzj_zhangjun@hotmail.com[/email] |
  6. '| Email:cszjun@gmail.com |
  7. '| 结合自己以前做的上传和生成缩略和改编一个
  8. '| 老前辈的生成图片水印做成这个 |
  9. '| 上传+生成缩略图+生成文字水印和图片水印 |
  10. '| 可以直接引用 |
  11. '-##########################################-|
  12. */
  13. //审明图片文件夹
  14. $bigfolder="img";
  15. $smallfolder="smallimg";
  16. //审明文件夹名称 以年月日来建议文件夹
  17. $fdate=date("Ymd");
  18. //echo $fdate;
  19. //审明文件名称。以年月日时分秒命名
  20. $fname=date("YmdHis");
  21. $bigname="0724e_com_".$fname;
  22. $smallname="0724e_com_".$fname."_s";
  23. //echo $fname."<br/>".$bigname."<br/>".$smallname;
  24. //确定大小文件夹的名称和路经
  25. $bigaddrname=$bigfolder."/".$fdate."/".$bigname;
  26. $smalladdrname=$smallfolder."/".$fdate."/".$smallname;
  27. //审明小图片的高度和宽度
  28. $RESIZEWIDTH=180;
  29. $RESIZEHEIGHT=150;
  30. //审明水印的文字或图片的地址及字体的地址
  31. $imgaddr="img.jpg"; //图片水印
  32. $fontname="www.0724e.com"; //文字水印
  33. //充许上传的文件扩展名
  34. $exit_name=array(".jpg",".gif",".png");
  35. if (isset($_POST['Submit'])){
  36. //上传部分----------------------------
  37. //定议上传名称和上传错误
  38. $upfile=$_FILES['image']['name'];
  39. $uperror=$_FILES['image']['error'];
  40. //最简表单验证
  41. switch ($uperror) {
  42. case 1:
  43. die("<div align=\"center\">上传真过PHP设置充许的最大值<a href=\"java script:history.back();\">点击返回</a></div>");
  44. break;
  45. case 2:
  46. die("<div align=\"center\">上传超过了表单充许的最大值<a href=\"java script:history.back();\">点击返回</a></div>");
  47. break;
  48. case 3:
  49. die("<div align=\"center\">文件只有部分被上传了<a href=\"java script:history.back();\">点击返回</a></div>");
  50. break;
  51. case 4:
  52. die("<div align=\"center\">文件不有被上传<a href=\"java script:history.back();\">点击返回</a></div>");
  53. break;
  54. }
  55. //检测扩展是否是充许上传的文件类型
  56. //取得文件扩展名
  57. $exname=strrchr($upfile,".");
  58. //判断取得扩展名是否在要求的扩展名内
  59. if(!in_array($exname,$exit_name))
  60. die("<div align=\"center\">文件扩展名不对。只允许上传jpg gif png的图片<a href=\"java script:history.back();\">点击返回</a></div>");
  61. //检测存放图片的目录和子目录是否存在,如果不存在则建目录和子目录,并给目录最大权限777 对LINUX或unix对WINDOWS没必要
  62. //大图
  63. if (!file_exists($bigfolder)){
  64. mkdir($bigfolder,0777);
  65. mkdir($bigfolder."/".$fdate,0777);
  66. }else{
  67. if (!file_exists($bigfolder."/".$fdate)){
  68. mkdir($bigfolder."/".$fdate,0777);
  69. }
  70. }
  71. //缩略图
  72. if (!file_exists($smallfolder)){
  73. mkdir($smallfolder,0777);
  74. mkdir($smallfolder."/".$fdate,0777);
  75. }else{
  76. if (!file_exists($smallfolder."/".$fdate)){
  77. mkdir($smallfolder."/".$fdate,0777);
  78. }
  79. }
  80. //创建目录结束
  81. //得到临时上传的文件
  82. $upfiletmp=$_FILES['image']['tmp_name'];
  83. //判断临时文件是否存在
  84. if ($uperror==6)
  85. die("<div align=\"center\">找不到临时上传文件,上传失败<a href=\"java script:history.back();\">点击返回</a></div>");
  86. //水印部分==========================================
  87. //水印函数开始============
  88. /** $groundImage 背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
  89. * $waterPos 水印位置,有10种状态,0为随机位置;
  90. * 1为顶端居左,2为顶端居中,3为顶端居右;
  91. * 4为中部居左,5为中部居中,6为中部居右;
  92. * 7为底端居左,8为底端居中,9为底端居右;
  93. * $waterImage 图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
  94. * $waterText 文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
  95. * $textFont 文字大小,值为1、2、3、4或5,默认为5;
  96. * $textColor 文字颜色,值为十六进制颜色值,默认为#FF0000(红色);
  97. * */
  98. function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="", $textFont=5,$textColor="#FF0000")
  99. {
  100. $isWaterImage = FALSE;
  101. $formatMsg = "暂不支持该文件格式,请用图片处理软件将图片转换为GIF、JPG、PNG格式。";
  102. //读取水印文件
  103. if(!emptyempty($waterImage) && file_exists($waterImage)) {
  104. $isWaterImage = TRUE;
  105. $water_info = getimagesize($waterImage);
  106. $water_w = $water_info[0];//取得水印图片的宽
  107. $water_h = $water_info[1];//取得水印图片的高
  108. switch($water_info[2]) { //取得水印图片的格式
  109. case 1:$water_im = imagecreatefromgif($waterImage);break;
  110. case 2:$water_im = imagecreatefromjpeg($waterImage);break;
  111. case 3:$water_im = imagecreatefrompng($waterImage);break;
  112. default:die($formatMsg);
  113. }
  114. }
  115. //读取背景图片
  116. if(!emptyempty($groundImage) && file_exists($groundImage)) {
  117. $ground_info = getimagesize($groundImage);
  118. $ground_w = $ground_info[0];//取得背景图片的宽
  119. $ground_h = $ground_info[1];//取得背景图片的高
  120. switch($ground_info[2]) { //取得背景图片的格式
  121. case 1:$ground_im = imagecreatefromgif($groundImage);break;
  122. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  123. case 3:$ground_im = imagecreatefrompng($groundImage);break;
  124. default:die($formatMsg);
  125. }
  126. } else {
  127. die("需要加水印的图片不存在!");
  128. }
  129. //水印位置
  130. if($isWaterImage) { //图片水印
  131. $w = $water_w;
  132. $h = $water_h;
  133. $label = "图片的";
  134. } else { //文字水印
  135. $temp = imagettfbbox(ceil($textFont*2.5),0,"./cour.ttf",$waterText);//取得使用 TrueType 字体的文本的范围
  136. $w = $temp[2] - $temp[6];
  137. $h = $temp[3] - $temp[7];
  138. unset($temp);
  139. $label = "文字区域";
  140. }
  141. if( ($ground_w<$w) || ($ground_h<$h) ) {
  142. echo "需要加水印的图片的长度或宽度比水印".$label."还小,无法生成水印!";
  143. return;
  144. }
  145. switch($waterPos) {
  146. case 0://随机
  147. $posX = rand(0,($ground_w - $w));
  148. $posY = rand(0,($ground_h - $h));
  149. break;
  150. case 1://1为顶端居左
  151. $posX = 0;
  152. $posY = 0;
  153. break;
  154. case 2://2为顶端居中
  155. $posX = ($ground_w - $w) / 2;
  156. $posY = 0;
  157. break;
  158. case 3://3为顶端居右
  159. $posX = $ground_w - $w;
  160. $posY = 0;
  161. break;
  162. case 4://4为中部居左
  163. $posX = 0;
  164. $posY = ($ground_h - $h) / 2;
  165. break;
  166. case 5://5为中部居中
  167. $posX = ($ground_w - $w) / 2;
  168. $posY = ($ground_h - $h) / 2;
  169. break;
  170. case 6://6为中部居右
  171. $posX = $ground_w - $w;
  172. $posY = ($ground_h - $h) / 2;
  173. break;
  174. case 7://7为底端居左
  175. $posX = 0;
  176. $posY = $ground_h - $h;
  177. break;
  178. case 8://8为底端居中
  179. $posX = ($ground_w - $w) / 2;
  180. $posY = $ground_h - $h;
  181. break;
  182. case 9://9为底端居右
  183. $posX = $ground_w - $w;
  184. $posY = $ground_h - $h;
  185. break;
  186. default://随机
  187. $posX = rand(0,($ground_w - $w));
  188. $posY = rand(0,($ground_h - $h));
  189. break;
  190. }
  191. //设定图像的混色模式
  192. imagealphablending($ground_im, true);
  193. if($isWaterImage) { //图片水印
  194. imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
  195. } else {//文字水印
  196. if( !emptyempty($textColor) && (strlen($textColor)==7) ) {
  197. $R = hexdec(substr($textColor,1,2));
  198. $G = hexdec(substr($textColor,3,2));
  199. $B = hexdec(substr($textColor,5));
  200. } else {
  201. die("水印文字颜色格式不正确!");
  202. }
  203. imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  204. }
  205. //生成水印后的图片
  206. @unlink($groundImage);
  207. switch($ground_info[2]) {//取得背景图片的格式
  208. case 1:imagegif($ground_im,$groundImage);break;
  209. case 2:imagejpeg($ground_im,$groundImage);break;
  210. case 3:imagepng($ground_im,$groundImage);break;
  211. default:die($errorMsg);
  212. }
  213. //释放内存
  214. if(isset($water_info)) unset($water_info);
  215. if(isset($water_im)) imagedestroy($water_im);
  216. unset($ground_info);
  217. imagedestroy($ground_im);
  218. }
  219. //水印函数完
  220. //存在则移动完在上传
  221. $goodupfile=@move_uploaded_file($upfiletmp,$bigaddrname.$exname);
  222. if (!$goodupfile){
  223. die("<div align=\"center\">上传图片失败<a href=\"java script:history.back();\">点击返回</a></div>");
  224. }else{
  225. //文字水印
  226. imageWaterMark($bigaddrname.$exname,5,"",$fontname,5,"#FF0000");
  227. //图片水印
  228. //imageWaterMark($bigaddrname.$exname,5,$imgaddr);
  229. //结束水印部分===================================
  230. //缩略图部分------------------------------------------------------------
  231. //判断缩略图大小函数-----
  232. function ResizeImage($im,$maxwidth,$maxheight,$name){
  233. $width = imagesx($im);
  234. $height = imagesy($im);
  235. if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
  236. if($maxwidth && $width > $maxwidth){
  237. $widthratio = $maxwidth/$width;
  238. $RESIZEWIDTH=true;
  239. }
  240. if($maxheight && $height > $maxheight){
  241. $heightratio = $maxheight/$height;
  242. $RESIZEHEIGHT=true;
  243. }
  244. if($RESIZEWIDTH && $RESIZEHEIGHT){
  245. if($widthratio < $heightratio){
  246. $ratio = $widthratio;
  247. }else{
  248. $ratio = $heightratio;
  249. }
  250. }elseif($RESIZEWIDTH){
  251. $ratio = $widthratio;
  252. }elseif($RESIZEHEIGHT){
  253. $ratio = $heightratio;
  254. }
  255. $newwidth = $width * $ratio;
  256. $newheight = $height * $ratio;
  257. if(function_exists("imagecopyresampled")){
  258. $newim = imagecreatetruecolor($newwidth, $newheight);
  259. imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  260. }else{
  261. $newim = imagecreate($newwidth, $newheight);
  262. imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  263. }
  264. ImageJpeg ($newim,$smalladdrname.$name.".jpg");
  265. ImageDestroy ($newim);
  266. }else{
  267. ImageJpeg ($im,$smalladdrname.$name.".jpg");
  268. }
  269. }
  270. //生成部分
  271. if($_FILES['image']['size']){
  272. if($_FILES['image']['type'] == "image/pjpeg"){
  273. $im = imagecreatefromjpeg($bigaddrname.$exname);
  274. }elseif($_FILES['image']['type'] == "image/x-png"){
  275. $im = imagecreatefrompng($bigaddrname.$exname);
  276. }elseif($_FILES['image']['type'] == "image/gif"){
  277. $im = imagecreatefromgif($bigaddrname.$exname);
  278. }
  279. if($im){
  280. if(file_exists($smalladdrname.".jpg")){
  281. unlink($smalladdrname.".jpg");
  282. }
  283. ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$smalladdrname);
  284. ImageDestroy ($im);
  285. }
  286. }
  287. echo "<div align='center'><a href='java script:window.history.back()'>上传成功</a></div>";
  288. }
  289. //缩略图结束-----------------------------------------------------
  290. }
  291. ?>
  292. <form action="" method="post" enctype="multipart/form-data" name="form1" >
  293. <input type="file" name="image" />
  294. <input type="hidden" name="MAX_FILE_SIZE" value="<?=1024*100?>">
  295. <input type="submit" name="Submit" value="上传图片" />
  296. </form>