PHP-generated thumbnail class Code _php tutorial

Source: Internet
Author: User
/**
* Function: Generate thumbnail image
* Author: phpox
* Date: Thu 09:57:05 CST 2007
*/

Class Creatminiature
{
Public variables
var $srcFile = ""; Original
var $echoType; Output picture type, link--not saved as file; file--saved as File
var $im = ""; Temp variable
var $srcW = ""; Original width
var $srcH = ""; Original height

Setting variables and initialization
function SetVar ($srcFile, $echoType)
{
if (!file_exists ($srcFile)) {
echo ' source picture 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 pictures, please use JPEG or PNG format!" Return ";
Exit ();
}
$this->im = imagecreatefromgif ($this->srcfile);
Break
Case 2:
if (!function_exists ("Imagecreatefromjpeg")) {
echo "Your GD library can not use JPEG format pictures, please use other format pictures!" 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);
}

To create a distorted shape thumbnail
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);
}

To generate a proportionally scaled thumbnail
function prorate ($toFile, $toW, $toH)
{
$toWH = $toW/$toH;
$srcWH = $this->srcw/$this->srch;
if ($toWH <= $srcWH)
{
$ftoW = $toW;
$ftoH = $ftoW * ($this->srch/$this-&GT;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-&GT;SRCW, $this->srch);
return $this->echoimage ($CIMG, $toFile);
Imagedestroy ($CIMG);
}
Else
{
$CIMG = $this->creatimage ($this->im, $this-&GT;SRCW, $this->srch,0,0,0,0, $this-&GT;SRCW, $this->srch);
return $this->echoimage ($CIMG, $toFile);
Imagedestroy ($CIMG);
}
}

Generate the smallest 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-&GT;SRCW);
}
$ALLIMG = $this->creatimage ($this->im, $ctoW, $ctoH, 0,0,0,0, $this-&GT;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);
}

Create a thumbnail of the background fill
function Backfill ($toFile, $toW, $toH, $BK 1=255, $BK 2=255, $BK 3=255)
{
$toWH = $toW/$toH;
$srcWH = $this->srcw/$this->srch;
if ($toWH <= $srcWH)
{
$ftoW = $toW;
$ftoH = $ftoW * ($this->srch/$this-&GT;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, $BK 1, $BK 2, $BK 3); The background color of the fill
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-&GT;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;
}

Output picture, link---only output, do not save the file. file--Save As 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
}
}
}
?>

http://www.bkjia.com/PHPjc/319502.html www.bkjia.com true http://www.bkjia.com/PHPjc/319502.html techarticle PHP/** * Features: Generate thumbnails * Author: phpox * Date: Thu may 09:57:05 CST */class Creatminiature {//public variable var $srcFile = "";//Original Figure Var $echoType; Output Picture ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.