最近用PHP做项目,参考了网上的一些资源封装了一个PHP图片调整类,支持输出图片流或者保存图片,等比的宽度、高度和宽高度同时调整,代码如下:
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; }}?>
使用:
$image = new ImageResize($tmp_image_name, NULL, 100);$image->output();