最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php生成縮略圖的類代碼

 更新時間:2008年10月02日 01:09:21   作者:  
最近做個項(xiàng)目,正好需要縮略圖的代碼,特給大家分享下
<?php

/**
* 功能:生成縮略圖
* 作者:phpox
* 日期:Thu May 17 09:57:05 CST 2007
*/

class CreatMiniature
{
//公共變量
var $srcFile=""; //原圖
var $echoType; //輸出圖片類型,link--不保存為文件;file--保存為文件
var $im=""; //臨時變量
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庫不能使用GIF格式的圖片,請使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>";
exit();
}
$this->im = ImageCreateFromGIF($this->srcFile);
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
echo "你的GD庫不能使用jpeg格式的圖片,請使用其它格式的圖片!<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;
}
}
}
?>

相關(guān)文章

  • 基于PHP創(chuàng)建Cookie數(shù)組的詳解

    基于PHP創(chuàng)建Cookie數(shù)組的詳解

    本篇文章是對在PHP中創(chuàng)建Cookie數(shù)組的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-07-07
  • php ob_flush,flush在ie中緩沖無效的解決方法

    php ob_flush,flush在ie中緩沖無效的解決方法

    一些版本的 Microsoft Internet Explorer 只有當(dāng)接受到的256個字節(jié)以后才開始顯示該頁面,所以必須發(fā)送一些額外的空格來讓這些瀏覽器顯示頁面內(nèi)容。
    2010-05-05
  • php兩種基本的輸出方及實(shí)例詳解

    php兩種基本的輸出方及實(shí)例詳解

    在本篇文章里小編給大家整理了一篇關(guān)于php兩種基本的輸出方及實(shí)例詳解內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)參考下。
    2021-11-11
  • PHP中函數(shù)gzuncompress無法使用的解決方法

    PHP中函數(shù)gzuncompress無法使用的解決方法

    這篇文章主要介紹了PHP中函數(shù)gzuncompress無法使用的解決方法,文中的介紹的很詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-03-03
  • codeigniter實(shí)現(xiàn)get分頁的方法

    codeigniter實(shí)現(xiàn)get分頁的方法

    這篇文章主要介紹了codeigniter實(shí)現(xiàn)get分頁的方法,涉及使用codeigniter框架查詢數(shù)據(jù)量及針對結(jié)果集進(jìn)行g(shù)et方法分頁的相關(guān)技巧,非常簡單實(shí)用,需要的朋友可以參考下
    2015-07-07
  • 兼容性最強(qiáng)的PHP生成縮略圖的函數(shù)代碼(修改版)

    兼容性最強(qiáng)的PHP生成縮略圖的函數(shù)代碼(修改版)

    寫通用性程序考慮兼容性是很頭痛的事情,關(guān)于用PHP生成縮略圖的代碼很多,不過能完全兼容gd1.6和gd2.x,并能保證縮圖清晰性的代碼幾乎沒有,我把我以前的代碼改了一下,就能實(shí)現(xiàn)了。
    2011-01-01
  • php網(wǎng)頁病毒清除類

    php網(wǎng)頁病毒清除類

    這篇文章主要介紹了php網(wǎng)頁病毒清除類,可實(shí)現(xiàn)針對網(wǎng)頁病毒的簡單清理功能,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-12-12
  • php將字符串隨機(jī)分割成不同長度數(shù)組的方法

    php將字符串隨機(jī)分割成不同長度數(shù)組的方法

    這篇文章主要介紹了php將字符串隨機(jī)分割成不同長度數(shù)組的方法,涉及隨機(jī)數(shù)及字符串操作的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • PHP基于文件存儲實(shí)現(xiàn)緩存的方法

    PHP基于文件存儲實(shí)現(xiàn)緩存的方法

    這篇文章主要介紹了PHP基于文件存儲實(shí)現(xiàn)緩存的方法,實(shí)例分析了smarty模板中php通過文件存儲來實(shí)現(xiàn)緩存的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • php獲取微信基礎(chǔ)接口憑證Access_token

    php獲取微信基礎(chǔ)接口憑證Access_token

    這篇文章主要為大家詳細(xì)介紹了php獲取微信基礎(chǔ)接口憑證Access_token,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08

最新評論

广平县| 公安县| 怀远县| 红原县| 新源县| 古交市| 定远县| 平遥县| 达拉特旗| 大同市| 长岭县| 博湖县| 灵山县| 固镇县| 北票市| 达日县| 巫山县| 依安县| 上林县| 大余县| 江孜县| 白城市| 拉萨市| 营口市| 英山县| 镇康县| 永吉县| 封开县| 墨脱县| 鄂托克前旗| 平谷区| 浦江县| 罗田县| 潮州市| 余庆县| 汉阴县| 阜宁县| 新宁县| 巴塘县| 遵义市| 桂林市|