ホームページ  >  記事  >  バックエンド開発  >  スマート PHP サムネイル生成プログラム_PHP チュートリアル

スマート PHP サムネイル生成プログラム_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-20 11:10:31736ブラウズ

これは、写真のサイズを自動的に検出し、同じ比率のサムネイル効果を生成して、必要な友人が参照できるようにする効果です。

コードは次のとおりです コードをコピーします

*作者: belucly
*完成時期: 2006-12-18
*クラス名: CreatMiniature
*機能: 複数種類のサムネイルを生成します
*基本パラメータ: $srcFile、$echoType
*メソッドで使用されるパラメータ:
$toFile、生成されたファイル
$toW、生成された幅
$toH、生成された高さ
$bk1、背景色のパラメータは 255 で、これは最高値です。 bk2, 背景色パラメータ
$ bk3, 背景色パラメータ

*例:

& & lt;? PHP

/** **************************************

*作者: 夢見る蠍 (belucly)

*完成時期: 2006-12-18

*クラス名: CreatMiniature

*機能: 複数種類のサムネイルを生成

*基本パラメータ: $srcFile、$echoType

*メソッドで使用されるパラメータ:

$toFile、生成されたファイル

$toW、生成された幅

$toH、生成された高さ

$bk1、255 を最高とする背景色パラメーター

$bk2、背景色パラメーター

$bk3、背景色パラメーター

*例:

include("thumb.php");

$cm=new CreatMiniature();

$cm->SetVar("1.jpg","file"); >歪み("dis_bei.jpg",150,200);

$cm->比例配分("pro_bei.jpg",150,200);

$cm->カット("cut_bei.jpg" ,150,200);

$cm->BackFill("fill_bei.jpg",150,200);

************************ ***** **************/

Class Creatminiature

{

{

/ /パブリック変数

var $srcFile=""; //元の画像

var $echoType; //出力画像の種類、ファイルとして保存されません -- ファイルとして保存されます

var $im= " "; //一時変数

var $srcH=""; //元の画像の幅

//変数と初期化を設定

function SetVar( $ srcFile,$echoType)

{

$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 形式を使用してください。 this->im = ImageCreateFromGIF($this->srcFile);

break;

case 2:

if(!function_exists("imagecreatefromjpeg")) {

echo "GD ライブラリでは jpeg 形式を使用できません。画像には他の形式を使用してください。Return";

exit();

}

$this->im = ImageCreateFromJpeg($this->srcFile);

break;

ケース 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{

$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);

}

}

//After最小クロップの生成

関数 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/$toH;

$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< $toW)

{

ImageCopyMerge($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ ftoH,100);

}

else if($ftoH<$toH)

{

ImageCopyMerge($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW ,$ftoH,100);

}

*/

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);

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);

}

return $creatImg;

}

//出力图片、リンク---のみ出力、非保存文書。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;

}

}

}

?>


www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/444719.html技術記事これは、転送される画像サイズを自動的に検出して、等比率の簡易画像効果を生成するもので、必要な友人は以下を参照できます。
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。