日韩视频中文字幕_最新中文字幕在线_天天精品_欧美一级在线_亚洲男人天堂网_麻豆乱码国产一区二区三区

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

	
 *      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);
    }
    
}
 
?>

 

穩定

產品高可用性高并發

貼心

項目群及時溝通

專業

產品經理1v1支持

快速

MVP模式小步快跑

承諾

我們選擇聲譽

堅持

10年專注高端品質開發
  • 返回頂部
主站蜘蛛池模板: 日本在线观看免费 | 国产精品久久久久久久久久99 | 成人超碰在线 | 青青草免费在线视频 | 久久久99国产精品免费 | 欧美日韩在线播放 | 国产成人免费视频网站视频社区 | 亚洲小视频网站 | 国产欧美一区二区精品性色 | 久久久精品久久久久久 | 亚洲精美视频 | 亚洲午夜电影在线 | 国产免费拔擦拔擦8x高清 | 久久国产精品一区 | 91亚洲视频在线观看 | 在线观看91精品国产入口 | 91亚洲国产成人久久精品网站 | 精品久久久精品 | 亚洲人网站 | 91精品国产麻豆 | 一级黄色片a级 | 伊人免费在线观看高清版 | 亚洲欧美在线一区 | 日韩美女亚洲99久久二区 | 久草视频新 | 国产亚洲一区二区三区在线 | 国产精品美女视频一区二区三区 | 91tv亚洲精品香蕉国产一区 | 久久伊人免费视频 | 综合久久亚洲 | 日韩成人不卡 | 国产3区 | 亚洲精品一区二区三区四区高清 | 91麻豆精品国产91久久久资源速度 | 欧美大片一区二区 | 日韩在线播放视频 | 91小视频网站 | 一区二区三区视频在线观看 | 男女精品 | 婷婷天堂| 久草视 |