Class code for generating thumbnails in php _ PHP Tutorial

Source: Internet
Author: User
Php generates a thumbnail class code .? Php *** function: generate thumbnails * author: phpox * Date: ThuMay1709: 57: 05CST2007 * classCreatMiniature {public variable var $ srcFile; source image var $ echoType; output image
/**
* Function: generate a thumbnail.
* Author: phpox
* Date: Thu May 17 09:57:05 CST 2007
*/

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)
{
If (! File_exists ($ srcFile )){
Echo '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 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)
{
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;
}
}
}
?>

Http://www.bkjia.com/PHPjc/319502.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/319502.htmlTechArticle? 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; // output image...

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.