PHP基于GD庫的縮略圖生成代碼(支持jpg,gif,png格式)
更新時(shí)間:2014年06月19日 10:13:10 投稿:hebedich
你可能會遇到這樣的問題,在用戶上傳了一張圖片后,得到這張圖片的縮略圖,PHP可以使用GD庫生成縮略圖,那么我們來探討下,如何才能生成高質(zhì)量的縮略圖呢?
還是老規(guī)矩,直接上代碼
<?php
/**
* 縮略圖生成類,使用示例:
*/
$newimage=new ImageResize();
$newimage->resize("tu.jpg","tu_lit.jpg",1000,1000);
echo $newimage->GetLastError();
class ImageResize{
private $localimage;//原圖路徑
private $remoteimage;//縮略圖保存路徑
private $localinfo;//原圖屬性
private $error;
function resize($localimg, $remoteimg, $x, $y) {
//檢測是否支持gd圖像處理
if(!$this->_checkenv()){
return false;
}
$this->localimage = $localimg;
$this->remoteimage = $remoteimg;
$this->localinfo = getimagesize($this->localimage); //獲取本地圖像的信息
return $this->_resize($x,$y);
}
/**
* 檢測當(dāng)前環(huán)境是否支持GD
*/
private function _checkenv(){
if(!function_exists('gd_info')){
$this->error[]="當(dāng)前環(huán)境不支持GD圖像處理,請先安裝GD庫并開啟PHP相關(guān)擴(kuò)展";
return false;
}
return true;
}
/**
* 生成縮略圖主函數(shù)
* @param int $x 指定的縮略圖寬度
* @param int $y 指定的縮略圖高度
* @return boolean
*/
private function _resize($x,$y){
if(!$this->localinfo){
$this->error[]="本地圖像文件不存在";
return false;
}
//創(chuàng)建圖像句柄
$im=@$this->_create($this->localinfo[2]);
if(!$im){
$this->error[]="當(dāng)前GD庫不支持圖像類型:{$this->localinfo['mime']}";
return false;
}
$dstsize=$this->_dstsize($x, $y);
$dstim=@imagecreatetruecolor($dstsize["width"],$dstsize["height"]);
$whitecolor=@imagecolorallocatealpha($dstim, 0, 0, 0,127);
imagefill($dstim,0,0,$whitecolor);
$re=@imagecopyresampled($dstim, $im, 0, 0, 0, 0, $dstsize["width"], $dstsize["height"], $this->localinfo[0], $this->localinfo[1]);
if(!$re){
$this->error[]="圖像重新采樣失敗";
return false;
}
if(!imagejpeg($dstim, $this->remoteimage)){
if(!imagepng($dstim,$this->remoteimage)){
if(!imagegif($dstim,$this->remoteimage)){
$this->error[]="保存縮略圖到{$this->remoteimage}失敗,請檢查gd環(huán)境是否正常和縮略圖文件夾的寫入權(quán)限。";
return false;
}
}
}
$this->error[]="success";
return true;
}
/**
* 根據(jù)本地圖片類型,創(chuàng)建圖片資源
* @param 圖像類型代碼 $code
* @return resource/boolean 成功則返回resourse失敗則返回false
*/
private function _create($code){
$src=$this->localimage;
switch ($code){
case 1:
return imagecreatefromgif($src);
break;
case 2:
return imagecreatefromjpeg($src);
break;
case 3:
return imagecreatefrompng($src);
break;
default :
return false;
break;
}
}
/**
* 按比例計(jì)算合適的寬度
* @param int $x 指定的縮略圖寬度
* @param int $y 指定的縮略圖高度
* @return array 包含調(diào)整后的縮略圖寬度和高度
*/
private function _dstsize($x,$y){
list($srcwidth,$srcheight)=$this->localinfo;
if(($srcwidth/$srcheight)<($x/$y)){
$x=floor($y*$srcwidth/$srcheight);
}else{
$y=floor($x*$srcheight/$srcwidth);
}
$dstsize["width"]=$x;
$dstsize["height"]=$y;
return $dstsize;
}
/**
* 獲取最后一條錯(cuò)誤信息
* return string
*/
function GetLastError(){
return array_pop($this->error);
}
/**
* 獲取所有錯(cuò)誤信息
* return array
*/
function GetAllError(){
return $this->error;
}
}
您可能感興趣的文章:
- PHP簡單實(shí)現(xiàn)圖片格式轉(zhuǎn)換(jpg轉(zhuǎn)png,gif轉(zhuǎn)png等)
- PHP中使用Imagick讀取pdf并生成png縮略圖實(shí)例
- PHP使用imagick讀取PDF生成png縮略圖的兩種方法
- PHP輸出圖像imagegif、imagejpeg與imagepng函數(shù)用法分析
- php縮放gif和png圖透明背景變成黑色的解決方法
- PHP實(shí)現(xiàn)生成透明背景的PNG縮略圖函數(shù)分享
- php 處理png圖片白色背景色改為透明色的實(shí)例代碼
- PHP實(shí)現(xiàn)對png圖像進(jìn)行縮放的方法(支持透明背景)
- 支持png透明圖片的php生成縮略圖類分享
- PHP添加PNG圖片背景透明水印操作類定義與用法示例
- php 實(shí)現(xiàn)svg轉(zhuǎn)化png格式的方法分析
相關(guān)文章
laravel項(xiàng)目利用twemproxy部署redis集群的完整步驟
Twemproxy是一個(gè)代理服務(wù)器,可以通過它減少M(fèi)emcached或Redis服務(wù)器所打開的連接數(shù)。下面這篇文章主要給大家介紹了關(guān)于laravel項(xiàng)目利用twemproxy部署redis集群的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-05-05
解決laravel 5.1報(bào)錯(cuò):No supported encrypter found的辦法
這篇文章主要給大家介紹了關(guān)于解決laravel 5.1報(bào)錯(cuò):No supported encrypter found的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06
Thinkphp 5.0實(shí)現(xiàn)微信企業(yè)付款到零錢
這篇文章主要為大家詳細(xì)介紹了Thinkphp 5.0實(shí)現(xiàn)微信企業(yè)付款到零錢,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
win7系統(tǒng)配置php+Apache+mysql環(huán)境的方法
如何在win7系統(tǒng)下配置php環(huán)境呢,php+Apache+mysql都是在配置過程中必不可少的元素,下面通過本篇文章給大家介紹win7系統(tǒng)配置php+Apache+mysql環(huán)境的方法,感興趣的朋友一起來學(xué)習(xí)
2015-08-08
CI框架學(xué)習(xí)筆記(二) -入口文件index.php
本文介紹的是CI框架的入口文件index.php的相關(guān)資料,需要的朋友可以參考下
2014-10-10 
