当前位置:Gxlcms > PHP教程 > php上传图片等比缩放图片的代码

php上传图片等比缩放图片的代码

时间:2021-07-01 10:21:17 帮助过:15人阅读

  1. /**

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

  137. }else{

  138. echo <<
  139. 缩放图片保存成正方形
  140. EOT;
  141. }

人气教程排行