Smart PHP image shrinking

Source: Internet
Author: User
Smart PHP image shrinking function: generate various types of thumbnail class names: CreatMiniature basic parameters: $ srcFile, $ echoType * author: beluckly)
* Completion Time: 2006-12-18
* Class name: CreatMiniature
* Function: generate various types of thumbnails.
* Basic parameters: $ srcFile, $ echoType
* Parameters used by the method:
$ ToFile: the generated file.
$ ToW: the generated width.
$ ToH, the generated height
$ Bk1. The highest background color parameter is 255.
$ Bk2, background color parameter
$ Bk3, background color parameter


* Example:


<? Php
 
/***************************************
* Author: beluckly)
* Completion Time: 2006-12-18
* Class name: CreatMiniature
* Function: generate various types of thumbnails.
* Basic parameters: $ srcFile, $ echoType
* Parameters used by the method:
$ ToFile: the generated file.
$ ToW: the generated width.
$ ToH, the generated height
$ Bk1. The highest background color parameter is 255.
$ Bk2, background color parameter
$ Bk3, background color parameter
 
* 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-& gt; Cut ("cut_bei.jpg", 150,200 );
$ Cm-> BackFill ("fill_bei.jpg", 150,200 );
 
***************************************/
 
Class CreatMiniature
{
// Public variables
Var $ srcFile = ""; // source image
Var $ echoType; // The type of the output image, link -- not saved as a file; file -- saved as a file
Var $ im = ""; // temporary variable
Var $ srcW = ""; // source image width
Var $ srcH = ""; // original height
 
// Set 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 images in GIF format. 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 a distorted thumbnail
Function Distortion ($ toFile, $ toW, $ toH)
{
$ CImg = $ this-> CreatImage ($ this-> im, $ toW, $ toH, 0, 0, 0, $ this-> srcW, $ this-> srcH );
Return $ this-> EchoImage ($ cImg, $ toFile );
ImageDestroy ($ cImg );
}

// Generate a scaled chart
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, $ this-> srcW, $ this-> srcH );
Return $ this-> EchoImage ($ cImg, $ toFile );
ImageDestroy ($ cImg );
}
Else
{
$ CImg = $ this-> CreatImage ($ this-> im, $ this-> srcW, $ this-> srcH, 0, 0, 0, $ this-> srcW, $ this-> srcH );
Return $ this-> EchoImage ($ cImg, $ toFile );
ImageDestroy ($ cImg );
}
}

// Generate a thumbnail after minimum cropping
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, $ this-> srcW, $ this-> srcH );
$ CImg = $ this-> CreatImage ($ allImg, $ toW, $ toH, 0, ($ ctoW-$ toW)/2, ($ ctoH-$ toH)/2, $ toW, $ toH );
Return $ this-> EchoImage ($ cImg, $ toFile );
ImageDestroy ($ cImg );
ImageDestroy ($ allImg );
}
 
// Generate a thumbnail for background filling
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 );
}
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); // fill in the background color
ImageFilledRectangle ($ cImg, 0, 0, $ toW, $ toH, $ backcolor );
If ($ this-> srcW> $ toW | $ this-> srcH> $ toH)
{
$ ProImg = $ this-> CreatImage ($ this-> im, $ ftoW, $ ftoH, 0, 0, 0, $ this-> srcW, $ this-> srcH );
/*
If ($ ftoW <$ toW)
{
ImageCopyMerge ($ cImg, $ proImg, ($ toW-$ ftoW)/100, $ ftoW, $ ftoH );
}
Else if ($ ftoH <$ toH)
{
ImageCopyMerge ($ cImg, $ proImg, 0, ($ toH-$ ftoH)/100, 0, $ ftoW, $ ftoH );
}
*/
If ($ ftoW <$ toW)
{
ImageCopy ($ cImg, $ proImg, ($ toW-$ ftoW)/2, 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, $ 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 image, link --- only output, not save file. File -- save as a 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;
}
}
 
}
?>

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.