Powerful PHP image processing class (Watermark, transparency, rotation ),. Powerful PHP image processing class (Watermark, transparency, rotation), very powerful php image processing class, allows you to customize image watermarks, transparency, image scaling, image sharpening, and image rotation. PHP image processing (watermarks, transparency, and rotation) is powerful ),
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 var $ 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; // scaling type var $ ZOOM_MULTIPLE; // scaling ratio var $ ZOOM_WIDTH ;// Zoom width var $ ZOOM_HEIGHT; // zoom height/*** crop, proportionally and fixed length, width */var $ CUT_TYPE = 1; // cropping type var $ CUT_X = 0; // The x-axis var $ CUT_Y = 0; // The y-axis var $ CUT_WIDTH = 100; // 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 number/*** error message */var $ ERRO R = array ('unalviable' => 'no related image 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 ($ PI CTURE_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 _ 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. if the height and width are not specified, scale the image by function zoom () {// ZOOM size 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_COLOR, 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 an image by coordinate or automatic function cut () {$ this-> TRUE_COLOR = imagecreatetruecolor ($ this-> CUT_WIDTH, $ thi S-> 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 image * watermark text */function _ mark_text () on the image () {$ this-> TRUE_COLOR = imagecreatetruecolor ($ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT); $ this-> WORD = mb_convert_encoding ($ this-> FONT_WORD, 'utf-8', 'gb2312');/*** get the Tr Text range of ueType 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 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); 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_COLOR, $ this-> FONT_SIZE, $ this-> ANGLE, $ this-> WORD_X, $ this-> WORD_Y, $ TEXT2, $ this-> FONT_P ATH, $ 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); $ FORCE_PICTURE_TYPE = "jpg"; break; case 3: $ FORCE_PICTURE_CREATE = imagecreate Frompng ($ 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, the generated image size is the watermark image size. 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 = inline ($ 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, $ 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-> F ORCE_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 = imagecolorat ($ 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 = $ this-> 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 tur N_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-> 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_H EIGHT); imageCopyResized ($ this-> TRUE_COLOR, $ this-> PICTURE_CREATE, 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 = imagecrea Tetruecolor ($ 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_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, 0, $ this-> PICTURE_WIDTH, $ this-> PICTURE_HEIGHT); $ this-> TRUE_COLOR = $ NEW_PICTURE_CREATE;}/*** generate the target image and display */funct Ion 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.
Watermark (Watermark, transparency, rotation), very powerful php image processing class, can customize the image watermark, transparency, image scaling, image sharpening, image rotation...