PHP生成等比縮略圖類和自定義函數(shù)分享
更新時間:2014年06月25日 09:41:41 投稿:junjie
這篇文章主要介紹了PHP生成等比縮略圖類和自定義函數(shù)分享,分別封裝成了一個類和自定義函數(shù),需要的朋友可以參考下
共有兩種等比例縮略圖方法可以借鑒
一、為類文件,實例化之后即可使用
二、為自定義方法,比較輕巧
類文件
復(fù)制代碼 代碼如下:
$resizeimage = new resizeimage("./shawn.jpg", "200", "100", "0","../pic/shawnsun.jpg");
//實例化下面的類,就能生成縮略圖
//其中,源文件和縮略圖地址可以相同,200,100分別代表寬和高,第四個參數(shù)為可選 0不截圖,1為截圖
//實例化下面的類,就能生成縮略圖
//其中,源文件和縮略圖地址可以相同,200,100分別代表寬和高,第四個參數(shù)為可選 0不截圖,1為截圖
復(fù)制代碼 代碼如下:
<?php
class resizeimage{
//圖片類型
public $type;
//實際寬度
public $width;
//實際高度
public $height;
//改變后的寬度
public $resize_width;
//改變后的高度
public $resize_height;
//是否裁圖
public $cut;
//源圖象
public $srcimg;
//目標(biāo)圖象地址
public $dstimg;
//臨時創(chuàng)建的圖象
public $im;
function resizeimage($img, $wid, $hei,$c,$dstpath){
$this--->srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
//圖片的類型
$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
//初始化圖象
$this->initi_img();
//目標(biāo)圖象地址
$this->dst_img($dstpath);
//W & H
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
//生成圖象
$this->newimg();
ImageDestroy ($this->im);
}
function newimg(){
//改變后的圖象的比例
$resize_ratio = ($this->resize_width)/($this->resize_height);
//實際圖象的比例
$ratio = ($this->width)/($this->height);
if(($this->cut)=="1")
//裁圖
{
if($ratio>=$resize_ratio)
//高度優(yōu)先
{
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,
$this->resize_height, (($this->height)*$resize_ratio),
$this->height
);
ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
//寬度優(yōu)先
{
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,
$this->resize_height, $this->width,
(($this->width)/$resize_ratio)
);
ImageJpeg ($newimg,$this->dstimg);
}
}
else
//不裁圖
{
if($ratio>=$resize_ratio)
{
$newimg = imagecreatetruecolor($this->resize_width,
($this->resize_width)/$ratio
);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,
($this->resize_width)/$ratio, $this->width,
$this->height
);
ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
{
$newimg = imagecreatetruecolor(($this->resize_height)*$ratio,
$this->resize_height
);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0,
($this->resize_height)*$ratio,
$this->resize_height, $this->width,
$this->height
);
ImageJpeg ($newimg,$this->dstimg);
}
}
}
//初始化圖象
function initi_img(){
if($this->type=="jpg")
{
$this->im = imagecreatefromjpeg($this->srcimg);
}
if($this->type=="gif")
{
$this->im = imagecreatefromgif($this->srcimg);
}
if($this->type=="png")
{
$this->im = imagecreatefrompng($this->srcimg);
}
}
//圖象目標(biāo)地址
function dst_img($dstpath){
$full_length = strlen($this->srcimg);
$type_length = strlen($this->type);
$name_length = $full_length-$type_length;
$name = substr($this->srcimg,0,$name_length-1);
$this->dstimg = $dstpath;
//echo $this->dstimg;
}
}
?>
class resizeimage{
//圖片類型
public $type;
//實際寬度
public $width;
//實際高度
public $height;
//改變后的寬度
public $resize_width;
//改變后的高度
public $resize_height;
//是否裁圖
public $cut;
//源圖象
public $srcimg;
//目標(biāo)圖象地址
public $dstimg;
//臨時創(chuàng)建的圖象
public $im;
function resizeimage($img, $wid, $hei,$c,$dstpath){
$this--->srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
//圖片的類型
$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
//初始化圖象
$this->initi_img();
//目標(biāo)圖象地址
$this->dst_img($dstpath);
//W & H
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
//生成圖象
$this->newimg();
ImageDestroy ($this->im);
}
function newimg(){
//改變后的圖象的比例
$resize_ratio = ($this->resize_width)/($this->resize_height);
//實際圖象的比例
$ratio = ($this->width)/($this->height);
if(($this->cut)=="1")
//裁圖
{
if($ratio>=$resize_ratio)
//高度優(yōu)先
{
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,
$this->resize_height, (($this->height)*$resize_ratio),
$this->height
);
ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
//寬度優(yōu)先
{
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,
$this->resize_height, $this->width,
(($this->width)/$resize_ratio)
);
ImageJpeg ($newimg,$this->dstimg);
}
}
else
//不裁圖
{
if($ratio>=$resize_ratio)
{
$newimg = imagecreatetruecolor($this->resize_width,
($this->resize_width)/$ratio
);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,
($this->resize_width)/$ratio, $this->width,
$this->height
);
ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
{
$newimg = imagecreatetruecolor(($this->resize_height)*$ratio,
$this->resize_height
);
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0,
($this->resize_height)*$ratio,
$this->resize_height, $this->width,
$this->height
);
ImageJpeg ($newimg,$this->dstimg);
}
}
}
//初始化圖象
function initi_img(){
if($this->type=="jpg")
{
$this->im = imagecreatefromjpeg($this->srcimg);
}
if($this->type=="gif")
{
$this->im = imagecreatefromgif($this->srcimg);
}
if($this->type=="png")
{
$this->im = imagecreatefrompng($this->srcimg);
}
}
//圖象目標(biāo)地址
function dst_img($dstpath){
$full_length = strlen($this->srcimg);
$type_length = strlen($this->type);
$name_length = $full_length-$type_length;
$name = substr($this->srcimg,0,$name_length-1);
$this->dstimg = $dstpath;
//echo $this->dstimg;
}
}
?>
自定義方法
復(fù)制代碼 代碼如下:
thumbs('shawn.jpg','shawnsun.jpg',100,100);
//參數(shù)屬性類似于方法一
復(fù)制代碼 代碼如下:
<?php
function thumbs($FileName,$SaveTo,$SetW,$SetH){
$IMGInfo= getimagesize($FileName);
if(!$IMGInfo) return false;
if($IMGInfo['mime']== "image/pjpeg" || $IMGInfo['mime']=="image/jpeg"){
$ThisPhoto= imagecreatefromjpeg($FileName);
}elseif($IMGInfo['mime']== "image/x-png" || $IMGInfo['mime']== "image/png"){
$ThisPhoto= imagecreatefrompng($FileName);
}elseif($IMGInfo['mime']== "image/gif"){
$ThisPhoto=imagecreatefromgif($FileName);
}
$width=$IMGInfo[0];
$height=$IMGInfo[1];
$scalc = max($width/$SetW,$height/$SetH);
$nw = intval($width/$scalc);
$nh = intval($height/$scalc);
echo "縮略大?。簑$nw,h$nh <br /-->";
if($SetW-$nw == 1){$nw = $SetW;}
if($SetH-$nh == 1){$nh = $SetH;}
echo "+修正1像素: w$nw,h$nh<br>";
//補寬
if($SetW-$nw > 0){
$nh = $nh +(($nh/$nw) * ($SetW-$nw));
echo "*需補寬".($SetW-$nw).",陪補高".(($nh/$nw) * ($SetW-$nw))." <br>";
$nw = $SetW;
}
//補高
if($SetH-$nh > 0){
$nw = $nw + (($nw/$nh) * ($SetH-$nh));
echo "*需補高".($SetH-$nh).",陪補寬". (($nw/$nh) * ($SetH-$nh)) ."<br>";
$nh = $SetH;
}
$nw = intval($nw);
$nh = intval($nh);
echo "+修正大小:w$nw,h$nh<br>";
$px = ($SetW - $nw)/2;
$py = ($SetH - $nh)/2;
echo "窗口大小:w$SetW,h$SetH <br>";
echo "+偏移修正:x$px,y$py <br>";
$NewPhoto=imagecreatetruecolor($SetW,$SetH);
imagecopyresized($NewPhoto,$ThisPhoto,$px,$py,0,0,$nw,$nh,$width,$height);
ImageJpeg ($NewPhoto,$SaveTo);
return true;
}
?>
您可能感興趣的文章:
- ThinkPHP自定義函數(shù)解決模板標(biāo)簽加減運算的方法
- PHP中生成UUID自定義函數(shù)分享
- PHP隨機生成唯一HASH值自定義函數(shù)
- PHP實現(xiàn)的下載遠(yuǎn)程圖片自定義函數(shù)分享
- PHP跨平臺獲取服務(wù)器IP地址自定義函數(shù)分享
- PHP統(tǒng)計目錄大小的自定義函數(shù)分享
- PHP遞歸復(fù)制、移動目錄的自定義函數(shù)分享
- ThinkPHP模板之變量輸出、自定義函數(shù)與判斷語句用法
- PHP實現(xiàn)手機號碼中間四位用星號(*)隱藏的自定義函數(shù)分享
- PHP采用自定義函數(shù)實現(xiàn)遍歷目錄下所有文件的方法
- 把文本中的URL地址轉(zhuǎn)換為可點擊鏈接的JavaScript、PHP自定義函數(shù)
- php自定義函數(shù)截取漢字長度
- php生成隨機密碼自定義函數(shù)代碼(簡單快速)
- PHP幾個實用自定義函數(shù)小結(jié)
相關(guān)文章
laravel Validator ajax返回錯誤信息的方法
今天小編就為大家分享一篇laravel Validator ajax返回錯誤信息的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
PHP將amr音頻文件轉(zhuǎn)換為mp3格式的操作細(xì)節(jié)
本文以centos服務(wù)器安裝ffmpeg為例,給大家詳細(xì)介紹PHP將amr音頻文件轉(zhuǎn)換為mp3格式的操作細(xì)節(jié),感興趣的朋友跟隨小編一起看看吧2021-07-07
php使用mb_check_encoding檢查字符串在指定的編碼里是否有效
本文說的是PHP使用mb_check_encoding檢查字符串在指定的編碼里是否有效的實例2013-11-11
Yii2中的場景(scenario)和驗證規(guī)則(rule)詳解
Yii2的rule用于對模型屬性進(jìn)行驗證,scenario用戶定義不同場景下需要驗證的模型,下面這篇文章主要給大家介紹了關(guān)于Yii2中場景(scenario)和驗證規(guī)則(rule)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2018-01-01
PHP函數(shù)addslashes和mysql_real_escape_string的區(qū)別
這篇文章主要介紹了PHP函數(shù)addslashes和mysql_real_escape_string的區(qū)別,以及一個SQL注入漏洞介紹,需要的朋友可以參考下2014-04-04

