话不多说直接上代码:
<?php
class Image
{
private $src;
private $imageinfo;
private $image;
public $percent = 0.1;
public function __construct($src)
{
$this->src = $src;
}
public function openImage()
{
list($width, $height, $type, $attr) = getimagesize($this->src);
$this->imageinfo = array(
'width' => $width,
'height' => $height,
'type' => image_type_to_extension($type, false),
'attr' => $attr
);
$fun = "imagecreatefrom" . $this->imageinfo['type'];
$this->image = $fun($this->src);
}
public function thumpImage()
{
$new_width = $this->imageinfo['width'] * $this->percent;
$new_height = $this->imageinfo['height'] * $this->percent;
$image_thump = imagecreatetruecolor($new_width, $new_height);
if($this->imageinfo['type'] == "png"){
imagealphablending($image_thump, false);
imagesavealpha($image_thump, true);
$bg = imagecolorallocatealpha($image_thump, 255, 255, 255,127);
imagefill($image_thump, 0, 0, $bg);
}
imagecopyresampled($image_thump, $this->image, 0, 0, 0, 0, $new_width, $new_height, $this->imageinfo['width'], $this->imageinfo['height']);
imagedestroy($this->image);
$this->image = $image_thump;
}
public function showImage()
{
header('Content-Type: image/' . $this->imageinfo['type']);
$funcs = "image" . $this->imageinfo['type'];
$funcs($this->image);
}
public function saveImage($dstImgName)
{
if(empty($dstImgName)) return false;
$funcs = "image".$this->imageinfo['type'];
$allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif'];
$dstExt = strrchr($dstImgName ,".");
$sourseExt = strrchr($this->src ,".");
if(!empty($dstExt)) $dstExt =strtolower($dstExt);
if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);
if(!empty($dstExt) && in_array($dstExt,$allowImgs)){
$dstName = $dstImgName;
}elseif(!empty($sourseExt) && in_array($sourseExt,$allowImgs)){
$dstName = $dstImgName.$sourseExt;
}else{
$dstName = $dstImgName.$this->imageinfo['type'];
}
if($this->imageinfo['type'] == "png"){
$funcs($this->image,$dstName,9);
}elseif ($this->imageinfo['type'] == "jpeg"){
$funcs($this->image,$dstName,25);
}else{
$funcs($this->image,$dstName);
}
}
public function __destruct()
{
imagedestroy($this->image);
}
}
|