サムネイルを生成するための PHP クラス コード_PHP チュートリアル

WBOY
リリース: 2016-07-21 15:49:39
オリジナル
742 人が閲覧しました


/**
* 機能: サムネイルの生成
* 著者: phpox
* 日付: Thu May 17 09:57:05 CST 2007
*/

class CreatMiniature
{
//パブリック変数
var $srcFile="" //元の画像
var $echoType; //出力画像タイプ、link- - ファイルとして保存しないでください - ファイルとして保存します
var $srcW="" // 元の画像の幅
var $srcH=""; height

//変数の設定と初期化
function SetVar($srcFile,$echoType)
{
if (!file_exists($srcFile)){
echo 'ソース画像ファイルが存在しません!'
exit();
}
$this->echoType=$echoType;

$info = ""
$data = GetImageSize($this->srcFile,$info); switch ($ data[2])
{
case 1:
if(!function_exists("imagecreatefromgif")){
echo "GD ライブラリでは GIF 形式の画像を使用できません。Jpeg または PNG 形式を使用してください!return";
exit();
}
$this->im = ImageCreateFromGIF($this->srcFile);
break;
ケース 2 :
if(!function_exists("imagecreatefromjpeg")){
echo "GD ライブラリでは jpeg 形式の画像を使用できません。他の形式の画像を使用してください。戻り";
exit();
}
$this->im = ImageCreateFromJpeg($this->srcFile);
ブレイク;
ケース 3:
$this->im = ImageCreateFromPNG($this->srcFile)
}
$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{
$ftoW=$toW*($this-> srcH/$this->srcW);
}
else
{
$ftoH=$toH;
$ftoW=$ftoH*($this->srcW/$this->srcH); ($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);
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{
$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/$ Hまで;
$srcWH=$this->srcW/$this->srcH;
if($toWH{
$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{
ImageCopy($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);
}
else if($ftoH{
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);
ImageCopyResize($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
}
}
else
{
$creatImg=ImageCreate($creatW,$creatH);
ImageCopyResize($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
}
$creatImg を返します;
}

//出力図片、リンク---出力のみ、文書を保存しない。file--文書として保存
function EchoImage($img,$to_File)
{
switch($this->echoType)
{
case "link":
if(function_exists('imagejpeg')) return ImageJpeg($img);
それ以外の場合は ImagePNG($img) を返します。
休憩;
case "file":
if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);
それ以外の場合は、ImagePNG($img,$to_File) を返します。
休憩;
}
}
}
?>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/319502.html技術記事 ?php /*** 機能: サムネイルの生成 * 著者: phpox * 日付: Thu May 17 09:57:05 CST 2007*/ class CreatMiniature { //パブリック変数 var $srcFile="" //元の画像 var $echoType; //出力画像...
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!