Php gd thumbnails (for Users)

Source: Internet
Author: User
Tags imagejpeg
/*** Note: This class completes the generation of thumbnails and supports page display and file generation.
* Version 1.0
* @ Author sanshi (sanshi)
* QQ: 35047205
* MSN: sanshi0815@tom.com
* Create 2005/6/18
**************************************** ***************
* @ Param string $ srcfile source file
* @ Param string $ dstfile target file
* @ Param string $ filetype current file type
* @ Param string $ Im image open resource handle
* @ Param array $ imgtype file type definition
*/
Class makeminiature
{
VaR $ srcfile; // source file
VaR $ dstfile; // target file
VaR $ filetype; // file type
VaR $ im; // open the resource handle for the image
VaR $ imgtype = array ("jpg", // file type definition
"GIF ",
"PNG ",
"BMP ");
/**
* Description: Obtain the file type.
* @ Param string $ filename file name
* @ Return Boolean: Return true
*/
Function findtype ($ filename)
{
If ($ type = strstr ($ filename ,"."))
{
$ Type = substr ($ type, 1 );
If (! Strstr ($ type ,"."))
{
$ Var = $ type;
} Else {
Echo "file type error!
";
}
} Else {
Echo "file type error!
";
}
For ($ I = 0; $ I <= count ($ this-> imgtype); $ I ++)
{
If (strcmp ($ this-> imgtype [$ I], $ var) = 0)
{
$ This-> filetype = $ var;
Return true;
} Else {
Return false;
}
}
}
/**
* @ Param $ filetype file type
* @ Return resource open the image resource handle
*/
Function loadimg ($ filetype)
{
$ Type = $ this-> isnull ($ filetype );
Switch ($ type)
{
Case "jpg ":
$ Im = imagecreatefromjpeg ($ this-> srcfile );
Break;
Case "GIF ":
$ Im = imagecreatefromgif ($ this-> srcfile );
Break;
Case "PNG ":
$ Im = imagecreatefrompng ($ this-> srcfile );
Break;
Case "BMP ":
$ Im = imagecreatefromwbmp ($ this-> srcfile );
Break;
Default:
$ Im = 0;
Echo "not you input file type!
"; Break;
}
$ This-> im = $ im;
Return $ im;
}
/**
* Description: determines whether a scalar is null. If not, a variable is returned.
*/
Function isnull ($ var)
{
If (! Isset ($ var) | empty ($ var ))
{
Echo "the variable value is null!
"; Exit (0 );
}
Return $ var;
}/**
* Description: The source file name and the generated file name are set, and the file type is determined.
* Opening a file
* @ Param string srcfile target file
* @ Param string dstfile create a file
*/
Function setparam ($ srcfile, $ dstfile)
{
$ This-> srcfile = $ this-> isnull ($ srcfile );
$ This-> dstfile = $ this-> isnull ($ dstfile );
If (! $ This-> findtype ($ srcfile ))
{
Echo "file type error! ";
}
If (! $ This-> loadimg ($ this-> filetype ))
{
Echo "open". $ this-> srcfile. "error!
";
}
}
/**
* Width of the obtained Image
* @ Param resource im the resource that successfully opens the image
* @ Return int width the width of the image
*/
Function getimgwidth ($ IM)
{
$ Im = $ this-> isnull ($ IM );
$ Width = imagesx ($ IM );
Return $ width;
}
/**
* Indicates the height of the obtained image.
* @ Param resource im the resource that successfully opens the image
* @ Return int height the height of the image
*/
Function getimgheight ($ IM)
{
$ Im = $ this-> isnull ($ IM );
$ Height = imagesy ($ IM );
Return $ height;
}
/**
* Image creation
* @ Param resource im the resource that successfully opens the image
* @ Param int scale the percentage of the image generated to the original image
* @ Param Boolean whether the page is output to the page
*/
Function createimg ($ im, $ scale, $ page)
{
$ Im = $ this-> isnull ($ IM );
$ Scale = $ this-> isnull ($ scale );
$ Srcw = $ this-> getimgwidth ($ IM );
$ Srch = $ this-> getimgheight ($ IM );
$ Detw = round ($ srcw * $ scale/100 );
$ Deth = round ($ srch * $ scale/100 );
// $ Om = imagecreate ($ detw, $ deth); // common usage
$ Om = imagecreatetruecolor ($ detw, $ deth); // true colors are required for the GB Library
// Imagecopyresized
($ Om, $ im, 0, 0, 0, $ detw, $ deth, $ srcw, $ srch );
Imagecopyresampled
($ Om, $ im, 0, 0, 0, $ detw, $ deth, $ srcw, $ srch );
$ This-> showimg ($ om, $ this-> filetype, $ page );
}
/**
* Image creation
* @ Param resource im the resource that successfully opens the image
* @ Param int scale the percentage of the image generated to the original image
* @ Param Boolean whether the page is output to the page
*/
Function createnewimg ($ im, $ width, $ height, $ page)
{
$ Im = $ this-> isnull ($ IM );
// $ Scale = $ this-> isnull ($ scale );
$ Srcw = $ this-> getimgwidth ($ IM );
$ Srch = $ this-> getimgheight ($ IM );
$ Detw = $ this-> isnull ($ width );
$ Deth = $ this-> isnull ($ height );
// $ Om = imagecreate ($ detw, $ deth); // common usage
$ Om = imagecreatetruecolor ($ detw, $ deth); // true colors are required for the GB Library
// Imagecopyresized
($ Om, $ im, 0, 0, 0, $ detw, $ deth, $ srcw, $ srch );
Imagecopyresampled
($ Om, $ im, 0, 0, 0, $ detw, $ deth, $ srcw, $ srch );
$ This-> showimg ($ om, $ this-> filetype, $ page );
}
/**
* Indicates the output image creation failure prompt.
* @ Param Boolean determines whether to output
*/
Function inputerror ($ Boolean)
{
If (! $ Boolean)
{
Echo "IMG input error!
";
}
}
/**
* The output position and type of the image are displayed based on the conditions.
* @ Param resource $ om image output Resource
* @ Param string $ type: Type of the output image. The source image type is used now.
* @ Param Boolean $ whether the page is displayed on the page
*/
Function showimg ($ om, $ type, $ page)
{
$ Om = $ this-> isnull ($ om );
$ Type = $ this-> isnull ($ type );
Switch ($ type)
{
Case "jpg ":
If ($ page)
{
$ Suc = imagejpeg ($ om );
$ This-> inputerror ($ Suc );
} Else {
$ Suc = imagejpeg ($ om, $ this-> dstfile );
$ This-> inputerror ($ Suc );
}
Break;
Case "GIF ":
If ($ page)
{
$ Suc = imagegif ($ om );
$ This-> inputerror ($ Suc );
} Else {
$ Suc = imagegif ($ om, $ this-> dstfile );
$ This-> inputerror ($ Suc );
}
Break;
Case "PNG ":
If ($ page)
{
$ Suc = imagepng ($ om );
$ This-> inputerror ($ Suc );
} Else {
$ Suc = imagepng ($ om, $ this-> dstfile );
$ This-> inputerror ($ Suc );
}
Break;
Case "BMP ":
If ($ page)
{
$ Suc = imagewbmp ($ om );
$ This-> inputerror ($ Suc );
} Else {
$ Suc = imagewbmp ($ om, $ this-> dstfile );
$ This-> inputerror ($ Suc );
}
Break;
Default:
Echo "not you input file type!
"; Break;
}
}
}
Use
$ File = new makeminiature ();
$ File-> setparam ("img/logo.jpg", "img/logo1.jpg"); // you can set the source file to generate a file.
$ File-> createimg ($ file-> im, 200, true); // generates an image in proportion to 200%, which is displayed on the page.
$ File-> createimg ($ file-> im, 200, false); // generates an image in proportion to 200%. The generated image is saved to the name and path set above.
$ File-> createnewimg ($ file-> im, 100,100, true); // generate an image based on its own length and width, save it or display it on the page

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.