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

php中文网
发布: 2016-07-25 08:51:48
原创
1072人浏览过
  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 "生成后的图片为:php上传图片等比缩放图片的代码 ";
  129. }else{
  130. echo $_files['img']['error'];
  131. return;
  132. }
  133. }else{
  134. echo "图片格式不正确,请上传jpg,gif,png的格式!";
  135. return;
  136. }
  137. }else{

    Cutout老照片上色
    Cutout老照片上色

    Cutout.Pro推出的黑白图片上色

    Cutout老照片上色 20
    查看详情 Cutout老照片上色
  138. echo
  139. 缩放图片保存成正方形
  • EOT;
  • }
  • 复制代码


  • PHP速学教程(入门到精通)
    PHP速学教程(入门到精通)

    PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

    下载
    来源:php中文网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    开源免费商场系统广告
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号