This article mainly introduces the generic image processing class implemented by php, which can be used to scale, cut, frame, watermark, sharpen, rotate, flip, transparency, reversed, and other functions for images, it has some reference value. if you need it, you can refer to the examples in this article to describe the generic image processing class implemented by php. Share it with you for your reference. The details are as follows:
This image processing function provides the following functions: scaling, cropping, photo frames, watermarks, sharpening, rotation, turning, transparency, and reversed colors. you can use this function to process and save historical records: when an image is changed, a new image is automatically generated. you can add steps based on the original image to name the image. for example, add the following steps to the image name +. You can refer to this class in some Web applications that require advanced image processing.
<? Phpclass picture {var $ PICTURE_URL; // var $ DEST_URL = "temp1_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; // The height of the original image/* Watermark type, the default is the watermark text */var $ MARK_TYPE = 1; var $ WORD; // The text var $ WORD_X after the UTF-8; // The text abscissa var $ WORD_Y; // text ordinate var $ FONT_TYPE; // font type var $ FONT_SIZE = "12"; // font size var $ FONT_WORD; // text var $ ANGLE = 0; // text angle. the default value is 0 var $ FONT_C. OLOR = "#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, proportional and fixed length, width */var $ CUT_TYPE = 1; // cropping type var $ CUT_X = 0; // The x-axis var $ CUT_Y = 0 of cropping; // The y-axis var $ CUT _ of cropping _; // crop width var $ CUT_HEIGHT = 100; // crop height/* sharpen */var $ SHARP = "7.0 "; // sharpen degree/* transparency processing */var $ ALPHA = '000000'; // var $ ALPHA_X = "90"; var $ ALPHA_Y = "50 "; /* rotate at any angle */var $ CIRCUMROTATE = "90.0"; // 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 information of the original image. First, check whether the image exists. Otherwise, the corresponding information is provided. */@ $ 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 _; $ 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 ); $ this-> 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 _ co Nstruct/* converts a hexadecimal color to a decimal (R, G, B) */function hex2dec () {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]) ;}// scaling type function zoom_type ($ ZOOM_TYPE) {$ this-> ZOOM = $ ZOOM_TYPE;} // scale the image. if the height and width are not specified, scale the image by function zoom () {// ZOOM size if ($ this-> ZOOM = 0) {$ th Is-> ZOOM _; gt; 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_COLOR, $ this-> ZOOM_WIDTH, $ this-> ZOOM_HEIGHT, $ WHITE); imagecopyresized ($ this-> TRUE_COLOR, $ this-> PICT URE_CREATE, 0, 0, 0, $ this-> ZOOM_WIDTH, $ this-> ZOOM_HEIGHT, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT );} // end of zoom // crop an image by coordinate or automatic function cut () {$ this-> TRUE_COLOR = imagecreatetruecolor ($ this-> CUT_WIDTH, $ this-> CUT_WIDTH ); imagecopy ($ this-> TRUE_COLOR, $ this-> PICTURE_CREATE, 0, 0, $ this-> CUT_X, $ this-> CUT_Y, $ this-> CUT_WIDTH, $ this-> CUT_HEIGHT);} // end of cut/* put text or 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');/* obtain the text range using the TrueType font */$ TEMP = imagettfbbox ($ this-> FONT_SIZE, 0, $ this-> 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 the bottom 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); imagefilledrectangle ($ 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_COLO R, $ this-> FONT_SIZE, $ this-> ANGLE, $ this-> WORD_X, $ this-> WORD_Y, $ TEXT2, $ this-> FONT_PATH, $ this-> WORD);}/* Watermark Image */function _ mark_picture () {/* obtain watermark image information */@ $ SIZE = getimagesize ($ this-> FORCE_URL); if (! $ SIZE) {exit ($ this-> ERROR ['unalviable']) ;}$ FORCE_PICTURE _; $ 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 = response ($ 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 _; gt; FORCE_START_X ;} else {$ CREATE _; gt; 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, $ this-> PICTURE_CREATE, 0, 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 markfunction 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 = imagecolorat ($ this-> PICTURE_CREATE, $ I, $ j); $ r = ($ rgb> 16) & 0xFF; $ g = ($ rgb> 8) & 0xFF; $ B = $ rgb & 0xFF; $ now_color = image Colorallocate ($ 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 = $ this-> PICTURE_CREATE ;}}/ * rotating images: rotating 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 = imagecreatetruecolor ($ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT); for ($ y = 0; $ y <$ this-> PICTURE_HEIGHT; $ y ++) {imagecopy ($ this-> TRUE_COLOR, $ this-> PICTURE_CREATE, 0, $ this-> PICTUR E_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_COLOR, $ this-> PICTURE_CREATE, 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 = imager Otate ($ 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 ($ thi S-> 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_CREATE, $ 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, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT); $ this-> TRUE_COLOR = $ NEW_PICTURE_CREATE;}/* Generate the target image and display */function show () {// judge the browser, if IE is used, if (isset ($ _ SERVER ['http _ USER_AGENT ']) {$ 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 not sent, 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 the image */function _ destruct () {/* Release Image */imagedestroy ($ this-> TRUE_COLOR); imagedestroy ($ this-> PICTURE_CREATE);} // end of class}?>
I hope this article will help you with php programming.