PHP基于GD庫的縮略圖生成代碼(支持jpg,gif,png格式)
更新時間: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;
}
}
/**
* 按比例計算合適的寬度
* @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;
}
/**
* 獲取最后一條錯誤信息
* return string
*/
function GetLastError(){
return array_pop($this->error);
}
/**
* 獲取所有錯誤信息
* 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)文章
PHP基礎(chǔ)學(xué)習(xí)之流程控制的實(shí)現(xiàn)分析
本篇文章介紹了,PHP基礎(chǔ)學(xué)習(xí)之流程控制的實(shí)現(xiàn)分析。需要的朋友參考下2013-04-04
本文章來給各位同學(xué)介紹一下關(guān)于Php CURL模擬登陸論壇并采集數(shù)據(jù)實(shí)例,如果你對利用curl模擬登錄功能有興趣可進(jìn)入?yún)⒖肌?/div> 2015-05-05
ThinkPHP單字母函數(shù)(快捷方法)使用總結(jié)
這篇文章主要介紹了ThinkPHP單字母函數(shù)(快捷方法)使用總結(jié),對ThinkPHP的快捷方法做了針對性的歸納總結(jié),需要的朋友可以參考下2014-07-07
Laravel第三方包報class not found的解決方法
今天小編就為大家分享一篇Laravel第三方包報class not found的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
購物車實(shí)現(xiàn)的幾種方式優(yōu)缺點(diǎn)對比
本文給大家分享的是實(shí)現(xiàn)購物車的幾種方式,以及這幾種方式的優(yōu)缺點(diǎn)對比的情況,非常的詳細(xì),有需要的小伙伴可以參考下2018-05-05最新評論

