PHP的一個完美GIF等比縮放類,附帶去除縮放黑背景

來源:互聯網
上載者:User

 我這裡沒有判斷檔案類型,因為png和jpeg縮放後都不會出現黑色背景,就單獨gif不行..查了下資料發現了一個完美的解決辦法,附帶有詳解

現在寫東西都喜歡封裝成類.....大家調用一下就行了..我就不說怎麼調用了 代碼如下:<?phpclass resize_image{   private $o_img_width;//原映像寬度   private $o_img_height;//原映像高度   private $n_img_width;//新映像寬度   private $n_img_height;//新映像高度   private $o_img_file;//原影像檔   private $o_img_source;//原映像資源   private $n_img_file;//新映像資源   private $n_img_source;//新映像資源   private $o_to_n_per=0.5;//映像縮放比    //初始化內部變數   function __construct($oldfile,$newfile){       list($width,$height)=getimagesize($oldfile);       $this->o_img_file=$oldfile;       $this->o_img_width=$width;       $this->o_img_height=$height;       $this->n_img_file=$newfile;   }    //等比例縮放並且解決GIF透明色為黑色背景的問題   function get_resize_scaling_img(){       $this->n_img_width=$this->o_img_width*$this->o_to_n_per;       $this->n_img_height=$this->o_img_height*$this->o_to_n_per;       //等比例縮放圖片(演算法)       if ( $this->n_img_width && ( $this->o_img_width <$this->o_img_height))       {             $this->n_img_width = ( $this->n_img_height/$this->o_img_height) * $this->o_img_width;       }       else       {            $this->n_img_height = ($this->n_img_width / $this->o_img_width) * $this->o_img_height;       }        $this->o_img_source=imagecreatefromgif($this->o_img_file);       //建立一個等比例縮放大小的畫布       $this->n_img_source=imagecreatetruecolor($this->o_img_width,$this->n_img_height);        //美化:去除黑色不透明背景       $trans_init=imagecolortransparent($this->o_img_source);       //尋找透明色並且判斷是否在總顏色中       if($trans_init>=0 && $trans_init < imagecolorstotal($this->o_img_source)){           //如果在的話則搜尋這個顏色的RGB色相           $trans_index=imagecolorsforindex($this->o_img_source,$trans_init);           //找到之後就建立這樣一個顏色           $trans_new=imagecolorallocate($this->n_img_source,$trans_index["red"],$trans_index["green"],$trans_index["blue"]);           //然後我們用這個顏色去填充新的映像           imagefill($this->n_img_source,0,0,$trans_new);           //然後我們在把填充色設定為透明           imagecolortransparent($this->n_img_source,$trans_new);       }       //拷貝原映像到新畫板上       imagecopyresized($this->n_img_source,$this->o_img_source,0,0,0,0,$this->n_img_width,$this->n_img_height,$this->o_img_width,$this->o_img_height);        return $this->n_img_source;   }   //最終銷毀資源   function __destruct(){       imagedestroy($this->o_img_source);       imagedestroy($this->n_img_source);    } }   說明:因為先前沒想那麼多所以聲明了很多私人的內部變數以便調用...程式看起來很笨拙啊......

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.