Home > Backend Development > PHP Tutorial > PHP class to generate thumbnails_PHP tutorial

PHP class to generate thumbnails_PHP tutorial

WBOY
Release: 2016-07-15 13:22:25
Original
785 people have browsed it

PHP code

/**

* Function: Generate thumbnails

* Author: phpox

* Date: Thu May 17 09:57:05 CST 2007

*/

class CreatMiniature

{

//Public variables

var $srcFile=""; //Original image

var $echoType; //Output image type, link--do not save as file; file--save as File

var $im=""; //Temporary variable

var $srcW=""; //Original image width

var $srcH=""; / /Original image height

//Set variables and initialization

function SetVar($srcFile,$echoType)

{

if (!file_exists($ srcFile)){

echo 'The source image file does not exist!';

exit();

}

$this->srcFile= $srcFile;

$this->echoType=$echoType;

$info = "";

$data = GetImageSize($this->srcFile,$ info);

switch ($data[2])

{

case 1:

if(!function_exists("imagecreatefromgif")){

echo "Your GD library cannot use GIF format images, please use Jpeg or PNG format!Return" ;

exit();

}

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

break;

case 2:

if(!function_exists("imagecreatefromjpeg")){

echo "Your GD library cannot use images in jpeg format, please use images in other formats ! return";

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

}

//Generate distorted thumbnails

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

}

//Generate scaled thumbnails

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

}

}

//Generate the minimum cropped thumbnail

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

}

//Generate thumbnail with background fill

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

}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446981.htmlTechArticlePHP code?php /*** Function: Generate thumbnails * Author: phpox * Date: Thu May 17 09:57:05 CST 2007*/ class CreatMiniature { //Public variable var $srcFile=" "; //Original image var $echoType; /...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template