Example of generating image thumbnails using PHP,

Source: Internet
Author: User

Example of generating image thumbnails using PHP,

This example describes how to generate image thumbnails in PHP. We will share this with you for your reference. The details are as follows:

Class App_image_helper {protected $ imgFileName; protected $ imgWidth; protected $ imgHeight; protected $ imgMime; protected $ imgResource; static $ imgMineList = array ('jpeg '=> 'image/jpeg ', 'gif' => 'image/gif', 'png '=> 'image/png', 'wbmp '=> 'image/wbmp ',); /*** initialize the Image Based on the file name, * calculate the width, height, and image type of the given image, and obtain the image resources and save them to the memory, so that you can use * App_image_helper constructor next time. ** @ param $ fileName */public function _ construc T ($ fileName) {$ this-> imgFileName = $ fileName; list ($ this-> imgWidth, $ this-> imgHeight, $ this-> imgMime) = $ this-> getImageInfo ($ this-> imgFileName); $ this-> imgResource = $ this-> getImageResource ($ this-> imgFileName );} /*** obtain related width, height, and MIME type information based on the image path ** @ param $ fileName ** @ return array | null */protected function getImageInfo ($ fileName) {$ result = null; if (is_file ($ fileName) {$ tmpImageInfo = getim Agesize ($ fileName); if ($ tmpImageInfo) {$ result = array ($ tmpImageInfo [0], $ tmpImageInfo [1], $ tmpImageInfo ['mime ']);} return $ result;}/*** convert the image file to the resource type ** @ param $ fileName ** @ return null | resource */protected function getImageResource ($ fileName) {$ image = null; if (is_file ($ fileName) {switch ($ this-> imgMime) {case self: $ imgMineList ['jpeg ']: $ image = imagecreatefromjpeg ($ fileName); Break; case self: $ imgMineList ['gif']: $ image = imagecreatefromgif ($ fileName); break; case self: $ imgMineList ['png']: $ image = imagecreatefrompng ($ fileName); break; case self: $ imgMineList ['wbmp ']: $ image = imagecreatefromwbmp ($ fileName); break; default: break ;}} return $ image;}/*** you can scale the image according to the fixed width or proportional ratio, or by percentage, proportional scaling image ** @ param int $ width * @ param int $ percent ** @ return array | null */protected f Unction getSizeByScale ($ width = 360, $ percent = 1) {$ result = null; if ($ this-> imgWidth & $ this-> imgHeight) {if ($ width) {$ result = array ($ width, intval ($ width * $ this-> imgHeight/$ this-> imgWidth);} elseif ($ percent) {$ result = array (intval ($ this-> imgWidth * $ percent), intval ($ this-> imgHeight * $ percent) ;}} return $ result ;} /*** out-of-box call ** @ param int $ percentOrWidth int integer indicates that the image is scaled to a fixed width., 0.0 ~ 0.99999 indicates the zoom percentage * @ param null $ fileName * @ param int $ quality * @ param bool $ reSample to ressample the image, the default value is ** @ return bool */public function createImage ($ percentOrWidth = 1, $ fileName = null, $ quality = 75, $ reSample = true) {$ result = false; $ fileName? Header ('content-Type: '. $ this-> imgMime): false; $ size = $ this-> getSizeByScale ($ percentOrWidth <= 1 )? Null: $ percentOrWidth, $ percentOrWidth); if ($ size) {$ thumb = imagecreatetruecolor ($ size [0], $ size [1]); if ($ reSample) {imagecopyresampled ($ thumb, $ this-> imgResource, 0, 0, 0, 0, $ size [0], $ size [1], $ this-> imgWidth, $ this-> imgHeight);} else {imagecopyresized ($ thumb, $ this-> imgResource, 0, 0, 0, 0, $ size [0], $ size [1], $ this-> imgWidth, $ this-> imgHeight);} $ result = imagejpeg ($ thumb, $ fileName, $ quality );} return $ result ;}}

PS: Here are some useful image processing tools for your reference:

Online image conversion BASE64 tool:
Http://tools.jb51.net/transcoding/img2base64

ICO icon generation tool:
Http://tools.jb51.net/aideddesign/ico_img

Online Email icon creation tools:
Http://tools.jb51.net/email/emaillogo

Online image format conversion (jpg/bmp/gif/png) tool:
Http://tools.jb51.net/aideddesign/picext

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.