Powerful PHP image processing class (Water

Source: Internet
Author: User
Tags image flip imagecopy
This article mainly summarizes the php image processing class (Water

This article mainly summarizes the php image processing class (Water

Powerful php image processing class that allows you to customize image watermarks, transparency, image scaling, image sharpening, image rotation, image flip, image cropping, and image reversed colors.

* Image processing functions: scaling, cropping, photo frames, watermarks, sharpening, rotation, turning, transparency, and reversed colors
* Idea of processing and saving historical records: a new image is automatically generated when an image is changed. You can add steps on the basis of the original image, for example: picture name + _ Step 1

The Code is as follows:

<? Php class picture {var $ PICTURE_URL; // var $ DEST_URL = "temp%01.jpg"; // generate the Target Image Location var $ PICTURE_CREATE; // var $ TURE_COLOR of the image to be created; // create a true color image var $ PICTURE_WIDTH; // var $ PICTURE_HEIGHT of the original image width; // Original Image Height/*** watermark type. The default value is watermark text */var $ MARK_TYPE = 1; var $ WORD; // The text var $ WORD_X after the UTF-8; // The text abscissa var $ WORD_Y; // The text ordinate var $ FONT_TYPE; // The font type var $ FONT_SIZE = "12 "; // font size var $ FONT_WORD; // text var $ ANGLE = 0; // text ANGLE. The default value is 0 v. Ar $ FONT_COLOR = "#000000"; // text color var $ FONT_PATH = "font/simkai. ttf "; // font library, default: var $ FORCE_URL; // watermark image var $ FORCE_X = 0; // watermark abscissa var $ FORCE_Y = 0; // watermark y coordinate var $ FORCE_START_X = 0; // The Y coordinate var $ FORCE_START_Y = 0; // The Y coordinate var $ PICTURE_TYPE; // image type var $ PICTURE_MIME; // The output Header/*** if the scaling ratio is 1, the image is scaled by the ZOOM height and width */var $ ZOOM = 1; // zoom type var $ ZOOM_MULTIPLE; // zoom ratio var $ ZOOM_WIDTH; // zoom width var $ ZOOM_HEIGHT; // zoom height /*** Crop, according to the ratio and fixed length, width */var $ CUT_TYPE = 1; // crop type var $ CUT_X = 0; // crop the abscissa var $ CUT_Y = 0; // crop the ordinate var $ CUT_WIDTH = 100; // crop the width var $ CUT_HEIGHT = 100; // crop height/***** sharpen */var $ SHARP = "7.0"; // sharpen Degree/***** transparency Processing */var $ ALPHA = '20140901 '; // transparency between 0 and 90.0 var $ ALPHA_X = "90"; var $ ALPHA_Y = "50";/*** rotate at any angle */var $ CIRCUMROTATE = "; // Note: it must be a floating point/*** ERROR message */var $ ERROR = array ('unalviable' => '. No related image is found! ');/*** Constructor: function initialization */function _ construct ($ PICTURE_URL) {$ this-> get_info ($ PICTURE_URL);} function get_info ($ PICTURE_URL) {/*** process the original image information. First, check whether the image exists. if the image does not exist, the corresponding information is displayed. */@ $ SIZE = getimagesize ($ PICTURE_URL); if (! $ SIZE) {exit ($ this-> ERROR ['unalviable']);} // obtain the information type, width, and height of the original image. $ this-> PICTURE_MIME = $ SIZE ['mime ']; $ this-> PICTURE_WIDTH = $ SIZE [0]; $ this-> PICTURE_HEIGHT = $ SIZE [1]; // create an image switch ($ SIZE [2]) {case 1: $ this-> PICTURE_CREATE = imagecreatefromgif ($ PICTURE_URL ); $ this-> PICTURE_TYPE = "imagejpeg"; $ this-> PICTURE_EXT = "jpg"; break; case 2: $ this-> PICTURE_CREATE = imagecreatefromjpeg ($ PICTURE_URL); $ thi S-> PICTURE_TYPE = "imagegif"; $ this-> PICTURE_EXT = "gif"; break; case 3: $ this-> PICTURE_CREATE = imagecreatefrompng ($ PICTURE_URL ); $ this-> PICTURE_TYPE = "imagepng"; $ this-> PICTURE_EXT = "png"; break ;} /*** convert text color to hexadecimal x/preg_match_all ("/([0-f]) {2, 2}/I", $ this-> FONT_COLOR, $ MATCHES); if (count ($ MATCHES) = 3) {$ this-> RED = hexdec ($ MATCHES [0] [0]); $ this-> GREEN = hexdec ($ MATCHES [0] [1]); $ this -> BLUE = hexdec ($ MATCHES [0] [2]);} # end of _ construct/*** convert the hexadecimal color into a decimal (R, G, B) */function hex2dec () {preg_match_all ("/([0-f]) {2}/I", $ this-> FONT_COLOR, $ MATCHES); if (count ($ MATCHES) = 3) {$ this-> RED = hexdec ($ MATCHES [0] [0]); $ this-> GREEN = hexdec ($ MATCHES [0] [1]); $ this-> BLUE = hexdec ($ MATCHES [0] [2]) ;}// scaling type function zoom_type ($ ZOOM_TYPE) {$ this-> ZOOM = $ ZOOM_TYPE;} // scale the image. Zoom function ZOOM () {// specify the height and width if ($ this-> zoom = 0) {$ this-> ZOOM_WIDTH = $ this-> PICTURE_WIDTH * $ this-> ZOOM_MULTIPLE; $ this-> ZOOM_HEIGHT = $ this-> PICTURE_HEIGHT * $ this-> ZOOM_MULTIPLE ;} // create a true color image $ this-> TRUE_COLOR = imagecreatetruecolor ($ this-> ZOOM_WIDTH, $ this-> ZOOM_HEIGHT); $ WHITE = imagecolorallocate ($ this-> TRUE_COLOR, 255,255,255); imagefilledrectangle ($ this-> TRUE_C OLOR, 0, 0, $ this-> ZOOM_WIDTH, $ this-> ZOOM_HEIGHT, $ WHITE); imagecopyresized ($ this-> TRUE_COLOR, $ this-> PICTURE_CREATE, 0, 0, 0, 0, $ this-> ZOOM_WIDTH, $ this-> ZOOM_HEIGHT, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT);} # end of zoom // crop the image, by Coordinate or automatic function cut () {$ this-> TRUE_COLOR = imagecreatetruecolor ($ this-> CUT_WIDTH, $ this-> CUT_WIDTH); imagecopy ($ this-> TRUE_COLOR, $ this-> PI CTURE_CREATE, 0, 0, $ this-> CUT_X, $ this-> CUT_Y, $ this-> CUT_WIDTH, $ this-> CUT_HEIGHT );} # end of cut/*** put text or image * watermark text on the image */function _ mark_text () {$ this-> TRUE_COLOR = imagecreatetruecolor ($ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT); $ this-> WORD = mb_convert_encoding ($ this-> FONT_WORD, 'utf-8', 'gb2312 '); /*** get the text range using the TrueType font */$ TEMP = imagettfbbox ($ this-> FONT_SIZE, 0, $ thi S-> FONT_PATH, $ this-> WORD); $ WORD_LENGTH = strlen ($ this-> WORD); $ WORD_WIDTH = $ TEMP [2]-$ TEMP [6]; $ WORD_HEIGHT = $ TEMP [3]-$ TEMP [7];/*** the default position of the text watermark is in the lower right corner */if ($ this-> WORD_X = "") {$ this-> WORD_X = $ this-> PICTURE_WIDTH-$ WORD_WIDTH;} if ($ this-> WORD_Y = "") {$ this-> WORD_Y = $ this-> PICTURE_HEIGHT-$ WORD_HEIGHT;} imagesettile ($ this-> TRUE_COLOR, $ this-> PICTURE_CREATE); imagefilledrectang Le ($ this-> TRUE_COLOR, 0, 0, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT, IMG_COLOR_TILED); $ TEXT2 = imagecolorallocate ($ this-> TRUE_COLOR, $ this-> RED, $ this-> GREEN, $ this-> Blue); imagettftext ($ this-> TRUE_COLOR, $ this-> FONT_SIZE, $ this-> ANGLE, $ this-> WORD_X, $ this-> WORD_Y, $ TEXT2, $ this-> FONT_PATH, $ this-> WORD );} /*** watermark image */function _ mark_picture () {/*** get watermark image information */@ $ SIZE = Getimagesize ($ this-> FORCE_URL); if (! $ SIZE) {exit ($ this-> ERROR ['unalviable']);} $ FORCE_PICTURE_WIDTH = $ SIZE [0]; $ FORCE_PICTURE_HEIGHT = $ SIZE [1]; // create a watermark image switch ($ SIZE [2]) {case 1: $ FORCE_PICTURE_CREATE = imagecreatefromgif ($ this-> FORCE_URL); $ FORCE_PICTURE_TYPE = "gif"; break; case 2: $ FORCE_PICTURE_CREATE = imagecreatefromjpeg ($ this-> FORCE_URL); $ response = "jpg"; break; case 3: $ FORCE_PICTURE_CREATE = imagecreatefrompng ($ this-> FORCE_URL); $ FORCE_PICTURE_TYPE = "png"; break;}/*** determine the watermark image size and generate the target image size. If the watermark is larger than the image size, the size of the generated image is the size of the watermark image. Otherwise, the size of the generated image is the size of the original image. */$ This-> NEW_PICTURE = $ this-> PICTURE_CREATE; if ($ FORCE_PICTURE_WIDTH> $ this-> PICTURE_WIDTH) {$ CREATE_WIDTH = $ FORCE_PICTURE_WIDTH-$ this-> FORCE_START_X ;} else {$ CREATE_WIDTH = $ this-> PICTURE_WIDTH;} if ($ FORCE_PICTURE_HEIGHT> $ this-> PICTURE_HEIGHT) {$ CREATE_HEIGHT = $ FORCE_PICTURE_HEIGHT-$ this-> FORCE_START_Y ;} else {$ CREATE_HEIGHT = $ this-> PICTURE_HEIGHT;}/*** create a canvas */$ NEW_PICTURE _ CREATE = imagecreatetruecolor ($ CREATE_WIDTH, $ CREATE_HEIGHT); $ WHITE = imagecolorallocate ($ NEW_PICTURE_CREATE, 255,255,255);/*** copy the background image to the canvas */imagecopy ($ NEW_PICTURE_CREATE, CREATE, $ this-> PICTURE_CREATE, 0, 0, 0, 0, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT ); /*** copy the target image to the background image */imagecopy ($ NEW_PICTURE_CREATE, $ FORCE_PICTURE_CREATE, $ this-> FORCE_X, $ this-> FORCE_Y, $ this-> FORCE_START_X, $ This-> FORCE_START_Y, $ FORCE_PICTURE_WIDTH, $ FORCE_PICTURE_HEIGHT); $ this-> TRUE_COLOR = $ NEW_PICTURE_CREATE;} # end of mark function alpha _() {$ this-> TRUE_COLOR = imagecreatetruecolor ($ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT); $ rgb = "# CDCDCD"; $ tran_color = "#000000 "; for ($ j = 0; $ j <= $ this-> PICTURE_HEIGHT-1; $ j ++) {for ($ I = 0; $ I <= $ this-> PICTURE_WIDTH-1; $ I ++) {$ rgb = imagecolo Rat ($ this-> PICTURE_CREATE, $ I, $ j); $ r = ($ rgb> 16) & 0xFF; $ g = ($ rgb> 8) & 0xFF; $ B = $ rgb & 0xFF; $ now_color = imagecolorallocate ($ this-> PICTURE_CREATE, $ r, $ g, $ B ); if ($ now_color = $ tran_color) {continue;} else {$ color = imagecolorallocatealpha ($ this-> PICTURE_CREATE, $ r, $ g, $ B, $ ALPHA ); imagesetpixel ($ this-> PICTURE_CREATE, $ ALPHA_X + $ I, $ ALPHA_Y + $ j, $ color);} $ this-> TRUE_COLOR = $ thi S-> PICTURE_CREATE ;}}/ *** image rotation: * rotate along the Y axis */function turn_y () {$ this-> TRUE_COLOR = imagecreatetruecolor ($ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT); for ($ x = 0; $ x <$ this-> PICTURE_WIDTH; $ x ++) {imagecopy ($ this-> TRUE_COLOR, $ this-> PICTURE_CREATE, $ this-> PICTURE_WIDTH-$ x-1, 0, $ x, 0, 1, $ this-> PICTURE_HEIGHT );}} /*** rotate along the X axis */function turn_x () {$ this-> TRUE_COLOR = imagec Reatetruecolor ($ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT); for ($ y = 0; $ y <$ this-> PICTURE_HEIGHT; $ y ++) {imagecopy ($ this-> TRUE_COLOR, $ this-> PICTURE_CREATE, 0, $ this-> PICTURE_HEIGHT-$ y-1, 0, $ y, $ this-> PICTURE_WIDTH, 1) ;}}/*** rotate at any angle */function turn () {$ this-> TRUE_COLOR = imagecreatetruecolor ($ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT ); imageCopyResized ($ this-> TRUE_CO LOR, $ this-> PICTURE_CREATE, 0, 0, 0, 0, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT); $ WHITE = imagecolorallocate ($ this-> TRUE_COLOR, 255,255,255); $ this-> TRUE_COLOR = imagerotate ($ this-> TRUE_COLOR, $ this-> CIRCUMROTATE, $ WHITE);}/*** image sharpening */function sharp () {$ this-> TRUE_COLOR = imagecreatetruecolor ($ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT); $ cnt = 0; for ($ x = 0; $ x <$ this-> PICTURE_WIDTH; $ x ++) {for ($ y = 0; $ y <$ this-> PICTURE_HEIGHT; $ y ++) {$ src_clr1 = imagecolorsforindex ($ this-> TRUE_COLOR, imagecolorat ($ this-> PICTURE_CREATE, $ X-1, $ Y-1); $ src_clr2 = imagecolorsforindex ($ this-> TRUE_COLOR, imagecolorat ($ this-> PICTURE_CREATE, $ x, $ y )); $ r = intval ($ src_clr2 ["red"] + $ this-> SHARP * ($ src_clr2 ["red"]-$ src _ Clr1 ["red"]); $ g = intval ($ src_clr2 ["green"] + $ this-> SHARP * ($ src_clr2 ["green"]-$ src_clr1 ["green"]); $ B = intval ($ src_clr2 ["blue"] + $ this-> SHARP * ($ src_clr2 ["blue"]-$ src_clr1 ["blue"]); $ r = min (255, max ($ r, 0); $ g = min (255, max ($ g, 0); $ B = min (255, max ($ B, 0); if ($ DST_CLR = imagecolorexact ($ this-> PICTURE_CREATE, $ r, $ g, $ B) =-1) $ DST_CLR = imagecolorallocate ($ this-> PICTURE_CR EATE, $ r, $ g, $ B); $ cnt ++; if ($ DST_CLR =-1) die ("color allocate faile at $ x, $ y ($ cnt ). "); imagesetpixel ($ this-> TRUE_COLOR, $ x, $ y, $ DST_CLR) ;}}/ *** process the image in Reversed colors ?? */Function return_color () {/*** create a canvas */$ NEW_PICTURE_CREATE = imagecreate ($ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT ); $ WHITE = imagecolorallocate ($ NEW_PICTURE_CREATE, 255,255,255);/*** copy the background image to the canvas */imagecopy ($ NEW_PICTURE_CREATE, $ this-> PICTURE_CREATE, 0, 0, 0, 0, 0, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT); $ this-> TRUE_COLOR = $ NEW_PICTURE_CREATE;}/*** generate the target image and display */functi On show () {// judge the browser. if IE is used, the header if (isset ($ _ SERVER ['HTTP _ USER_AGENT ']) is not sent. {$ ua = strtoupper ($ _ SERVER ['HTTP _ USER_AGENT ']); if (! Preg_match ('/^. * MSIE. * \) $/I ', $ ua) {header ("Content-type: $ this-> PICTURE_MIME") ;}$ OUT = $ this-> PICTURE_TYPE; $ OUT ($ this-> TRUE_COLOR);}/*** generate the target image and save */function save_picture () {// output the image to a browser or file in JPEG format $ OUT = $ this-> PICTURE_TYPE; if (function_exists ($ OUT) {// judge the browser, if IE is used, it will not send the header if (isset ($ _ SERVER ['HTTP _ USER_AGENT ']) {$ ua = strtoupper ($ _ SERVER ['HTTP _ USER_AGENT']); if (! Preg_match ('/^. * MSIE. * \) $/I ', $ ua) {header ("Content-type: $ this-> PICTURE_MIME") ;}} if (! $ This-> TRUE_COLOR) {exit ($ this-> ERROR ['unavilable']);} else {$ OUT ($ this-> TRUE_COLOR, $ this-> DEST_URL ); $ OUT ($ this-> TRUE_COLOR) ;}}/ *** destructor: Release image */function _ destruct () {/*** release image */imagedestroy ($ this-> TRUE_COLOR); imagedestroy ($ this-> PICTURE_CREATE);} # end of class}?>

This is a very powerful php image processing class. I believe it will come in handy in the future.

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.