Common php image processing class,
<? Php/* known issues: 1. In the image scaling function, use the imagecreatetruecolor function to create a canvas and use transparent processing algorithms. However, images in PNG format cannot be transparent. Using the imagecreate function to create a canvas can solve this problem, but the scaled image color is too small. *** type value: * (1): indicates that the image scaling function is used, $ value1 represents the width of the scaled image, and $ value2 represents the height of the scaled image * (2): represents the image cropping function. $ value1 represents the coordinates of the starting point of the cropping, for example, starting from the origin, that is, "0, 0", followed by the X axis and Y axis, separated by commas (,). $ value2 indicates the width and height of the crop, * (3) is also used in the form of "20, 20": indicates that the image watermark function is used. $ value1 indicates the watermark image file name, and $ value2 indicates the watermark position in the image, you can select 10 values. 1 indicates top left, 2 indicates middle left, 3 indicates left, 4 indicates middle left, 5 indicates middle, 6 indicates middle right, and 7 indicates lower, 8 stands for bottom center, 9 stands for bottom right, 0 stands for random position **/class image {private $ types; // function number used, 1 stands for image scaling function 2 stands for image Cropping Function 3: add an image watermark to an image. The image watermark function is private $ imgtype; // The image format is private $ image; // The image resource is private $ width; // The image width is private $ height; // The image height is private $ value1; // $ value1 indicates the private $ value2 value based on the type value. // The value varies depending on the type value, $ value2 represents different values, private $ endaddress; // output address + file name function _ construct ($ imageaddress, $ types, $ value1 = "", $ value2 = "", $ endaddress) {$ this-> types = $ types; $ this-> image = $ this-> imagesources ($ imageaddress); $ this-> wid Th = $ this-> imagesizex (); $ this-> height = $ this-> imagesizey (); $ this-> value1 = $ value1; $ this-> value2 = $ value2; $ this-> endaddress = $ endaddress;} function outimage () {// depending on the input type value, output different functions: switch ($ this-> types) {case 1: $ this-> scaling (); break; case 2: $ this-> clipping (); break; case 3: $ this-> imagewater (); break; default: return false;} private function imagewater () {// http://www.hzhuti.com with image watermark function // Function to get the length and width of the watermark File $ imagearrs = $ this-> getimagearr ($ this-> value1 ); // call the function to calculate the position where the watermark is loaded. $ positionarr = $ this-> position ($ this-> value2, $ imagearrs [0], $ imagearrs [1]); // Add the watermark imagecopy ($ this-> image, $ this-> imagesources ($ this-> value1), $ positionarr [0], $ positionarr [1], 0, 0, $ imagearrs [0], $ imagearrs [1]); // call the output method to save $ this-> output ($ this-> image);} private function clipping () {// image cropping function // assign the passed values to the variable list ($ src_x, $ src _ Y) = explode (",", $ this-> value1); list ($ dst_w, $ dst_h) = explode (",", $ this-> value2 ); if ($ this-> width <$ src_x + $ dst_w | $ this-> height <$ src_y + $ dst_h) {// This judgment means that the canvas resource $ newimg = imagecreatetruecolor ($ dst_w, $ dst_h) cannot be captured outside the image to return false;} // create a new canvas resource $ newimg ); // crop imagecopyresampled ($ newimg, $ this-> image, 0, 0, $ src_x, $ src_y, $ dst_w, $ dst_h, $ dst_w, $ dst_h ); // call the output method to save $ this-> output ($ newimg);} private functio N scaling () {// image scaling function // get the width and height of proportional scaling $ this-> proimagesize (); // scale according to parameters, and call the output function to save the processed file $ this-> output ($ this-> imagescaling ();} private function imagesources ($ imgad) {// obtain the image type and open the image resource $ imagearray = $ this-> getimagearr ($ imgad); switch ($ imagearray [2]) {case 1: // gif $ this-> imgtype = 1; $ img = imagecreatefromgif ($ imgad); break; case 2: // jpeg $ this-> imgtype = 2; $ img = imagecreatefromjpeg ($ imgad); break; case 3: // pn G $ this-> imgtype = 3; $ img = imagecreatefrompng ($ imgad); break; default: return false;} return $ img;} private function imagesizex () {// retrieve image width return imagesx ($ this-> image);} private function imagesizey () {// retrieve image Height return imagesy ($ this-> image );} private function proimagesize () {// calculate the width and height of the scaled image. if ($ this-> value1 & ($ this-> width <$ this-> height )) {// proportional Scaling Algorithm $ this-> value1 = round ($ this-> value2/$ this-> h Eight) * $ this-> width);} else {$ this-> value2 = round ($ this-> value1/$ this-> width) * $ this-> height) ;}} private function imagescaling () {// image scaling function, returns the processed image resource $ newimg = imagecreatetruecolor ($ this-> value1, $ this-> value2); $ tran = imagecolortransparent ($ this-> image ); // process the transparent algorithm if ($ tran >=0 & $ tran <imagecolorstotal ($ this-> image) {$ tranarr = imagecolorsforindex ($ this-> image, $ tran); $ newcolor = imagecolorallocat E ($ newimg, $ tranarr ['red'], $ tranarr ['green'], $ tranarr ['blue']); imagefill ($ newimg, 0, 0, $ newcolor); imagecolortransparent ($ newimg, $ newcolor);} imagecopyresampled ($ newimg, $ this-> image, 0, 0, 0, 0, 0, $ this-> value1, $ this-> value2, $ this-> width, $ this-> height); return $ newimg;} private function output ($ image) {// output image switch ($ this-> imgtype) {case 1: imagegif ($ image, $ this-> endaddress); break; case 2: imagejpeg ($ image, $ this-> endaddress); break; case 3: imagepng ($ image, $ this-> endaddress); break; default: return false ;}} private function getimagearr ($ imagesou) {// return the image attribute array method return getimagesize ($ imagesou);} private function position ($ num, $ width, $ height) {// return the coordinates of a position based on the input number. $ width and $ height indicate the width and height of the inserted image. switch ($ num) {case 1: $ positionarr [0] = 0; $ positionarr [1] = 0; break; case 2: $ positionarr [0] = ($ This-> width-$ width)/2; $ positionarr [1] = 0; break; case 3: $ positionarr [0] = $ this-> width-$ width; $ positionarr [1] = 0; break; case 4: $ positionarr [0] = 0; $ positionarr [1] = ($ this-> height-$ height)/2; break; case 5: $ positionarr [0] = ($ this-> width-$ width) /2; $ positionarr [1] = ($ this-> height-$ height)/2; break; case 6: $ positionarr [0] = $ this-> width-$ width; $ positionarr [1] = ($ this-> height-$ height)/2; break; case 7: $ Positionarr [0] = 0; $ positionarr [1] = $ this-> height-$ height; break; case 8: $ positionarr [0] = ($ this-> width-$ width)/2; $ positionarr [1] = $ this-> height-$ height; break; case 9: $ positionarr [0] = $ this-> width-$ width; $ positionarr [1] = $ this-> height-$ height; break; case 0: $ positionarr [0] = rand (0, $ this-> width-$ width); $ positionarr [1] = rand (0, $ this-> height-$ height ); break;} return $ positionarr;} function _ des Truct () {imagedestroy ($ this-> image) ;}}?>