欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

使用php怎么壓縮圖片并保持大小不變

這篇文章將為大家詳細(xì)講解有關(guān)使用php怎么壓縮圖片并保持大小不變,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

成都創(chuàng)新互聯(lián)公司于2013年成立,先為巨鹿等服務(wù)建站,巨鹿等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為巨鹿企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

1.cdn靜態(tài)資源包

2.oss管理文件資源,不一定是圖片,也可以是文件哦

3.搭建自己的文件服務(wù)器,圖片當(dāng)然可以了

不過(guò)以上好是好,不過(guò)都得花錢(qián),我感覺(jué)還是從根本上解決比較好,來(lái)吧,那我們就上代碼把

<?php/**
 * 圖片壓縮類(lèi):通過(guò)縮放來(lái)壓縮。
 * 如果要保持源圖比例,把參數(shù)$percent保持為1即可。
 * 即使原比例壓縮,也可大幅度縮小。數(shù)碼相機(jī)4M圖片。也可以縮為700KB左右。如果縮小比例,則體積會(huì)更小。
 *
 * 結(jié)果:可保存、可直接顯示。 
 */
     class imgcompress{    
         private $srcurlurl;    
         private $image;    
         private $imageinfo;    
         private $percent = 0.5;    
 /**
     * 圖片壓縮
     * @param $srcurl 源圖
     * @param float $percent  壓縮比例     */
    public function __construct($srcurl, $percent=1)
    {        
    $this->src = $srcurl;        
    $this->percent = $percent;
    }    
    /** 高清壓縮圖片
     * @param string $saveName  提供圖片名(可不帶擴(kuò)展名,用源圖擴(kuò)展名)用于保存。或不提供文件名直接顯示     
     */
    public function compressImg($saveName='')
    {        
    $this->_openImage();        
    if(!empty($saveName)) $this->_saveImage($saveName);  //保存
       else $this->_showImage();
    }    
    /**
     * 內(nèi)部:打開(kāi)圖片     */
    private 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);        
        $this->_thumpImage();
    }    
    /**
     * 內(nèi)部:操作圖片     
     */
    private function _thumpImage()
    {
    $new_width = $this->imageinfo['width'] * $this->percent;        
    $new_height = $this->imageinfo['height'] * $this->percent;        
    $image_thump = imagecreatetruecolor($new_width,$new_height);        
    //將原圖復(fù)制到圖片載體上面,并且按照一定比例壓縮,極大的保持了清晰度
        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;
    }
    /**
     * 輸出圖片:保存圖片則用saveImage()     
     */
    private function _showImage()
    {        
    header('Content-Type: image/'.$this->imageinfo['type']);        
    $funcs = "image".$this->imageinfo['type'];        
    $funcs($this->image);
    }    
    /**
     * 保存圖片到硬盤(pán):
     * @param  string $dstImgName  1、可指定字符串不帶后綴的名稱(chēng),使用源圖擴(kuò)展名 。2、直接指定目標(biāo)圖片名帶擴(kuò)展名。     */
    private function _saveImage($dstImgName)
    {        
    if(empty($dstImgName)) return false;        
    $allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif'];   //如果目標(biāo)圖片名有后綴就用目標(biāo)圖片擴(kuò)展名 后綴,如果沒(méi)有,則用源圖的擴(kuò)展名
        $dstExt =  strrchr($dstImgName ,".");        
        $sourseExt = strrchr($this->src ,".");        
        if(!empty($dstExt)) $dstExt =strtolower($dstExt);        
        if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);        
        //有指定目標(biāo)名擴(kuò)展名
        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'];
        }        
        $funcs = "image".$this->imageinfo['type'];        
        $funcs($this->image,$dstName);
    }    
    /**
     * 銷(xiāo)毀圖片     
     */
    public function __destruct(){
        imagedestroy($this->image);
    }
}
$source =  '1.jpg';//原圖片名稱(chēng)
$dst_img = 'text.png';//壓縮后圖片的名稱(chēng)
$percent = 1;  #原圖壓縮,不縮放,但體積大大降低
$image = (new imgcompress($source,$percent))->compressImg($dst_img);//新圖片名字叫什么名字可以自己定

關(guān)于使用php怎么壓縮圖片并保持大小不變就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前文章:使用php怎么壓縮圖片并保持大小不變
分享URL:http://www.chinadenli.net/article0/gpcpoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)做網(wǎng)站App設(shè)計(jì)搜索引擎優(yōu)化網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)