PHP的一個完美GIF等比縮放類,附帶去除縮放黑背景
現(xiàn)在寫東西都喜歡封裝成類.....大家調(diào)用一下就行了..我就不說怎么調(diào)用了
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)建一個等比例縮放大小的畫布
$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);
//找到之后就創(chuàng)建這樣一個顏色
$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);
//然后我們在把填充色設(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);
}
}
說明:因為先前沒想那么多所以聲明了很多私有的內(nèi)部變量以便調(diào)用...程序看起來很笨拙啊......
相關(guān)文章
Yii使用find findAll查找出指定字段的實現(xiàn)方法
這篇文章主要介紹了Yii使用find findAll查找出指定字段的實現(xiàn)方法,非常實用的技巧,需要的朋友可以參考下2014-09-09
laravel-admin表單提交隱藏一些數(shù)據(jù),回調(diào)時獲取數(shù)據(jù)的方法
今天小編就為大家分享一篇laravel-admin表單提交隱藏一些數(shù)據(jù),回調(diào)時獲取數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP把小數(shù)轉(zhuǎn)成整數(shù)3種方法
這篇文章主要介紹了PHP把小數(shù)轉(zhuǎn)成整數(shù)3種方法,實際上是使用的PHP自帶的3個函數(shù),分別是floor、ceil和round,需要的朋友可以參考下2014-06-06
詳解Laravel5.6 Passport實現(xiàn)Api接口認證
這篇文章主要介紹了詳解Laravel5.6 Passport實現(xiàn)Api接口認證,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
PHP安裝擴展mcrypt以及相關(guān)依賴項深入講解
這篇文章主要介紹了PHP安裝擴展mcrypt以及相關(guān)依賴項深入講解,步驟講解的很清晰,有需要的同學可以研究下2021-03-03
圖文詳解laravel多對多關(guān)聯(lián)模型
多對多就相當于一個專題Topic有多個文章,但是這多個文章又屬于多個專題,下面這篇文章主要給大家介紹了關(guān)于laravel多對多關(guān)聯(lián)模型的相關(guān)資料,需要的朋友可以參考下2021-08-08

