Php image processing instance

Source: Internet
Author: User
This article mainly introduces the php image processing class, which involves modifying the size of php images and adding water.

This article mainly introduces the php image processing class, which involves modifying the size of php images and adding water.

This article describes the php image processing class. Share it with you for your reference. The details are as follows:

<? Php/*** Image class */class Image {/*** @ var string $ fileName file name * @ access private */private $ fileName = ''; /*** @ var gd resource $ imageResource original image * @ access private */private $ imageResource = NULL; /*** @ var int $ imageWidth Original Image Width * @ access private */private $ imageWidth = NULL; /*** @ var int $ imageHeight Original Image Height * @ access private */private $ imageHeight = NULL;/*** @ var int $ imageType original image type * @ Access private */private $ imageType = NULL;/*** @ var int $ imageWidth Original Image width * @ access private */public $ width = NULL; /*** @ var int $ imageHeight Original Image height * @ access private */public $ height = NULL; /*** @ var int $ imageType original image type * @ access private */public $ type = NULL; /*** @ var int $ newResource new image * @ access private */private $ newResource = NULL;/*** @ var int $ newResType new image type * @ access pr Ivate */private $ newResType = NULL;/*** constructor * @ param string $ fileName file name */public function _ construct ($ fileName = NULL) {$ this-> fileName = $ fileName; if ($ this-> fileName) {$ this-> getSrcImageInfo ();}} /*** obtain the source image information * @ access private * @ return void */private function getSrcImageInfo () {$ info = $ this-> getImageInfo (); $ this-> imageWidth = $ info [0]; $ this-> imageHeight = $ info [1]; $ this-> image Type = $ info [2]; $ this-> width = $ info [0]; $ this-> height = $ info [1]; $ this-> type = $ info [2];} /*** retrieve image information * @ param string $ fileName file name * @ access private * @ return array */private function getImageInfo ($ fileName = NULL) {if ($ fileName = NULL) {$ fileName = $ this-> fileName;} $ info = getimagesize ($ fileName); return $ info ;} /*** create source image GD resource * @ access private * @ return void */private function crea TeSrcImage () {$ this-> imageResource = $ this-> createImageFromFile ();} /*** Create Image GD resource with the data file * @ param string $ fileName file name * @ return gd resource */public function createImageFromFile ($ fileName = NULL) {if (! $ FileName) {$ fileName = $ this-> fileName; $ imgType = $ this-> imageType;} if (! Is_readable ($ fileName) |! File_exists ($ fileName) {throw new Exception ('unable to open file "'. $ fileName.'" ');} if (! $ ImgType) {$ imageInfo = $ this-> getImageInfo ($ fileName); $ imgType = $ imageInfo [2];} switch ($ imgType) {case IMAGETYPE_GIF: $ tempResource = hour ($ fileName); break; case IMAGETYPE_JPEG: $ tempResource = imagecreatefromjpeg ($ fileName); break; case IMAGETYPE_PNG: $ tempResource = hour ($ fileName); break; case IMAGETYPE_WBMP: $ tempResource = imagecreatefromwbmp ($ fileName); break; case IMAGETYPE_XBM: $ tempResource = imagecreatefromxbm ($ fileName); break; default: throw new Exception ('unsupport image type');} return $ tempResource ;} /*** change image size ** @ param int $ width * @ param int $ height * @ param string $ flag generally, 4 is allowed, if 1 is not allowed, and a 4-3 image is required: 4 = if the length is too long, the system automatically removes part of the value 0 = the length and width is converted to the value 1 specified by the parameter = proportional scaling. The system automatically determines whether the length is too long or too wide, length and width constraints within the specified parameter 2 = width constraints zoom 3 = height constraints Zoom * @ param string $ bgcolor if not null, use the color specified by this parameter as the background color, and the image is expanded to the specified height and width. This parameter should be an array; * @ return string */public function resizeImage ($ width, $ height, $ flag = 1, $ bgcolor = null) {$ widthRatio = $ width/$ this-> imageWidth; $ heightRatio = $ height/$ this-> imageHeight; switch ($ flag) {case 1: if ($ this-> imageHeight <$ height & $ this-> imageWidth <$ width) {$ endWidth = $ this-> imageWidth; $ endHeight = $ this-> imageHeight; // return;} elseif ($ this-> imageHeight * $ widthRatio)> $ height) {$ endWidth = ceil ($ this-> imageWidth * $ heightRatio ); $ endHeight = $ height;} else {$ endWidth = $ width; $ endHeight = ceil ($ this-> imageHeight * $ widthRatio);} break; case 2: $ endWidth = $ width; $ endHeight = ceil ($ this-> imageHeight * $ widthRatio); break; case 3: $ endWidth = ceil ($ this-> imageWidth * $ heightRatio); $ endHeight = $ height; break; case 4: $ endwidth1 = $ width; $ endHeight2 = $ height; if ($ this-> imageHeight <$ height & $ this-> imageWidth <$ width) {$ endWidth = $ this-> imageWidth; $ endHeight = $ this-> imageHeight; // return;} elseif ($ this-> imageHeight * $ widthRatio) <$ height) {$ endWidth = ceil ($ this-> imageWidth * $ heightRatio ); $ endHeight = $ height;} else {$ endWidth = $ width; $ endHeight = ceil ($ this-> imageHeight * $ widthRatio);} break; default: $ endWidth = $ width; $ endHeight = $ height; break;} if ($ this-> imageResource = NULL) {$ this-> createSrcImage ();} if ($ bgcolor) {$ this-> newResource = imagecreatetruecolor ($ width, $ height); $ bg = ImageColorAllocate ($ this-> newResource, $ bgcolor [0], $ bgcolor [1], $ bgcolor [2]); ImageFilledRectangle ($ this-> newResource, 0, 0, $ width, $ height, $ bg ); $ tox = ceil ($ width-$ endWidth)/2); $ toy = ceil ($ height-$ endHeight)/2); if ($ tox <0) $ tox = 0; if ($ toy <0) $ toy = 0;} else if ($ flag = 4) {$ this-> newResource = imagecreatetruecolor ($ endwid2, $ endHeight2);} else {$ this-> newResource = imagecreatetruecolor ($ endWidth, $ endHeight);} $ this-> newResType = $ this-> imageType; imagecopyresampled ($ this-> newResource, $ this-> imageResource, $ tox, $ toy, 0, 0, $ endWidth, $ endHeight, $ this-> imageWidth, $ this-> imageHeight);}/*** Add a watermark to the image * @ param string $ waterContent the watermark content can be the image file name, it can also be a text * @ param int $ pos position 0-9 or an array * @ param int $ textFont font. It is valid when the watermark content is text * @ param string $ textColor, valid when the waterMark content is text * @ return string */public function waterMark ($ waterContent, $ pos = 0, $ textFont = 5, $ textColor = "# ffffff ") {$ isWaterImage = file_exists ($ waterContent); if ($ isWaterImage) {$ waterImgRes = $ this-> createImageFromFile ($ waterContent ); $ waterImgInfo = $ this-> getImageInfo ($ waterContent); $ waterWidth = $ waterImgInfo [0]; $ waterHeight = $ waterImgInfo [1];} else {$ waterText = $ waterContent; // $ temp = @ imagettfbbox (ceil ($ textFont * 2.5), 0 ,". /cour. ttf ", $ waterContent); if ($ temp) {$ waterWidth = $ temp [2]-$ temp [6]; $ waterHeight = $ temp [3]-$ temp [7];} else {$ waterWidth = 100; $ waterHeight = 12 ;}} if ($ this-> imageResource = NULL) {$ this-> createSrcImage ();} switch ($ pos) {case 0: // random $ posX = rand (0, ($ this-> imageWidth-$ waterWidth); $ posY = rand (0, ($ this-> imageHeight-$ waterHeight); break; case 1: // 1 indicates the top center. $ posX = 0; $ posY = 0; break; case 2: // 2 indicates the top center. $ posX = ($ this-> imageWidth-$ waterWidth) /2; $ posY = 0; break; case 3: // 3: Top right $ posX = $ this-> imageWidth-$ waterWidth; $ posY = 0; break; case 4: // 4 is left in the middle $ posX = 0; $ posY = ($ this-> imageHeight-$ waterHeight)/2; break; case 5: // 5 is centered in the center $ posX = ($ this-> imageWidth-$ waterWidth)/2; $ posY = ($ this-> imageHeight-$ waterHeight)/2; break; case 6: // 6 is the center of the right $ posX = $ this-> imageWidth-$ waterWidth; $ posY = ($ this-> imageHeight-$ waterHeight)/2; break; case 7: // 7 is left at the bottom $ posX = 0; $ posY = $ this-> imageHeight-$ waterHeight; break; case 8: // 8 is centered at the bottom $ posX = ($ this-> imageWidth-$ waterWidth)/2; $ posY = $ this-> imageHeight-$ waterHeight; break; case 9: // 9 is the bottom right $ posX = $ this-> imageWidth-$ waterWidth-20; $ posY = $ this-> imageHeight-$ waterHeight-10; break; default: // random $ posX = rand (0, ($ this-> imageWidth-$ waterWidth); $ posY = rand (0, ($ this-> imageHeight-$ waterHeight); break;} imagealphablending ($ this-> imageResource, true); if ($ isWaterImage) {imagecopy ($ this-> imageResource, $ waterImgRes, $ posX, $ posY, 0, 0, $ waterWidth, $ waterHeight);} else {$ R = hexdec (substr ($ textColor, 1, 2 )); $ G = hexdec (substr ($ textColor, 3, 2); $ B = hexdec (substr ($ textColor, 5); $ textColor = imagecolorallocate ($ this-> imageResource, $ R, $ G, $ B); imagestring ($ this-> imageResource, $ textFont, $ posX, $ posY, $ waterText, $ textColor );} $ this-> newResource = $ this-> imageResource; $ this-> newResType = $ this-> imageType ;} /*** generate Verification Code image ** @ param int $ width * @ param string $ height * @ param int $ length * @ param int $ validType 0 = number, 1 = letter, 2 = number plus letter * @ param string $ textColor text color * @ param string $ backgroundColor background color * @ return void */public function imageValidate ($ width, $ height, $ length = 4, $ validType = 1, $ textColor = '# 000000', $ backgroundColor =' # ffff') {if ($ validType = 1) {$ validString = 'hangzhou'; $ validLength = 52;} elseif ($ validType = 2) {$ validString = 'hangzhou'; $ validLength = 62 ;} else {$ validString = '000000'; $ validLength = 9 ;}srand (int) time (); $ valid = ''; for ($ I = 0; $ I <$ length; $ I ++) {$ valid. = $ validString {rand (0, $ validLength-1) };}$ this-> newResource = imagecreate ($ width, $ height); $ bgR = hexdec (substr ($ backgroundColor, 1, 2); $ bgG = hexdec (substr ($ backgroundColor, 3, 2); $ bgB = hexdec (substr ($ backgroundColor, 5, 2 )); $ backgroundColor = imagecolorallocate ($ this-> newResource, $ bgR, $ bgG, $ bgB); $ tR = hexdec (substr ($ textColor, 1, 2 )); $ tG = hexdec (substr ($ textColor, 3, 2); $ tB = hexdec (substr ($ textColor, 5, 2); $ textColor = imagecolorallocate ($ this-> newResource, $ tR, $ tG, $ tB); for ($ I = 0; $ I NewResource, 5, $ I * $ width/$ length + 3, 2, $ valid [$ I], $ textColor) ;}$ this-> newResType = IMAGETYPE_JPEG; return $ valid ;} /*** display output image * @ return void */public function display ($ fileName = '', $ quality = 100) {$ imgType = $ this-> newResType; $ imageSrc = $ this-> newResource; switch ($ imgType) {case IMAGETYPE_GIF: if ($ fileName = '') {header ('content-type: image/gif ');} imagegif ($ imageSrc, $ fileName, $ quality ); Break; case IMAGETYPE_JPEG: if ($ fileName = '') {header ('content-type: image/jpeg ');} imagejpeg ($ imageSrc, $ fileName, $ quality); break; case IMAGETYPE_PNG: if ($ fileName = '') {header ('content-type: image/png '); imagepng ($ imageSrc );} else {imagepng ($ imageSrc, $ fileName);} break; case IMAGETYPE_WBMP: if ($ fileName = '') {header ('content-type: image/wbmp ');} imagewbmp ($ imageSrc, $ fileName, $ qu Ality); break; case IMAGETYPE_XBM: if ($ fileName = '') {header ('content-type: image/xbm ');} imagexbm ($ imageSrc, $ fileName, $ quality); break; default: throw new Exception ('unsupport image type');} imagedestroy ($ imageSrc );} /*** Save image ** @ param int $ fileNameType file name Type 0 use the original file name, 1 use the specified file name, 2 Add a suffix to the original file name, 3. Generate a random file name * @ param string $ the path of the folder is empty. The path is the same as that of the original file * @ param string $ param parameter $ fileNameType * @ Return void */public function save ($ fileNameType = 0, $ folder = NULL, $ param = '_ miniature') {if ($ folder = NULL) {$ folder = dirname ($ this-> fileName ). DIRECTORY_SEPARATOR;} $ fileExtName = FileSystem: fileExt ($ this-> fileName, true); $ fileBesicName = FileSystem: getBasicName ($ this-> fileName, false ); switch ($ fileNameType) {case 1: $ newFileName = $ folder. $ param; break; case 2: $ newFileName = $ fol Der. $ fileBesicName. $ param. $ fileExtName; break; case 3: $ tmp = date ('ymdhis '); $ fileBesicName = $ tmp; $ I = 0; while (file_exists ($ folder. $ fileBesicName. $ fileExtName) {$ fileBesicName = $ tmp. $ I; $ I ++;} $ newFileName = $ folder. $ fileBesicName. $ fileExtName; break; default: $ newFileName = $ this-> fileName; break;} $ this-> display ($ newFileName); return $ newFileName; }}?>

I hope this article will help you with php programming.

,

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.