如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
调用的时候:
$image = Helper_Image::createFromFile(‘文件路径’,‘文件保存格式’);
$image->resampled(‘’, ‘’); //缩放到100px * 100PX
$image->saveAsJpeg(‘文件路径’ ,’品质(1到100)’);
下面代码中部分代码被转义,使用时自己替换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
resize($width, $height); * $image->saveAsJpeg('2.jpg'); * @endcode * * 对于上传的文件,由于其临时文件名中并没有包含扩展名。 * 因此需要采用下面的方法创建 Image 对象: * * @code php * $ext = pathinfo($_FILES['postfile']['name'], PATHINFO_EXTENSION); * $image = Image::createFromFile($_FILES['postfile']['tmp_name'], $ext); * @endcode * * @param string $filename 图像文件的完整路径 * @param string $fileext 指定扩展名 * * @return Helper_ImageGD 从文件创建的 Helper_ImageGD 对象 * @throw Q_NotImplementedException */ static function createFromFile($filename, $fileext) { echo $fileext = trim(strtolower($fileext), '.'); $ext2functions = array( 'jpg' => 'imagecreatefromjpeg', 'jpeg' => 'imagecreatefromjpeg', 'png' => 'imagecreatefrompng', 'gif' => 'imagecreatefromgif' ); if (!isset($ext2functions[$fileext])) { throw new Q_NotImplementedException(__('imagecreateform' . $fileext)); } $handle = call_user_func($ext2functions[$fileext], $filename); return new Helper_ImageGD($handle); } /** * 将 16 进制颜色值转换为 rgb 值 * * 用法: * @code php * $color = '#369'; * list($r, $g, $b) = Helper_Image::hex2rgb($color); * echo "red: {$r}, green: {$g}, blue: {$b}"; * @endcode * * @param string $color 颜色值 * @param string $default 使用无效颜色值时返回的默认颜色 * * @return array 由 RGB 三色组成的数组 */ static function hex2rgb($color, $default = 'ffffff') { $hex = trim($color, '#&Hh'); $len = strlen($hex); if ($len == 3) { $hex = "{$hex[0]}{$hex[0]}{$hex[1]}{$hex[1]}{$hex[2]}{$hex[2]}"; } elseif ($len < 6) { $hex = $default; } $dec = hexdec($hex); return array(($dec >> 16) & 0xff, ($dec >> 8) & 0xff, $dec & 0xff); } } /** * Helper_ImageGD 类封装了一个 gd 句柄,用于对图像进行操作 * * @author YuLei Liao * @version $Id: image.php 1937 2009-01-05 19:09:40Z dualface $ * @package helper */ class Helper_ImageGD { /** * GD 资源句柄 * * @var resource */ protected $_handle = null; /** * 构造函数 * * @param resource $handle GD 资源句柄 */ function __construct($handle) { $this->_handle = $handle; } /** * 析构函数 */ function __destruct() { $this->destroy(); } /** * 快速缩放图像到指定大小(质量较差) * * @param int $width 新的宽度 * @param int $height 新的高度 * * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口 */ function resize($width, $height) { if (is_null($this->_handle)) return $this; $dest = imagecreatetruecolor($width, $height); imagecopyresized($dest, $this->_handle, 0, 0, 0, 0, $width, $height, imagesx($this->_handle), imagesy($this->_handle)); imagedestroy($this->_handle); $this->_handle = $dest; return $this; } /** * 缩放图像到指定大小(质量较好,速度比 resize() 慢) * * @param int $width 新的宽度 * @param int $height 新的高度 * * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口 */ function resampled($width, $height) { if (is_null($this->_handle)) return $this; $dest = imagecreatetruecolor($width, $height); imagecopyresampled($dest, $this->_handle, 0, 0, 0, 0, $width, $height, imagesx($this->_handle), imagesy($this->_handle)); imagedestroy($this->_handle); $this->_handle = $dest; return $this; } /** * 调整图像大小,但不进行缩放操作 * * 用法: * @code php * $image->resizeCanvas($width, $height, 'top-left'); * @endcode * * $pos 参数指定了调整图像大小时,图像内容按照什么位置对齐。 * $pos 参数的可用值有: * * - left: 左对齐 * - right: 右对齐 * - center: 中心对齐 * - top: 顶部对齐 * - bottom: 底部对齐 * - top-left, left-top: 左上角对齐 * - top-right, right-top: 右上角对齐 * - bottom-left, left-bottom: 左下角对齐 * - bottom-right, right-bottom: 右下角对齐 * * 如果指定了无效的 $pos 参数,则等同于指定 center。 * * @param int $width 新的高度 * @param int $height 新的宽度 * @param string $pos 调整时图像位置的变化 * @param string $bgcolor 空白部分的默认颜色 * * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口 */ function resizeCanvas($width, $height, $pos = 'center', $bgcolor = '0xffffff') { if (is_null($this->_handle)) return $this; $dest = imagecreatetruecolor($width, $height); $sx = imagesx($this->_handle); $sy = imagesy($this->_handle); // 根据 pos 属性来决定如何定位原始图片 switch (strtolower($pos)) { case 'left': $ox = 0; $oy = ($height - $sy) / 2; break; case 'right': $ox = $width - $sx; $oy = ($height - $sy) / 2; break; case 'top': $ox = ($width - $sx) / 2; $oy = 0; break; case 'bottom': $ox = ($width - $sx) / 2; $oy = $height - $sy; break; case 'top-left': case 'left-top': $ox = $oy = 0; break; case 'top-right': case 'right-top': $ox = $width - $sx; $oy = 0; break; case 'bottom-left': case 'left-bottom': $ox = 0; $oy = $height - $sy; break; case 'bottom-right': case 'right-bottom': $ox = $width - $sx; $oy = $height - $sy; break; default: $ox = ($width - $sx) / 2; $oy = ($height - $sy) / 2; } list ($r, $g, $b) = Helper_Image::hex2rgb($bgcolor, '0xffffff'); $bgcolor = imagecolorallocate($dest, $r, $g, $b); imagefilledrectangle($dest, 0, 0, $width, $height, $bgcolor); imagecolordeallocate($dest, $bgcolor); imagecopy($dest, $this->_handle, $ox, $oy, 0, 0, $sx, $sy); imagedestroy($this->_handle); $this->_handle = $dest; return $this; } /** * 在保持图像长宽比的情况下将图像裁减到指定大小 * * crop() 在缩放图像时,可以保持图像的长宽比,从而保证图像不会拉高或压扁。 * * crop() 默认情况下会按照 $width 和 $height 参数计算出最大缩放比例, * 保持裁减后的图像能够最大程度的充满图片。 * * 例如源图的大小是 800 x 600,而指定的 $width 和 $height 是 200 和 100。 * 那么源图会被首先缩小为 200 x 150 尺寸,然后裁减掉多余的 50 像素高度。 * * 用法: * @code php * $image->crop($width, $height); * @endcode * * 如果希望最终生成图片始终包含完整图像内容,那么应该指定 $options 参数。 * 该参数可用值有: * * - fullimage: 是否保持完整图像 * - pos: 缩放时的对齐方式 * - bgcolor: 缩放时多余部分的背景色 * - enlarge: 是否允许放大 * - reduce: 是否允许缩小 * * 其中 $options['pos'] 参数的可用值有: * * - left: 左对齐 * - right: 右对齐 * - center: 中心对齐 * - top: 顶部对齐 * - bottom: 底部对齐 * - top-left, left-top: 左上角对齐 * - top-right, right-top: 右上角对齐 * - bottom-left, left-bottom: 左下角对齐 * - bottom-right, right-bottom: 右下角对齐 * * 如果指定了无效的 $pos 参数,则等同于指定 center。 * * $options 中的每一个选项都可以单独指定,例如在允许裁减的情况下将图像放到新图片的右下角。 * * @code php * $image->crop($width, $height, array('pos' => 'right-bottom')); * @endcode * * @param int $width 新的宽度 * @param int $height 新的高度 * @param array $options 裁减选项 * * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口 */ function crop($width, $height, $options = array()) { if (is_null($this->_handle)) return $this; $default_options = array( 'fullimage' => false, 'pos' => 'center', 'bgcolor' => '0xfff', 'enlarge' => false, 'reduce' => true, ); $options = array_merge($default_options, $options); // 创建目标图像 $dest = imagecreatetruecolor($width, $height); // 填充背景色 list ($r, $g, $b) = Helper_Image::hex2rgb($options['bgcolor'], '0xffffff'); $bgcolor = imagecolorallocate($dest, $r, $g, $b); imagefilledrectangle($dest, 0, 0, $width, $height, $bgcolor); imagecolordeallocate($dest, $bgcolor); // 根据源图计算长宽比 $full_w = imagesx($this->_handle); $full_h = imagesy($this->_handle); $ratio_w = doubleval($width) / doubleval($full_w); $ratio_h = doubleval($height) / doubleval($full_h); if ($options['fullimage']) { // 如果要保持完整图像,则选择最小的比率 $ratio = $ratio_w < $ratio_h ? $ratio_w : $ratio_h; } else { // 否则选择最大的比率 $ratio = $ratio_w > $ratio_h ? $ratio_w : $ratio_h; } if (!$options['enlarge'] && $ratio > 1) $ratio = 1; if (!$options['reduce'] && $ratio < 1) $ratio = 1; // 计算目标区域的宽高、位置 $dst_w = $full_w * $ratio; $dst_h = $full_h * $ratio; // 根据 pos 属性来决定如何定位 switch (strtolower($options['pos'])) { case 'left': $dst_x = 0; $dst_y = ($height - $dst_h) / 2; break; case 'right': $dst_x = $width - $dst_w; $dst_y = ($height - $dst_h) / 2; break; case 'top': $dst_x = ($width - $dst_w) / 2; $dst_y = 0; break; case 'bottom': $dst_x = ($width - $dst_w) / 2; $dst_y = $height - $dst_h; break; case 'top-left': case 'left-top': $dst_x = $dst_y = 0; break; case 'top-right': case 'right-top': $dst_x = $width - $dst_w; $dst_y = 0; break; case 'bottom-left': case 'left-bottom': $dst_x = 0; $dst_y = $height - $dst_h; break; case 'bottom-right': case 'right-bottom': $dst_x = $width - $dst_w; $dst_y = $height - $dst_h; break; case 'center': default: $dst_x = ($width - $dst_w) / 2; $dst_y = ($height - $dst_h) / 2; } imagecopyresampled($dest, $this->_handle, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $full_w, $full_h); imagedestroy($this->_handle); $this->_handle = $dest; return $this; } /** * 保存为 JPEG 文件 * * @param string $filename 保存文件名 * @param int $quality 品质参数,默认为 80 * * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口 */ function saveAsJpeg($filename, $quality = 80) { imagejpeg($this->_handle, $filename, $quality); } /** * 保存为 PNG 文件 * * @param string $filename 保存文件名 * * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口 */ function saveAsPng($filename) { imagepng($this->_handle, $filename); } /** * 保存为 GIF 文件 * * @param string $filename 保存文件名 * * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口 */ function saveAsGif($filename) { imagegif($this->_handle, $filename); } /** * 销毁内存中的图像 * * @return Helper_ImageGD 返回 Helper_ImageGD 对象本身,实现连贯接口 */ function destroy() { if (!$this->_handle) { @imagedestroy($this->_handle); } $this->_handle = null; return $this; } } |