Php-encapsulated image (thumbnail) processing class complete instance,

Source: Internet
Author: User

Php-encapsulated image (thumbnail) processing class complete instance,

This article describes the php-encapsulated image (thumbnail) processing class. We will share this with you for your reference. The details are as follows:

<? Php // Image processing tool class Image {// attribute private $ thumb_width; // width private $ thumb_height of the thumbnail; // error attribute public $ thumb_error; // constructor public function _ construct ($ width = 0, $ height = 0) {$ this-> thumb_width = ($ width = 0 )? $ GLOBALS ['config'] ['admin _ goods_thumb '] ['width']: $ width; $ this-> thumb_height = ($ height = 0 )? $ GLOBALS ['config'] ['admin _ goods_thumb '] ['height']: $ height;}/** create a thumbnail * @ param1 string $ src, source image path, /uploads/20150122101010abcdef.gif * @ param2 string $ path, thumbnail save path/uploads/thumb_20150122101010abcdef.gif * @ return thumbnail name */public function makeThumb ($ src, $ path) {// determine whether the source image exists if (! File_exists ($ src) {$ this-> thumb_error = 'the source image does not exist! '; Return false;} // open the source image resource // obtain the available suffix $ ext = $ this-> getFunctionName ($ src ); // gif // name of the patchwork function $ open = 'imagecreatefrom '. $ ext; // imagecreatefromgif $ save = 'image '. $ ext; // imagegif // if not clear; echo $ open, $ save; exit; // Variable Function to open source image resources $ src_img = $ open ($ src ); // use the variable function to open image resources // imagecreatefromgif ($ src) // The thumbnail resource $ dst_img = imagecreatetruecolor ($ this-> thumb_width, $ this-> thumb_height ); // fill the background color with white $ dst_bg_color = ima Gecolorallocate ($ dst_img, 255,255,255); imagefill ($ dst_img, 0, 0, $ dst_bg_color ); // determine the width and height of the aspect ratio $ dst_size = $ this-> thumb_width/$ this-> thumb_height; // obtain the source image data $ file_info = getimagesize ($ src ); $ src_size = $ file_info [0]/$ file_info [1]; // obtain the thumbnail width and height if ($ src_size> $ dst_size) {// the aspect ratio of the source image is greater than the thumbnail $ width = $ this-> thumb_width; $ height = round ($ width/$ src_size );} else {$ height = $ this-> thumb_height; $ width = round ($ heig Ht * $ src_size);} // locate the starting position of the thumbnail $ dst_x = round ($ this-> thumb_width-$ width)/2; $ dst_y = round ($ this-> thumb_height-$ height)/2; // create a thumbnail if (imagecopyresampled ($ dst_img, $ src_img, $ dst_x, $ dst_y, 0, 0, $ width, $ height, $ file_info [0], $ file_info [1]) {// sampling successful: save, save the file to the corresponding path $ thumb_name = 'thumb _'. basename ($ src); $ save ($ dst_img, $ path. '/'. $ thumb_name); // save successfully. return $ thumb_name;} else {// sample failed $ this-> thumb _ Error = 'thumbnail sampling failed! '; Return false ;}}/** get the name of the function to be called in the file * @ param1 string $ file, file name * @ return function string obtained by file extension */private function getFunctionName ($ file) {// get the file suffix $ file_info = pathinfo ($ file ); $ ext = $ file_info ['extension']; // Suffix: gif, png, jpg, jpeg, pjpeg // imagecreatefromgif, imagecreatefromjpeg, imagecreatefrompng // define an array to save the function name $ func = array ('gif' => 'gif', 'png '=> 'png ', 'jpg '=> 'jpeg', 'jpeg '=> 'jpeg', 'pjpeg '=> 'jpeg '); // return value return $ func [$ ext];}

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.