PHP Smart image generation thumbnail class _php tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg
A PHP smart image generation thumbnail class, can be automatically based on the size of your image to generate such proportions of the picture Oh, the need for friends can refer to.
The code is as follows Copy Code
< PHP

/*************************************** * Author: Falling Dream Scorpio (beluckly)
* Completion time: 2006-12-18
* Class Name: Creatminiature
* Function: Generate multiple types of thumbnails
* Basic parameters: $srcFile, $echoType
* The parameters used in the method:
$toFile, the resulting file
$toW, the resulting width
$toH, the resulting high
$BK 1, the background color parameter is the highest of 255
$BK 2, background color parameters
$BK 3, background color parameters

Example

Include ("thumb.php");
$CM =new creatminiature ();
$CM->setvar ("1.jpg", "file");
$CM->distortion ("Dis_bei.jpg", 150,200);
$CM->prorate ("Pro_bei.jpg", 150,200);
$CM->cut ("Cut_bei.jpg", 150,200);
$CM->backfill ("Fill_bei.jpg", 150,200);

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

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)
{
$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-&GT;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)
{
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);
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/632961.html www.bkjia.com true http://www.bkjia.com/PHPjc/632961.html techarticle a PHP Smart image generation thumbnail class, can be automatically based on the size of your image to generate such proportions of the picture Oh, the need for friends can refer to. Code to copy code like this? PHP/****** ...

  • Related Article

    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.