The function is very simple, the code has comments, directly to the code of the People
Copy CodeThe code is as follows:
/**
* Upload images to generate thumbnails
*
* Requires support of GD2 Library
*
* Initialization requires parameter new thumbnails (' original address of thumbnail image ', ' width of thumbnail ', ' height of thumbnail ', ' (optional parameter) thumbnail save path ');
* If the last parameter is not specified, then the thumbnail is saved in the small folder in the same directory as the original image.
* If the small folder does not exist, the small folder will be created automatically
*
* Call method produce to create thumbnails after initialization
* $thumbnails = new thumbnails ("...);
* $thumbnails->produce ();
*
* 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 values after the thumbnail of the picture, etc.
* $size = Array (' width ' = ', ' height ' = ');
* Get the width and height of the image after the thumbnail
* $size the width of [' width ']//, etc. than the thumbnail image
* $size the height of the [' Height ']//, etc. than the thumbnail image
*
*/
Class thumbnails{
Private $IMGSRC; The path to the picture
Private $SAVESRC; Save path of picture, default is empty
Private $canvasWidth; Width of canvas
Private $canvasHeight; The height of the canvas
Private $im; Canvas Resources
Private $DM; Copy the resources returned by the picture
/**
* Initialize class, load related settings
*
* @param $imgSrc The path of the picture that needs to be abbreviated
* @param $canvasWidth the width of the thumbnail image
* @param $canvasHeight The height of the thumbnail image
*/
Public function __construct ($IMGSRC, $canvasWidth, $canvasHeight, $SAVESRC =null)
{
$this->imgsrc = $IMGSRC;
$this->canvaswidth = $canvasWidth;
$this->canvasheight = $canvasHeight;
$this->savesrc = $SAVESRC;
}
/**
* Generate thumbnail image
*/
Public function Produce ()
{
$this->createcanvas ();
$this->judgeimage ();
$this->copyimage ();
$this->headerimage ();
}
/**
* Get information on loading images
*
* Includes length, width, picture type
*
* @return array containing the image length, width, mime
*/
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 image
*/
Public Function Getimageheight ()
{
$imageInfo = $this->getimageinfo ();
return $imageInfo [' 1 '];
}
/**
* Get the type of picture
*
* MIME value of @return picture
*/
Public Function Getimagemime ()
{
$imageInfo = $this->getimageinfo ();
return $imageInfo [' MIME '];
}
/**
* Create Canvas
*
* Also put the created canvas resource into the property $this->im
*/
Private Function Createcanvas ()
{
$size = $this->truesize ();
$this->im = Imagecreatetruecolor ($size [' width '], $size [' height ']);
}
/**
* Determine the MIME value of the image and decide which function to use
*
* 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 image after the thumbnail
*
* This width and height also as the size of the canvas
*
* @return the size of an array image after a 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
*
* Images will be scaled in equal proportions 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
*
* The name of the picture is the same as the original picture name
*
* Path is large picture in small directory under current 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);
}
}
http://www.bkjia.com/PHPjc/754037.html www.bkjia.com true http://www.bkjia.com/PHPjc/754037.html techarticle The function is very simple, the code has the comment, directly to everybody the code the Copy Code code is as follows:? PHP/** * Upload image to generate thumbnails * * requires support of GD2 Library * * required to initialize the parameter ...