用php和imagemagick来处理图片文件的上传和缩放处理

时间:2023-03-09 13:00:15
用php和imagemagick来处理图片文件的上传和缩放处理

啥也不说,直接上代码,大家可以自行添加增加水印功能:

  1. <?php
  2. /**
  3. *
  4. * @author zhao jinhan
  5. * @date 2014年1月13日11:54:30
  6. * @email xb_zjh@126.com
  7. *
  8. */
  9. header('Content-type:text/html; charset=utf-8');
  10. //定义缩略图的宽高
  11. define('THUMB_WIDTH',300);
  12. define('THUMB_HEIGHT',300);
  13. /**
  14. * 重新生成上传的文件名
  15. * @return string
  16. * @author zhao jinhan
  17. *
  18. */
  19. function _file_type($filetype = null){
  20. switch($filetype)
  21. {
  22. case "image/jpeg":
  23. $fileextname  = "jpg";
  24. break;
  25. case "image/gif":
  26. $fileextname  = "gif";
  27. break;
  28. case "image/png":
  29. $fileextname  =  "png";
  30. break;
  31. default:
  32. $fileextname = false;
  33. break;
  34. }
  35. return $fileextname?date('YmdHis',time()).'.'.$fileextname:false;
  36. }
  37. /**
  38. *
  39. * @param string $filename
  40. * @param string $width
  41. * @param string $height
  42. * @param string $quality
  43. * @param string $savepath
  44. * @return boolean
  45. */
  46. function _make_thumb($filename='', $width=THUMB_WIDTH, $height=THUMB_HEIGHT, $savepath='./upload'){
  47. if(file_exists($filename)){
  48. //上传图片的尺寸
  49. $imagesize=getimagesize($filename);
  50. $imagewidth=$imagesize[0];
  51. $imageheight=$imagesize[1];
  52. $mime = $imagesize['mime'];
  53. //宽高比例
  54. $ratio = $imagewidth/$imageheight;
  55. //新建一个背景图片
  56. $bgimg = imagecreatetruecolor($width, $height);
  57. $white = imagecolorallocate($bgimg, 255, 255, 255);
  58. //填充背景色为白色
  59. imagefill($bgimg,0,0,$white);
  60. if($mime == 'image/gif'){
  61. $im = @imagecreatefromgif($filename); /* Attempt to open */
  62. $outfun = 'imagegif';
  63. }elseif($mime == 'image/png'){
  64. $im = @imagecreatefrompng($filename); /* Attempt to open */
  65. $outfun = 'imagepng';
  66. }else{
  67. $im = @imagecreatefromjpeg($filename); /* Attempt to open */
  68. $outfun = 'imagejpeg';
  69. }
  70. if($ratio > 1){
  71. //宽度较大
  72. if($imagewidth > $width){
  73. //缩放图片到背景图片上
  74. $new_width = $width;
  75. $new_height = ($width*$imageheight)/$imagewidth;
  76. $bg_y = ceil(abs(($height-$new_height)/2));
  77. imagecopyresampled($bgimg, $im, 0, $bg_y, 0, 0, $new_width, $new_height, $imagewidth, $imageheight);
  78. }else{
  79. //复制图片到背景图片上
  80. $copy = true;
  81. }
  82. }else{
  83. //高度较大
  84. if($imageheight > $height){
  85. //缩放图片
  86. $new_height = $height;
  87. $new_width = ($height*$imagewidth)/$imageheight;
  88. $bg_x = ceil(($width-$new_width)/2);
  89. imagecopyresampled($bgimg, $im, $bg_x, 0, 0, 0, $new_width, $new_height, $imagewidth, $imageheight);
  90. }else{
  91. //复制图片到背景图片上
  92. $copy = true;
  93. }
  94. }
  95. if($copy){
  96. //复制图片到背景图片上
  97. $bg_x = ceil(($width-$imagewidth)/2);
  98. $bg_y = ceil(($height-$imageheight)/2);
  99. imagecopy($bgimg, $im, $bg_x, $bg_y, 0, 0, $imagewidth, $imageheight);
  100. }
  101. $ext = _file_type($mime);
  102. $outfun($bgimg, $savepath.'/'.$ext);
  103. imagedestroy($bgimg);
  104. return $savepath.'/'.$ext;
  105. }else{
  106. return false;
  107. }
  108. }
  109. if($_POST){
  110. $size = $_POST['size']?strtoupper(trim($_POST['size'])):'2M';
  111. $imgsize = $_FILES['img']['size']?$_FILES['img']['size']/(1024*1024):0;
  112. $imgwidth = $imgheight = $_POST['width-height']?intval($_POST['width-height']):300;
  113. //自定定义文件上传大小
  114. ini_set('upload_max_filesize',$size);
  115. $mathsize = str_replace('M','',$size);
  116. if($imgsize>$mathsize){
  117. echo "图片大小不得超过{$size}!";
  118. return;
  119. }
  120. if($file_name = _file_type($_FILES['img']['type'])){
  121. if($_FILES['img']['error'] == UPLOAD_ERR_OK){
  122. $savepath = 'upload/';
  123. if(!is_dir($savepath)){
  124. mkdir($savepath,0644);
  125. }
  126. //生成缩略图
  127. $thumb_file = _make_thumb($_FILES['img']['tmp_name'], $imgwidth, $imgheight, $savepath);
  128. //move_uploaded_file($_FILES['img']['tmp_name'],$savepath.$file_name);
  129. echo "生成后的图片为:<img src='".$thumb_file."' />";
  130. }else{
  131. echo $_FILES['img']['error'];
  132. return;
  133. }
  134. }else{
  135. echo "图片格式不正确,请上传jpg,gif,png的格式!";
  136. return;
  137. }
  138. }else{
  139. echo <<<EOT
  140. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  141. <html>
  142. <head>
  143. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  144. <title>缩放图片保存成正方形</title>
  145. </head>
  146. <body>
  147. <form action="" method="POST" enctype="multipart/form-data">
  148. <div>
  149. <label>上传一张图片:</label>
  150. <input type="file" name="img" />
  151. </div>
  152. <div>
  153. <label>生成缩略图的宽高(单位px):</label>
  154. <input type="text" name="width-height" value="300" />
  155. </div>
  156. <div>
  157. <label>文件大小上限:</label>
  158. <input type="text" name="size" value="2M" />
  159. </div>
  160. <div><input type="submit" name="submit" value="提交" /></div>
  161. </form>
  162. </body>
  163. </html>
  164. EOT;
  165. }

摘自:http://www.icode100.com/posts/view/64

本文非原创,感谢作者提供学习