Example of generating thumbnails for uploading images using php

Source: Internet
Author: User

The function is very simple. There are comments in the code and the code is directly written to everyone.

Copy codeThe Code is as follows:
<? Php
/**
* Upload an image to generate a thumbnail
*
* GD2 database support is required
*
* During initialization, the new thumbnails parameter is required ('original address of the scaled image ', 'width of the thumbnail', 'height of the thumbnail ',' (optional) saved path of the thumbnail ');
* If the last parameter is not specified, the thumbnail is saved in the small folder in the directory where the original image is located by default,
* If the small Folder does not exist, the small folder is automatically created.
*
* After initialization, you must call the produce method to create a thumbnail.
* $ Thumbnails = new thumbnails (''....);
* $ Thumbnails-> produce ();
*
* You can obtain information about the original image, such as width, height, and image mime.
*
* $ Thumbnails-> getImageWidth (); // int Image Width
* $ Thumbnails-> getImageHeight (); // int Image Height
* $ Thumbnails-> getImageMime (); // mime of the string image
*
* $ Thumbnails-> trueSize (); // array this is an array containing the width and height values after scaling down the image.
* $ Size = array ('width' => '', 'height' => '');
* Obtain the width and height of the image after the proportional Scaling
* $ Size ['width'] // proportional width of the thumbnail
* $ Size ['height'] // proportional height of the thumbnail
*
*/
Class thumbnails {

Private $ imgSrc; // image path
Private $ saveSrc; // save path of the image. The default value is null.
Private $ canvasWidth; // The width of the canvas.
Private $ canvasHeight; // canvas height
Private $ im; // canvas Resource
Private $ dm; // copy the resources returned by the image

/**
* Initialize the class and load related settings.
*
* @ Param $ the path of the image to be scaled down by imgSrc
* @ 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;
}

/**
* Generating thumbnails
*/
Public function produce ()
{
$ This-> createCanvas ();
$ This-> judgeImage ();
$ This-> copyImage ();
$ This-> headerImage ();
}

/**
* Obtain the information of the uploaded image.
*
* Contains the length, width, and image type.
*
* @ Return array contains the image length, width, and mime array.
*/
Private function getImageInfo ()
{
Return getimagesize ($ this-> imgSrc );
}

/**
* Get the image Length
*
* @ Return int the Image Width
*/
Public function getImageWidth ()
{
$ ImageInfo = $ this-> getImageInfo ();
Return $ imageInfo ['0'];
}

/**
* Obtain the Image Height.
*
* @ Return int the Image Height
*/
Public function getImageHeight ()
{
$ ImageInfo = $ this-> getImageInfo ();
Return $ imageInfo ['1'];
}

/**
* Obtain the image type.
*
* @ Return the mime value of the image
*/
Public function getImageMime ()
{
$ ImageInfo = $ this-> getImageInfo ();
Return $ imageInfo ['mime '];
}

/**
* Create a canvas
*
* Add the created canvas resource to 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 determine the function used
*
* Add the created image resource to $ 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 scaled image.
*
* The width and height are also used as the size of the canvas.
*
* @ Return array the size of the image after the scale is reduced.
*/
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 image to the new canvas.
*
* The image is scaled proportionally without deformation.
*/
Private function copyImage ()
{
$ Size = $ this-> trueSize ();
Imagecopyresized ($ this-> im, $ this-> dm, 0, 0, 0, 0, $ size ['width'], $ size ['height'], $ this-> getImageWidth (), $ this-> getImageheight ());
}

/**
* Output images
*
* The image name is the same as the original image name by default.
*
* The path is the small directory in the current directory of the large image.
*
* If the small directory does not exist, it is automatically created.
*/
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 );
}
}

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.