PHP生成等比縮略圖類和自定義函數(shù)分享
共有兩種等比例縮略圖方法可以借鑒
一、為類文件,實(shí)例化之后即可使用
二、為自定義方法,比較輕巧
類文件
//實(shí)例化下面的類,就能生成縮略圖
//其中,源文件和縮略圖地址可以相同,200,100分別代表寬和高,第四個(gè)參數(shù)為可選 0不截圖,1為截圖
class resizeimage{
//圖片類型
public $type;
//實(shí)際寬度
public $width;
//實(shí)際高度
public $height;
//改變后的寬度
public $resize_width;
//改變后的高度
public $resize_height;
//是否裁圖
public $cut;
//源圖象
public $srcimg;
//目標(biāo)圖象地址
public $dstimg;
//臨時(shí)創(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);
//實(shí)際圖象的比例
$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;
}
}
?>
自定義方法
thumbs('shawn.jpg','shawnsun.jpg',100,100);
//參數(shù)屬性類似于方法一
<?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>";
//補(bǔ)寬
if($SetW-$nw > 0){
$nh = $nh +(($nh/$nw) * ($SetW-$nw));
echo "*需補(bǔ)寬".($SetW-$nw).",陪補(bǔ)高".(($nh/$nw) * ($SetW-$nw))." <br>";
$nw = $SetW;
}
//補(bǔ)高
if($SetH-$nh > 0){
$nw = $nw + (($nw/$nh) * ($SetH-$nh));
echo "*需補(bǔ)高".($SetH-$nh).",陪補(bǔ)寬". (($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 "窗口大?。簑$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)簽加減運(yùn)算的方法
- PHP中生成UUID自定義函數(shù)分享
- PHP隨機(jī)生成唯一HASH值自定義函數(shù)
- PHP實(shí)現(xiàn)的下載遠(yuǎn)程圖片自定義函數(shù)分享
- PHP跨平臺(tái)獲取服務(wù)器IP地址自定義函數(shù)分享
- PHP統(tǒng)計(jì)目錄大小的自定義函數(shù)分享
- PHP遞歸復(fù)制、移動(dòng)目錄的自定義函數(shù)分享
- ThinkPHP模板之變量輸出、自定義函數(shù)與判斷語句用法
- PHP實(shí)現(xiàn)手機(jī)號(hào)碼中間四位用星號(hào)(*)隱藏的自定義函數(shù)分享
- PHP采用自定義函數(shù)實(shí)現(xiàn)遍歷目錄下所有文件的方法
- 把文本中的URL地址轉(zhuǎn)換為可點(diǎn)擊鏈接的JavaScript、PHP自定義函數(shù)
- php自定義函數(shù)截取漢字長度
- php生成隨機(jī)密碼自定義函數(shù)代碼(簡單快速)
- PHP幾個(gè)實(shí)用自定義函數(shù)小結(jié)
相關(guān)文章
記Laravel調(diào)用Gin接口調(diào)用formData上傳文件的實(shí)現(xiàn)方法
這篇文章主要介紹了記Laravel調(diào)用Gin接口調(diào)用formData上傳文件的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(六)
這篇文章主要介紹了PHP網(wǎng)頁游戲Xnova(ogame)源碼解讀的公共代碼,需要的朋友可以參考下2014-06-06PHP中使用mpdf 導(dǎo)出PDF文件的實(shí)現(xiàn)方法
這篇文章主要介紹了PHP中使用mpdf 導(dǎo)出PDF文件的實(shí)現(xiàn)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10thinkphp3.2嵌入百度編輯器ueditor的實(shí)例代碼
本篇文章主要介紹了thinkphp3.2嵌入百度編輯器ueditor的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07tp5(thinkPHP5)框架連接數(shù)據(jù)庫的方法示例
這篇文章主要介紹了tp5(thinkPHP5)框架連接數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了基于thinkPHP5框架連接數(shù)據(jù)庫的相關(guān)配置、數(shù)據(jù)讀取、模板渲染等操作技巧,需要的朋友可以參考下2018-12-12PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)(oop)學(xué)習(xí)筆記 (五) - PHP 命名空間
PHP 在 5.3.0 以后的版本開始支持命名空間。什么是命名空間?從廣義上來說,命名空間是一種封裝事物的方法。在很多地方都可以見到這種抽象概念。2014-06-06