/**
 * 图片工具类,支持缩略图, 获取类型等
 *

	
 *      ImgUtil::thumbnailTo("src.jpg",100,100,"src_100.jpg");
 *      ImgUtil::thumbnailBat("src.jpg",array(
 *          array("width"=>50,"height"=>50,"file"=>"src_50.jpg"),
 *          array("width"=>100,"height"=>100,"file"=>"src_100.jpg"),
 *          array("width"=>180,"height"=>180,"file"=>"src_180.jpg"),
 *      ));
 *
 */
class ImgUtil
{
    
 
    /**
     * 生成缩略图
     * @param string $src       源文件路径
     * @param int $width        缩略图宽度
     * @param int $height       缩略图高度
     * @param string $toFile    目标文件地址
     */
    public static function thumbnailTo($src, $width, $height, $toFile){
        return self::resizeTo($src, $width, $height, $toFile, true, true);
    }
    /**
     * 批量缩略图
     * @param string $src   源文件路径
     * @param array $list array(array("width"=>"宽","height"=>"高","file"=>"存储文件路径"))
     */
    public static function thumbnailBat($src, $list){
        $img = new ImgUtil($src);
        $srcImg = $img->resource;
        $srcWidth = imagesx($srcImg);
        $srcHeight = imagesy($srcImg);
        foreach($list as $k=>$info){
            $toWidth = $info["width"];
            $toHeight = $info["height"];
            self::scale($toWidth, $toHeight, $srcWidth, $srcHeight, true, true);
            $retImg = self::scaleToImg($srcImg, $toWidth, $toHeight, $srcWidth, $srcHeight);
            $toFile = $info["file"];
            self::saveTo($retImg, $toFile, $img->type);
            imagedestroy($retImg);
        }
        return true;
    }
    /**
     * 拉伸图片到目标大小
     * @param string $src       源文件路径
     * @param int $width        目标尺寸
     * @param int $height       目标尺寸
     * @param string $toFile    缩放后存储文件
     */
    public static function resizeForceTo($src, $width, $height, $toFile){
        return self::resizeTo($src, $width, $height, $toFile, false, false);
    }
    /**
     * @param string $src        目录文件
     * @param int $width         目标尺寸
     * @param int $height        目标尺寸
     * @param string $toFile     缩放后存储文件
     * @param boolean $ratio     保持比例
     * @param boolean $thumbnail 如果为false支持等比放大 true则只支持等比从大到小
     */
    public static function resizeTo($src, $width, $height, $toFile, $ratio=TRUE, $thumbnail=FALSE){
        $img = new ImgUtil($src);
        $srcImg = $img->resource;
    
        $srcWidth = imagesx($srcImg);
        $srcHeight = imagesy($srcImg);
        self::scale($width, $height, $srcWidth, $srcHeight, $ratio, $thumbnail);
        $retImg = self::scaleToImg($srcImg, $width, $height, $srcWidth, $srcHeight);
        $img->destory();
        if(!$toFile || empty($toFile)){
            $toFile = $src;
        }
        self::saveTo($retImg, $toFile, $img->type);
        imagedestroy($retImg);
        return true;
    }
    /**
     * 缩放srcImgResource
     * @param resource $srcImg ImageResource
     * @param int $toWidth   toWidth
     * @param int $toHeight  toHeight
     * @param int $srcWidth  srcWidth
     * @param int $srcHeight srcHeight
     * @return resource      ImageResource
     */
    public static function scaleToImg($srcImg,$toWidth,$toHeight,$srcWidth=-1,$srcHeight=-1){
        if($srcWidth<0||$srcHeight<0){
            $srcWidth = imagesx($srcImg);
            $srcHeight = imagesy($srcImg);
        }
        if(function_exists("imagecopyresampled")){
            $toImg = imagecreatetruecolor($toWidth, $toHeight);
            imagecopyresampled($toImg,$srcImg,0,0,0,0,$toWidth,$toHeight,$srcWidth,$srcHeight);
        }else{
            $toImg = imagecreate($toWidth,$toHeight);
            imagecopyresized($toImg,$srcImg,0,0,0,0,$toWidth,$toHeight,$srcWidth,$srcHeight);
        }
        return $toImg;
    }
    /**
     * 根据是否保持比例是否缩略图,计算缩放后的真实尺寸
     * @param int $toWidth       toWidth
     * @param int $toHeight      toHeight
     * @param int $srcWidth      srcWidth
     * @param int $srcHeight     srcHeight
     * @param boolean $ratio     保持比例
     * @param boolean $thumbnail 如果为false支持等比放大 true则只支持等比从大到小
     */
    public static function scale(&$toWidth,&$toHeight,$srcWidth,$srcHeight, $ratio=TRUE, $thumbnail=FALSE){
        if($ratio || $thumbnail){
            if($thumbnail && ($srcWidth<$toWidth && $srcHeight<$toHeight)){
                $toWidth = $srcWidth;
                $toHeight = $srcHeight;
            }else{
                if (($toWidth/$toHeight) <= ($srcWidth/$srcHeight)){
                    $toHeight = intval($toWidth * ($srcHeight / $srcWidth));
                }else{
                    $toWidth = intval($toHeight * ($srcWidth / $srcHeight));
                }
            }
        }
    }
    /**
     * 保存ImageResource到文件
     * @param resource $image
     * @param string $file
     * @param string $type
     */
    public static function saveTo($image,$file,$type="jpg"){
        if($type=="png"){
            imagepng($image, $file);
        }else if($type=="gif"){
            $transColor = imagecolorallocatealpha($image, 255, 255, 255, 127);
            imagecolortransparent($image, $transColor);
            imagegif($image, $file);
        }else{
            imagejpeg($image, $file, 100);
        }
    }   
    
    
    public $resource;
    public $type = null;
    public function __construct($src){
        if(is_string($src)){
            if(file_exists($src) && is_readable($src)){
                $info = getimagesize($src);
                if($info){
                    if ($info[2] == IMAGETYPE_JPEG){
                        $this->resource = imagecreatefromjpeg($src);
                        $this->type = "jpeg";
                    }else if($info[2] == IMAGETYPE_PNG){
                        $this->resource = @imagecreatefrompng($src);
                        $this->type = "png";
                    }else if($info[2] == IMAGETYPE_GIF){
                        $this->resource = @imagecreatefromgif($src);
                        $this->type = "gif";
                    }else if($info[2] == IMAGETYPE_BMP){
                        $this->resource = @imagecreatefromwbmp($src);
                        $this->type = "bmp";
                    }
                }
            }else{
                $this->resource = @imagecreatefromstring($src);
            }
        }else if(is_array($src) && count($src)>1){
            if(isset($src[0])){
                $this->resource = imagecreatetruecolor($src[0], $src[1]);
            }else{
                $this->resource = imagecreatetruecolor($src["width"], $src["height"]);
            }
        }else if(is_resource($src)){
            $this->resource = $src;
        }else if(get_class($src)==get_class($this)){
            $this->resource = $src->resource;
            $this->type = $src->type;
        }
        if($this->resource==null){
            throw new Exception("ArgumentError:".$src);
        }
    }
    public function width()
    {
        return imagesx($this->resource);
    }
    public function height()
    {
        return imagesy($this->resource);
    }
    public function save($file,$type=null){
        if($type==null){
            $type = $this->type;
        }
        self::saveTo($this->resource, $file, $type);
    }
    public function destory(){
        imagedestroy($this->resource);
    }
    
}
 
?>

 

稳定

产品可用性高于99.9%

贴心

全国7*24小时客服热线

专业

产品经理在线技术支持

快速

快速上线运营快

承诺

我们选择声誉

坚持

10年专注高端品质开发
  • 返回顶部