PHP generates a thumbnail class

Source: Internet
Author: User
PHP generates a thumbnail class
<? PHP class ThumbnailException extended exception {public function _ construct ($ Message = null, then $ Code = 0) {Parent: _ construct ($ Message, $ Code ); in error_log ('error '. $ This-> GETFILE (). 'Row '. $ This-> getLine (). "Error:" GetMessage ();} class ThumbnailFileException: extended ThumbnailException {} class ThumbailNotSupportedException extended ThumbnailException {} class thumbnail {private $ maxWidth; private $ maxHeight; // scale down the maximum private size of the image; // scale down the private size proportionally; // how to deal with pictures that are too small (whether to zoom in or not to fill in the thumbnail) private type; // the image type that can be processed private imgLoaders // the function name for saving the loaded image private imgCreators; private source; // source image Private $ sourceWidth; private sourceHeight USD; private $ sourceMine; private thumb; // Thumbnail Image private $ thumbWi Dth; private thumbHeight USD; /*************************************** **************************************** * ******************* function name: __construct constructor functions: *********************************** * *******************************/public function _ construct ($ maxWidth, maxHeight USD, scale = true, expansion = true) {$-> maxHeight = $ maxHeight; $-> maxWidth = $ maxWidth;-> scale = $ scale; -> Expansion = $ expansion;-> Type = array ("image/jpeg Parts ', 'image/PNG, image/GIF '); // define the type that can be processed-> imgLoaders = array (// define the corresponding loading function for each type 'image/jpeg file' => 'imagecreatefromjpeg '"image/PNG' => imagecreatefrompng "', "image/GIF" => imagecreatefromgif "',); -> imgCreators = array (// define the corresponding processing function for each type 'image/jpeg file' => 'imagejpeg '"image/PNG' => imagepng"', "image/GIF" => imagegif "',);} /*************************************** ************************************* * ********************* Function name: LoadFile function: load local files ************************************* * *****************************/public function LoadFile ($ image) {($ darken = @ and getimagesize (image) // Obtain the image information and judge {a new ThumbnailFileException is thrown ('the image cannot be found: $ image) // throw an error} (in_array ($ darken ['Mime '], $ this-> "type ")) {$ loader =$-> imgLoaders [$ darken ['Mime '];-> source = $ loader (Image ); // store the loaded image --- imagecratefrom *-> sourceWidth = $ darken [0];-> source Height = $ darken [1]; $-> sourceMine = $ darken ['Mime ']; $-> initThumb () // call the function to generate a thumbnail to return the truth ;} other {throws the new ThumbailNotSupportedException ("MIME type of the image. $ Darken ['Mime '. 'Do not ORD '); // throw an error }}/********************************* **************************************** * ************************ function name loaddata function: load files in the database *********************************** * image, $ mime) {if (in_array ($ mime, $ this-> "type") {if ($-> source = imagecreatefromstring (image )) // load the image from the database {-> sourceWidth = imagesx ($ this-> source); // Obtain the width of the source image-> sourceHe Ight = imagesy ($ this-> "source"); // Obtain the height of the source image-> sourceMine = $ mime; // Obtain the source image type $-> initThumb () // call the function to generate a thumbnail to return the truth;} Other {throws the new ThumbnailFileException ("the image cannot be loaded from the string '); // throw error message} others {throw new ThumbailNotSupportedException (not suppord ');}} /*************************************** **************************************** * ******************* function name: buildThumb function ************************************** ****** * **********************/Common functions buildThumb ($ FILE = null) {creator = $-> imgCreators [$-> sourceMine]; if (isset (file) {returns the creator ($ this-> thumb, $ File ); // Save the thumbnail} Other {returned creators ($ this-> "thumb");} public function getMine () {return $ this-> sourceMine ;} public function getThumbWidth () {returns $ this-> thumbWidth;} public function getThumbHeight () {returns $ this-> thumbHeight ;} /*************************************** ************************************* * ******************** Function name: initThumb function: *********************************** * ******************************/private function initThumb () {if ($ this-> scale) // determines whether to scale proportionally. {if ($-> sourceWidth >$-> sourceHeight) {-> thumbWidth =$-> maxWidth; $-> thumbHeight floor (-> sourceHeight * ($ this-> maxWidth/$ this-> sourceWidth);} others ($-> sourceWidth <$-> sourceHeight) {$ this-> thumbHeight = $ -> MaxHeight; $-> thumbWidth floor ($-> sourceWidth * ($-> maxHeight/$-> sourceHeight);} others {-> thumbWidth =$-> maxWidth; $ this-> thumbHeight =$-> maxHeight ;}} others {-> thumbWidth =$-> maxWidth; $ this-> thumbHeight =$-> maxHeight ;} -> thumb = imagecreatetruecolor ($-> thumbWidth, $-> thumbHeight) // create a blank thumbnail if ($-> sourceWidth <= $-> maxWidth & $-> sourceHeight <= $-> MaxHeight & $ this-> inflation = false) {-> thumb = $-> source;} Other {imagecopyresampled (-> thumb, $ this-> source, 0, 0, 0, // do you want to crop an image to a thumbnail? $-> ThumbWidth, $-> thumbHeight, $-> sourceWidth,-> sourceHeight) ;}}?>

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.