├── Image.php ├── README.md └── test.php /Image.php: -------------------------------------------------------------------------------- 1 | initialize($config); 29 | } 30 | 31 | // 初始化配置 32 | public function initialize($config) 33 | { 34 | $this->clear(); // 清除之前的配置 35 | foreach ($config as $key => $val) { 36 | if (isset($this->$key)) { 37 | $this->$key = $val; 38 | } 39 | } 40 | } 41 | 42 | // 清除配置 43 | public function clear() 44 | { 45 | $this->source_image = ''; 46 | $this->width = ''; 47 | $this->height = ''; 48 | $this->create_thumb = false; 49 | $this->thumb_marker = '_thumb'; 50 | } 51 | 52 | // 等比缩放 53 | public function resize() 54 | { 55 | $source_path = $this->source_image; 56 | $target_width = $this->width; 57 | $target_height = $this->height; 58 | list($source_width, $source_height) = $imagesize = getimagesize($source_path); 59 | $source_mime = $imagesize['mime']; 60 | switch ($source_mime) { 61 | case 'image/gif': 62 | $source_func = 'imagecreatefromgif'; 63 | $output_func = 'imagegif'; 64 | $suffix = '.gif'; 65 | break; 66 | case 'image/png': 67 | $source_func = 'imagecreatefrompng'; 68 | $output_func = 'imagepng'; 69 | $suffix = '.png'; 70 | break; 71 | case 'image/jpeg': 72 | $source_func = 'imagecreatefromjpeg'; 73 | $output_func = 'imagejpeg'; 74 | $suffix = '.jpg'; 75 | break; 76 | default: 77 | $source_func = 'imagecreatefromjpeg'; // 兼容app, 许多app上传的图片无mime信息 78 | $output_func = 'imagejpeg'; 79 | $suffix = '.jpg'; 80 | break; 81 | } 82 | $source_image = $source_func($source_path); 83 | 84 | $width_ratio = $target_width / $source_width; 85 | $height_ratio = $target_height / $source_height; 86 | 87 | // 源图宽高均小于要设置的值 88 | if ($width_ratio >= 1 && $height_ratio >= 1) { 89 | $target_image = $source_image; 90 | } else { 91 | // 根据缩放倍率小的宽或者高缩放 92 | if ($width_ratio < $height_ratio) { 93 | $zoom_width = $target_width; 94 | $zoom_height = $source_height * ($target_width / $source_width); 95 | } else { 96 | $zoom_height = $target_height; 97 | $zoom_width = $source_width * ($target_height / $source_height); 98 | } 99 | 100 | // 声明图片资源 101 | $target_image = imagecreatetruecolor($zoom_width, $zoom_height); 102 | // 保留png透明色 103 | imagealphablending($target_image, false); 104 | imagesavealpha($target_image, true); 105 | 106 | // 缩放 107 | imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $zoom_width, $zoom_height, $source_width, $source_height); 108 | } 109 | 110 | // 图片地址为url 111 | if (strpos($source_path, 'http') !== false) { 112 | $output_func($target_image, __DIR__ . '/tmp' . $suffix); 113 | } else { 114 | if ($this->create_thumb) { 115 | $source_path = str_replace('.', $this->thumb_marker . '.', $source_path); 116 | } 117 | $output_func($target_image, $source_path); 118 | } 119 | 120 | //销毁资源 121 | imagedestroy($source_image); 122 | @imagedestroy($target_image); 123 | } 124 | 125 | // 居中剪裁 126 | public function crop() 127 | { 128 | $source_path = $this->source_image; 129 | $target_width = $this->width; 130 | $target_height = $this->height; 131 | list($source_width, $source_height) = $imagesize = getimagesize($source_path); 132 | $source_mime = $imagesize['mime']; 133 | $source_ratio = $source_height / $source_width; 134 | $target_ratio = $target_height / $target_width; 135 | if ($source_ratio > $target_ratio) { 136 | // 源图过高 137 | $cropped_width = $source_width; 138 | $cropped_height = $source_width * $target_ratio; 139 | $source_x = 0; 140 | $source_y = ($source_height - $cropped_height) / 2; 141 | } elseif ($source_ratio < $target_ratio) { 142 | // 源图过宽 143 | $cropped_width = $source_height / $target_ratio; 144 | $cropped_height = $source_height; 145 | $source_x = ($source_width - $cropped_width) / 2; 146 | $source_y = 0; 147 | } else { 148 | // 源图适中 149 | $cropped_width = $source_width; 150 | $cropped_height = $source_height; 151 | $source_x = 0; 152 | $source_y = 0; 153 | } 154 | switch ($source_mime) { 155 | case 'image/gif': 156 | $source_func = 'imagecreatefromgif'; 157 | $output_func = 'imagegif'; 158 | $suffix = '.gif'; 159 | break; 160 | case 'image/png': 161 | $source_func = 'imagecreatefrompng'; 162 | $output_func = 'imagepng'; 163 | $suffix = '.png'; 164 | break; 165 | case 'image/jpeg': 166 | $source_func = 'imagecreatefromjpeg'; 167 | $output_func = 'imagejpeg'; 168 | $suffix = '.jpg'; 169 | break; 170 | default: 171 | $source_func = 'imagecreatefromjpeg'; // 兼容app, 许多app上传的图片无mime信息 172 | $output_func = 'imagejpeg'; 173 | $suffix = '.jpg'; 174 | break; 175 | } 176 | $source_image = $source_func($source_path); 177 | 178 | // 声明图片资源 179 | $target_image = imagecreatetruecolor($target_width, $target_height); 180 | $cropped_image = imagecreatetruecolor($cropped_width, $cropped_height); 181 | // 保留png透明色 182 | imagealphablending($target_image, false); 183 | imagesavealpha($target_image, true); 184 | imagealphablending($cropped_image, false); 185 | imagesavealpha($cropped_image, true); 186 | 187 | // 裁剪 188 | imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height); 189 | // 缩放 190 | imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height); 191 | 192 | // 图片地址为url 193 | if (strpos($source_path, 'http') !== false) { 194 | $output_func($target_image, __DIR__ . '/tmp' . $suffix); 195 | } else { 196 | if ($this->create_thumb) { 197 | $source_path = str_replace('.', $this->thumb_marker . '.', $source_path); 198 | } 199 | $output_func($target_image, $source_path); 200 | } 201 | 202 | // 销毁资源 203 | imagedestroy($source_image); 204 | imagedestroy($target_image); 205 | imagedestroy($cropped_image); 206 | } 207 | 208 | } 209 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Image.php 2 | 3 | 很多PHP框架竟然没有图片设定宽高居中剪裁的功能,比如CI,所以我自己封装了一个图片处理类:可设定宽高居中剪裁、设定宽高等比缩放、创建缩略图 4 | 5 | #### 提供了以下功能 6 | 7 | 1. 居中剪裁 (设定宽高) 8 | 2. 等比缩放 (设定宽高, 源图小于宽高的不做缩放) 9 | 3. 创建缩略图 10 | 4. 如果操作网络图片, 会在根目录生成"tmp.jpg" (用于测试) 11 | 12 | ## 生成用户头像 (Sample 1) 13 | 14 | ```php 15 | $img = new Image(); 16 | 17 | $config['source_image'] = '/www/img/4533070d32960cd35e726ddb715a1eac.jpg'; 18 | $config['width'] = 200; 19 | $config['height'] = 200; 20 | $img->initialize($config); 21 | $img->crop(); // 剪裁 22 | ``` 23 | 24 | ## 创建缩略图 (Sample 2) 25 | 26 | ```php 27 | $img = new Image(); 28 | 29 | $config['source_image'] = '/www/img/4533070d32960cd35e726ddb715a1eac.jpg'; 30 | $config['width'] = 200; 31 | $config['height'] = 200; 32 | $config['create_thumb'] = true; 33 | $img->initialize($config); 34 | $img->crop(); // 剪裁 35 | ``` 36 | 37 | ## 等比压缩图片并创建缩略图 (Sample 3) 38 | 39 | ```php 40 | $img = new Image(); 41 | 42 | $config['source_image'] = '/www/img/4533070d32960cd35e726ddb715a1eac.jpg'; 43 | $config['width'] = 1280; 44 | $config['height'] = 720; 45 | $img->initialize($config); 46 | $img->resize(); // 缩放 47 | 48 | $config = array(); // 清空之前的配置 49 | 50 | $config['source_image'] = '/www/img/4533070d32960cd35e726ddb715a1eac.jpg'; 51 | $config['width'] = 600; 52 | $config['height'] = 450; 53 | $config['create_thumb'] = true; 54 | $config['thumb_marker'] = '_small'; // 默认为'_thumb' 55 | $img->initialize($config); 56 | $img->crop(); // 剪裁 57 | ``` 58 | -------------------------------------------------------------------------------- /test.php: -------------------------------------------------------------------------------- 1 | initialize([ 7 | 'source_image' => 'http://www.wallcoo.com/animal/Dogs_Summer_and_Winter/wallpapers/1920x1200/DogsB10_Lucy.jpg', 8 | 'width' => 200, 9 | 'height' => 200, 10 | ]); 11 | $image->resize(); 12 | --------------------------------------------------------------------------------