php生成縮略圖的類代碼
/**
* 功能:生成縮略圖
* 作者:phpox
* 日期:Thu May 17 09:57:05 CST 2007
*/
class CreatMiniature
{
//公共變量
var $srcFile=""; //原圖
var $echoType; //輸出圖片類型,link--不保存為文件;file--保存為文件
var $im=""; //臨時(shí)變量
var $srcW=""; //原圖寬
var $srcH=""; //原圖高
//設(shè)置變量及初始化
function SetVar($srcFile,$echoType)
{
if (!file_exists($srcFile)){
echo '源圖片文件不存在!';
exit();
}
$this->srcFile=$srcFile;
$this->echoType=$echoType;
$info = "";
$data = GetImageSize($this->srcFile,$info);
switch ($data[2])
{
case 1:
if(!function_exists("imagecreatefromgif")){
echo "你的GD庫(kù)不能使用GIF格式的圖片,請(qǐng)使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>";
exit();
}
$this->im = ImageCreateFromGIF($this->srcFile);
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
echo "你的GD庫(kù)不能使用jpeg格式的圖片,請(qǐng)使用其它格式的圖片!<a href='javascript:go(-1);'>返回</a>";
exit();
}
$this->im = ImageCreateFromJpeg($this->srcFile);
break;
case 3:
$this->im = ImageCreateFromPNG($this->srcFile);
break;
}
$this->srcW=ImageSX($this->im);
$this->srcH=ImageSY($this->im);
}
//生成扭曲型縮圖
function Distortion($toFile,$toW,$toH)
{
$cImg=$this->CreatImage($this->im,$toW,$toH,0,0,0,0,$this->srcW,$this->srcH);
return $this->EchoImage($cImg,$toFile);
ImageDestroy($cImg);
}
//生成按比例縮放的縮圖
function Prorate($toFile,$toW,$toH)
{
$toWH=$toW/$toH;
$srcWH=$this->srcW/$this->srcH;
if($toWH<=$srcWH)
{
$ftoW=$toW;
$ftoH=$ftoW*($this->srcH/$this->srcW);
}
else
{
$ftoH=$toH;
$ftoW=$ftoH*($this->srcW/$this->srcH);
}
if($this->srcW>$toW||$this->srcH>$toH)
{
$cImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
return $this->EchoImage($cImg,$toFile);
ImageDestroy($cImg);
}
else
{
$cImg=$this->CreatImage($this->im,$this->srcW,$this->srcH,0,0,0,0,$this->srcW,$this->srcH);
return $this->EchoImage($cImg,$toFile);
ImageDestroy($cImg);
}
}
//生成最小裁剪后的縮圖
function Cut($toFile,$toW,$toH)
{
$toWH=$toW/$toH;
$srcWH=$this->srcW/$this->srcH;
if($toWH<=$srcWH)
{
$ctoH=$toH;
$ctoW=$ctoH*($this->srcW/$this->srcH);
}
else
{
$ctoW=$toW;
$ctoH=$ctoW*($this->srcH/$this->srcW);
}
$allImg=$this->CreatImage($this->im,$ctoW,$ctoH,0,0,0,0,$this->srcW,$this->srcH);
$cImg=$this->CreatImage($allImg,$toW,$toH,0,0,($ctoW-$toW)/2,($ctoH-$toH)/2,$toW,$toH);
return $this->EchoImage($cImg,$toFile);
ImageDestroy($cImg);
ImageDestroy($allImg);
}
//生成背景填充的縮圖
function BackFill($toFile,$toW,$toH,$bk1=255,$bk2=255,$bk3=255)
{
$toWH=$toW/$toH;
$srcWH=$this->srcW/$this->srcH;
if($toWH<=$srcWH)
{
$ftoW=$toW;
$ftoH=$ftoW*($this->srcH/$this->srcW);
}
else
{
$ftoH=$toH;
$ftoW=$ftoH*($this->srcW/$this->srcH);
}
if(function_exists("imagecreatetruecolor"))
{
@$cImg=ImageCreateTrueColor($toW,$toH);
if(!$cImg)
{
$cImg=ImageCreate($toW,$toH);
}
}
else
{
$cImg=ImageCreate($toW,$toH);
}
$backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3); //填充的背景顏色
ImageFilledRectangle($cImg,0,0,$toW,$toH,$backcolor);
if($this->srcW>$toW||$this->srcH>$toH)
{
$proImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
if($ftoW<$toW)
{
ImageCopy($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);
}
else if($ftoH<$toH)
{
ImageCopy($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH);
}
else
{
ImageCopy($cImg,$proImg,0,0,0,0,$ftoW,$ftoH);
}
}
else
{
ImageCopyMerge($cImg,$this->im,($toW-$ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
}
return $this->EchoImage($cImg,$toFile);
ImageDestroy($cImg);
}
function CreatImage($img,$creatW,$creatH,$dstX,$dstY,$srcX,$srcY,$srcImgW,$srcImgH)
{
if(function_exists("imagecreatetruecolor"))
{
@$creatImg = ImageCreateTrueColor($creatW,$creatH);
if($creatImg)
ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
else
{
$creatImg=ImageCreate($creatW,$creatH);
ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
}
}
else
{
$creatImg=ImageCreate($creatW,$creatH);
ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
}
return $creatImg;
}
//輸出圖片,link---只輸出,不保存文件。file--保存為文件
function EchoImage($img,$to_File)
{
switch($this->echoType)
{
case "link":
if(function_exists('imagejpeg')) return ImageJpeg($img);
else return ImagePNG($img);
break;
case "file":
if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);
else return ImagePNG($img,$to_File);
break;
}
}
}
?>
- PHP生成等比縮略圖類和自定義函數(shù)分享
- PHP生成自適應(yīng)大小的縮略圖類及使用方法分享
- 超級(jí)好用的一個(gè)php上傳圖片類(隨機(jī)名,縮略圖,加水印)
- php 方便水印和縮略圖的圖形類
- PHP圖片庫(kù)imagemagick安裝方法
- windows7下安裝php的imagick和imagemagick擴(kuò)展教程
- PHP基于GD庫(kù)的縮略圖生成代碼(支持jpg,gif,png格式)
- php 使用GD庫(kù)為頁(yè)面增加水印示例代碼
- php利用GD庫(kù)生成縮略圖示例
- PHP5中GD庫(kù)生成圖形驗(yàn)證碼(有漢字)
- PHP用GD庫(kù)生成高質(zhì)量的縮略圖片
- php實(shí)現(xiàn)的支持imagemagick及gd庫(kù)兩種處理的縮略圖生成類
相關(guān)文章
php數(shù)組函數(shù)序列 之shuffle()和array_rand() 隨機(jī)函數(shù)使用介紹
shuffle與array_rand定義和用法,需要的朋友可以參考下。2011-10-10PHP使用HTML5 FileApi實(shí)現(xiàn)Ajax上傳文件功能示例
這篇文章主要介紹了PHP使用HTML5 FileApi實(shí)現(xiàn)Ajax上傳文件功能,結(jié)合實(shí)例形式分析了HTML5 FileApi的功能、原理及php使用HTML5 FileApi實(shí)現(xiàn)ajax上傳的相關(guān)操作技巧,需要的朋友可以參考下2019-07-07PHP設(shè)計(jì)模式之建造者模式定義與用法簡(jiǎn)單示例
這篇文章主要介紹了PHP設(shè)計(jì)模式之建造者模式定義與用法,簡(jiǎn)單描述了建造者模式的概念、原理并結(jié)合實(shí)例形式分析了建造者模式的具體定義與使用方法,需要的朋友可以參考下2018-08-08php 去除html標(biāo)記--strip_tags與htmlspecialchars的區(qū)別詳解
本篇文章是對(duì)php中去除html標(biāo)記以及strip_tags與htmlspecialchars的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP基于正則批量替換Img中src內(nèi)容實(shí)現(xiàn)獲取縮略圖的功能示例
這篇文章主要介紹了PHP基于正則批量替換Img中src內(nèi)容實(shí)現(xiàn)獲取縮略圖的功能,涉及php針對(duì)頁(yè)面img元素的正則匹配與替換操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06關(guān)于file_get_contents返回為空或函數(shù)不可用的解決方案
本篇文章是對(duì)file_get_contents返回為空或函數(shù)不可用的解決方案進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06thinkphp 一個(gè)頁(yè)面使用2次分頁(yè)的實(shí)現(xiàn)方法
thinkphp內(nèi)置ORG.Util.Page方法分頁(yè),使分頁(yè)變得非常簡(jiǎn)單快捷。 但是如果一個(gè)頁(yè)面里需要使用2次分頁(yè),就會(huì)產(chǎn)生沖突,這里先記錄下百度來(lái)的解決辦法。需要的朋友可以參考下2013-07-07