国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

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

2024-05-04 23:22:34
字體:
供稿:網(wǎng)友

現(xiàn)在寫東西都喜歡封裝成類.....大家調(diào)用一下就行了..我就不說怎么調(diào)用了

復(fù)制代碼 代碼如下:

<?php
class 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;//圖像縮放比

   //初始化內(nèi)部變量
   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);
       //創(chuàng)建一個(gè)等比例縮放大小的畫布
       $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)){
           //如果在的話則搜索這個(gè)顏色的RGB色相
           $trans_index=imagecolorsforindex($this->o_img_source,$trans_init);
           //找到之后就創(chuàng)建這樣一個(gè)顏色
           $trans_new=imagecolorallocate($this->n_img_source,$trans_index["red"],$trans_index["green"],$trans_index["blue"]);
           //然后我們用這個(gè)顏色去填充新的圖像
           imagefill($this->n_img_source,0,0,$trans_new);
           //然后我們在把填充色設(shè)置為透明
           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);

   }

}


說明:因?yàn)橄惹皼]想那么多所以聲明了很多私有的內(nèi)部變量以便調(diào)用...程序看起來很笨拙啊......

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 麻栗坡县| 大石桥市| 资溪县| 常宁市| 新建县| 筠连县| 方城县| 陆丰市| 江安县| 依安县| 垦利县| 聊城市| 平果县| 阜康市| 博乐市| 温泉县| 龙井市| 科尔| 韶关市| 普兰店市| 从化市| 防城港市| 巴青县| 沾益县| 都兰县| 汤原县| 苍溪县| 临汾市| 吴忠市| 罗定市| 灵山县| 鱼台县| 苏州市| 盖州市| 襄城县| 康平县| 遂平县| 都兰县| 昭通市| 常德市| 衡东县|