PHP封裝的圖片大小調整類
PHP封裝的一個圖片調整類,可以輸出圖片流或者保存圖片,支持等比的寬度、高度和寬高度同時調整。代碼如下:
代碼如下:
<?php class ImageResize { private $image; private $img_des; private $image_type; private $permissions; private $compression;/* 構造函數 @param string $img_src 源圖片 @param string $img_des 保存圖片對象 @param int $compression 壓縮比率,范圍0-100(0:壓縮比最高,100:壓縮比最低) @param null $permissions 改變文件模式 mode 參數由 4 個數字組成: 第一個數字永遠是 0 第二個數字規定所有者的權限 第二個數字規定所有者所屬的用戶組的權限 第四個數字規定其他所有人的權限 1 - 執行權限,2 - 寫權限,4 - 讀權限 */ function __construct($img_src, $img_des = NULL, $compression = 75, $permissions = null) { $image_info = getimagesize($img_src);
$this->img_des = $img_des; $this->image_type = $image_info[2]; $this->compression = $compression; $this->permissions = $permissions;
if ($this->image_type == IMAGETYPE_JPEG) { $this->image = ImageCreateFromJPEG($img_src); } elseif ($this->image_type == IMAGETYPE_GIF) { $this->image = ImageCreateFromGIF($img_src); } elseif ($this->image_type == IMAGETYPE_PNG) { $this->image = ImageCreateFromPNG($img_src); } }
/* 保存圖片 */ function save() { if ($this->image_type == IMAGETYPE_JPEG) { imagejpeg($this->image, $this->img_des, $this->compression); } elseif ($this->image_type == IMAGETYPE_GIF) { imagegif($this->image, $this->img_des); } elseif ($this->image_type == IMAGETYPE_PNG) { imagepng($this->image, $this->img_des); }
if ($this->permissions != null) { chmod($this->image, $this->compression); } }
/* 做為圖片流直接輸出 */ function output() { if ($this->image_type == IMAGETYPE_JPEG) { header('Content-Type: image/jpg'); imagejpeg($this->image); } elseif ($this->image_type == IMAGETYPE_GIF) { header('Content-Type: image/gif'); imagegif($this->image); } elseif ($this->image_type == IMAGETYPE_PNG) { header('Content-Type: image/png'); imagepng($this->image); } }
/* 獲取圖片寬度 @return int 圖片寬度 / function getWidth() { return imagesx($this->image); }
/ 獲取圖片高度 @return int 圖片高度 / function getHeight() { return imagesy($this->image); }
/* 按照固定高度縮放圖片 @param $height 需要改變大小的高度 / function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width, $height); }
/* 按照固定寬度縮放圖片 @param $width 指定寬度 / function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width, $height); }
/* 等比縮放圖片 @param int $scale 縮放比例 / function scale($scale) { $width = $this->getWidth() $scale / 100; $height = $this->getheight() $scale / 100; $this->resize($width, $height); }
/* 指定寬度和高度縮放圖片 @param int $width 縮放寬度 @param int $height 縮放高度 @return 縮放后圖片對象 / function resize($width, $height) { $new_image = imagecreatetruecolor($width, $height);
if ($this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG) { $current_transparent = imagecolortransparent($this->image); if ($current_transparent != -1) { $transparent_color = imagecolorsforindex($this->image, $current_transparent); $current_transparent = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); imagefill($new_image, 0, 0, $current_transparent); imagecolortransparent($new_image, $current_transparent); } elseif ($this->image_type == IMAGETYPE_PNG) { imagealphablending($new_image, false); $color = imagecolorallocatealpha($new_image, 0, 0, 0, 127); imagefill($new_image, 0, 0, $color); imagesavealpha($new_image, true); } }
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } }
?></pre>
使用:
$image = new ImageResize($tmp_image_name, NULL, 100); $image->output();來自:http://my.oschina.net/u/437511/blog/183055