PHP图片缩放函数

  1 <?php
  2 resizeImage('qm.jpg','up/1.jpg',93,94);
  3 /**
  4  * 图片生成缩略图
  5  *
  6  * @param string $img 预缩略的图片
  7  * @param string $thum_path 生成缩略图路径
  8  * @param int $max_w 缩略图最大宽度 px
  9  * @param int $max_h 缩略图最大高度 px
 10  * @return unknown
 11  */
 12 function resizeImage($img, $thum_path, $max_w, $max_h) {
 13     if (!in_array(getFileSuffix($thum_path), array('jpg', 'png', 'jpeg', 'gif'))) {
 14         return false;
 15     }
 16     if (!function_exists('ImageCreate')) {
 17         return false;
 18     }
 19 
 20     $size = chImageSize($img, $max_w, $max_h);
 21     $newwidth = $size['w'];
 22     $newheight = $size['h'];
 23     $w = $size['rc_w'];
 24     $h = $size['rc_h'];
 25     if ($w <= $max_w && $h <= $max_h) {
 26         return false;
 27     }
 28     return imageCropAndResize($img, $thum_path, 0, 0, 0, 0, $newwidth, $newheight, $w, $h);
 29 }
 30 
 31 /**
 32  * 裁剪、缩放图片
 33  *
 34  * @param string $src_image 原始图
 35  * @param string $dst_path 裁剪后的图片保存路径
 36  * @param int $dst_x 新图坐标x
 37  * @param int $dst_y 新图坐标y
 38  * @param int $src_x 原图坐标x
 39  * @param int $src_y 原图坐标y
 40  * @param int $dst_w 新图宽度
 41  * @param int $dst_h 新图高度
 42  * @param int $src_w 原图宽度
 43  * @param int $src_h 原图高度
 44  */
 45 function imageCropAndResize($src_image, $dst_path, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
 46     if (function_exists('imagecreatefromstring')) {
 47         $src_img = imagecreatefromstring(file_get_contents($src_image));
 48     } else {
 49         return false;
 50     }
 51 
 52     if (function_exists('imagecopyresampled')) {
 53         $new_img = imagecreatetruecolor($dst_w, $dst_h);
 54         imagecopyresampled($new_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
 55     } elseif (function_exists('imagecopyresized')) {
 56         $new_img = imagecreate($dst_w, $dst_h);
 57         imagecopyresized($new_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
 58     } else {
 59         return false;
 60     }
 61 
 62     switch (getFileSuffix($dst_path)) {
 63         case 'png':
 64             if (function_exists('imagepng') && imagepng($new_img, $dst_path)) {
 65                 ImageDestroy($new_img);
 66                 return true;
 67             } else {
 68                 return false;
 69             }
 70             break;
 71         case 'jpg':
 72         default:
 73             if (function_exists('imagejpeg') && imagejpeg($new_img, $dst_path)) {
 74                 ImageDestroy($new_img);
 75                 return true;
 76             } else {
 77                 return false;
 78             }
 79             break;
 80         case 'gif':
 81             if (function_exists('imagegif') && imagegif($new_img, $dst_path)) {
 82                 ImageDestroy($new_img);
 83                 return true;
 84             } else {
 85                 return false;
 86             }
 87             break;
 88     }
 89 }
 90 
 91 /**
 92  * 按比例计算图片缩放尺寸
 93  *
 94  * @param string $img 图片路径
 95  * @param int $max_w 最大缩放宽
 96  * @param int $max_h 最大缩放高
 97  * @return array
 98  */
 99 function chImageSize($img, $max_w, $max_h) {
100     $size = @getimagesize($img);
101     $w = $size[0];
102     $h = $size[1];
103     //计算缩放比例
104     @$w_ratio = $max_w / $w;
105     @$h_ratio = $max_h / $h;
106     //决定处理后的图片宽和高
107     if (($w <= $max_w) && ($h <= $max_h)) {
108         $tn['w'] = $w;
109         $tn['h'] = $h;
110     } else if (($w_ratio * $h) < $max_h) {
111         $tn['h'] = ceil($w_ratio * $h);
112         $tn['w'] = $max_w;
113     } else {
114         $tn['w'] = ceil($h_ratio * $w);
115         $tn['h'] = $max_h;
116     }
117     $tn['rc_w'] = $w;
118     $tn['rc_h'] = $h;
119     return $tn;
120 }
121 
122 
123 /**
124  * 获取文件名后缀
125  */
126 function getFileSuffix($fileName) {
127     return strtolower(pathinfo($fileName,  PATHINFO_EXTENSION));
128 }
129 
130 ?>

 

posted on 2012-10-31 17:56  S.T  阅读(224)  评论(0编辑  收藏  举报

导航