91亚洲精华国内精华精华液_国产高清在线精品一区不卡_精品特级一级毛片免费观看_欧美日韩中文制服有码_亚洲精品无码你懂的网站369

/**
 * 圖片工具類,支持縮略圖, 獲取類型等
 *

	
 *      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    目標(biāo)文件地址
     */
    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;
    }
    /**
     * 拉伸圖片到目標(biāo)大小
     * @param string $src       源文件路徑
     * @param int $width        目標(biāo)尺寸
     * @param int $height       目標(biāo)尺寸
     * @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         目標(biāo)尺寸
     * @param int $height        目標(biāo)尺寸
     * @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;
    }
    /**
     * 根據(jù)是否保持比例是否縮略圖,計算縮放后的真實(shí)尺寸
     * @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);
    }
    
}
 
?>

 

穩(wěn)定

產(chǎn)品高可用性高并發(fā)

貼心

項(xiàng)目群及時溝通

專業(yè)

產(chǎn)品經(jīng)理1v1支持

快速

MVP模式小步快跑

承諾

我們選擇聲譽(yù)

堅持

10年專注高端品質(zhì)開發(fā)
  • 返回頂部