function is very simple, there are comments in the code, directly to everyone on the code
Copy Code code as follows:
<?php
/**
* Upload pictures to generate thumbnails
*
* Need support for GD2 library
*
* Initialization requires parameter new thumbnails (' original address of thumbnail image ', ' width of thumbnail ', ' height of thumbnail ', ' (optional parameter) ' save path ' thumbnail);
* If the last parameter is not specified, the thumbnail is saved by default in the small folder in the original image's directory.
* If the small folder is not present, the small folder will be created automatically
*
* After initialization, you need to invoke method produce create thumbnails
* $thumbnails = new thumbnails (' ...);
* $thumbnails->produce ();
*
* Where you can get information about the original picture, width, height, and picture mime
*
* $thumbnails->getimagewidth (); int picture width
* $thumbnails->getimageheight (); int picture Height
* $thumbnails->getimagemime (); MIME of a string picture
*
* $thumbnails->truesize (); Array this is a value that contains the width and height of the image after the scale
* $size = Array (' width ' => ', ' height ' => ');
* Get the width and height of the picture, etc.
* The width of the $size [' width ']//, etc than the thumbnail
* $size [' height ']//]
*
*/
Class thumbnails{
Private $IMGSRC; The path of the picture
Private $SAVESRC; The save path to the picture, which defaults to empty
Private $canvasWidth; The width of the canvas
Private $canvasHeight; The height of the canvas
Private $im; Canvas Resources
Private $DM; Copy a resource returned by a picture
/**
* Initialize class, load related settings
*
* @param $imgSrc The path to the thumbnail image
* @param $canvasWidth the width of the thumbnail
* @param $canvasHeight The height of the thumbnail
*/
Public function __construct ($IMGSRC, $canvasWidth, $canvasHeight, $SAVESRC =null)
{
$this->imgsrc = $IMGSRC;
$this->canvaswidth = $canvasWidth;
$this->canvasheight = $canvasHeight;
$this->savesrc = $SAVESRC;
}
/**
* Generate thumbnails
*/
Public function Produce ()
{
$this->createcanvas ();
$this->judgeimage ();
$this->copyimage ();
$this->headerimage ();
}
/**
* Get information for loading pictures
*
* Contains length, width, picture type
*
* @return array contains the image length, width, mime arrays
*/
Private Function Getimageinfo ()
{
Return getimagesize ($this->imgsrc);
}
/**
* Get the length of the picture
*
* @return the width of the int picture
*/
Public Function Getimagewidth ()
{
$imageInfo = $this->getimageinfo ();
return $imageInfo [' 0 '];
}
/**
* Get Picture height
*
* @return the height of the int picture
*/
Public Function Getimageheight ()
{
$imageInfo = $this->getimageinfo ();
return $imageInfo [' 1 '];
}
/**
* Get the type of picture
*
* @return The MIME value of the picture
*/
Public Function Getimagemime ()
{
$imageInfo = $this->getimageinfo ();
return $imageInfo [' MIME '];
}
/**
* Create Canvas
*
* Put the created canvas resource into the property $this->im at the same time
*/
Private Function Createcanvas ()
{
$size = $this->truesize ();
$this->im = Imagecreatetruecolor ($size [' width '], $size [' height ']);
}
/**
* Judge the MIME value of the picture, determine the function used
*
* Also put the created picture resources into the $THIS->DM
*/
Private Function Judgeimage ()
{
$mime = $this->getimagemime ();
Switch ($mime)
{
Case ' image/png ': $DM = imagecreatefrompng ($this->imgsrc);
Break
Case ' image/gif ': $DM = imagecreatefromgif ($this->imgsrc);
Break
Case ' image/jpg ': $DM = imagecreatefromjpeg ($this->imgsrc);
Break
Case ' image/jpeg ': $DM = imagecreatefromgjpeg ($this->imgsrc);
Break
}
$this->DM = $DM;
}
/**
* Determine the width and height of the thumbnail image
*
* This width and height also as the canvas size
*
* @return Array picture After the size of the proportional thumbnail
*/
Public Function Truesize ()
{
$proportionW = $this->getimagewidth ()/$this->canvaswidth;
$proportionH = $this->getimageheight ()/$this->canvasheight;
if ($this->getimagewidth () < $this->canvaswidth) && ($this->getimageheight () < $this-> Canvasheight))
{
$trueSize = Array (' width ' => $this->getimagewidth (), ' height ' => $this->getimageheight ());
}
ElseIf ($proportionW >= $proportionH)
{
$trueSize = Array (' width ' => $this->canvaswidth, ' height ' => $this->getimageheight ()/$proportionW);
}
Else
{
$trueSize = Array (' width ' => $this->getimagewidth ()/$proportionH, ' height ' => $this->canvasheight);
}
return $trueSize;
}
/**
* Copy the picture to the new canvas
*
* The picture will be scaled proportionally and will not deform
*/
Private Function Copyimage ()
{
$size = $this->truesize ();
Imagecopyresized ($this->im, $this->dm, 0, 0, 0, 0, $size [' width '], $size [' height '], $this->getimagewidth () , $this->getimageheight ());
}
/**
* Output the picture
*
* Picture name defaults to the same name as the original picture
*
* Path is large picture in the current directory under the small directory
*
* If the small directory does not exist, it will be created automatically
*/
Public Function Headerimage ()
{
$position = Strrpos ($this->imgsrc, '/');
$imageName = substr ($this->imgsrc, ($position + 1));
if ($this->savesrc)
{
$imageFlode = $this->savesrc. ' /';
}
Else
{
$imageFlode = substr ($this->imgsrc,0, $position). ' /small/';
}
if (!file_exists ($imageFlode))
{
mkdir ($imageFlode);
}
$SAVESRC = $imageFlode. $imageName;
Imagejpeg ($this->im, $SAVESRC);
}
}