PHP uses basic GD and Image functions to implement many Image processing functions. With more open-source class libraries, you cannot forget the basics. & Amp; nbsp; how PHP generates thumbnails and watermarks: & amp; lt ;? Phpheader ('content-T
PHP uses basic GD and Image functions to implement many Image processing functions. With more open-source class libraries, you cannot forget the basics.
How PHP generates thumbnails and watermarks:
A simple image processing class that enables generating thumbnails, cropping, and watermarking.
Param ($ img1)-> thumb ('. /image/test_0.jpg ', 200,200, 0); // center crop $ img-> param ($ img1)-> thumb ('. /image/test_1.jpg ', 200,200, 1); // top left crop $ img-> param ($ img1)-> thumb ('. /image/test_2.jpg ', 200,200, 2); // add a watermark $ img-> param ($ img1)-> water ($ img2, $ water, 9) in the lower right corner ); class image {// protected $ img; // protected $ types = array (1 => 'GIF', 2 => 'jpg ', 3 => 'PNG ', 6 => 'bmp'); // image public Function _ construct ($ img = ''){! $ Img & $ this-> param ($ img);} // Info public function param ($ img) {$ this-> img = $ img; return $ this ;} // imageInfo public function getImageInfo ($ img) {$ info = @ getimagesize ($ img); if (isset ($ this-> types [$ info [2]) {$ info ['ext '] = $ info ['type'] = $ this-> types [$ info [2];} else {$ info ['ext '] = $ info ['type'] = 'jpg ';} $ info ['type'] = 'jpg '& $ info ['type'] = 'jpeg '; $ info ['size'] = @ filesize ($ img ); Return $ info;} // thumb (New Image address, width, height, cropping, allowed amplification) public function thumb ($ filename, $ new_w = 160, $ new_h = 120, $ cut = 0, $ big = 0) {// Obtain the source image information $ info = $ this-> getImageInfo ($ this-> img); if (! Empty ($ info [0]) {$ old_w = $ info [0]; $ old_h = $ info [1]; $ type = $ info ['type']; $ ext = $ info ['text']; unset ($ info ); // if the source image is smaller than the thumbnail and cannot be enlarged, if ($ old_w <$ new_h & $ old_h <$ new_w &&! $ Big) {return false;} // crop an image if ($ cut = 0) {// equals ratio column $ scale = min ($ new_w/$ old_w, $ new_h/$ old_h); // calculate the zoom ratio $ width = (int) ($ old_w * $ scale); // The thumbnail size $ height = (int) ($ old_h * $ scale); $ start_w = $ start_h = 0; $ end_w = $ old_w; $ end_h = $ old_h;} elseif ($ cut = 1) {// center crop $ scale1 = round ($ new_w/$ new_h, 2); $ scale2 = round ($ old_w/$ old_h, 2 ); if ($ scale1> $ scale2) {$ end_h = round ($ old_w/$ scale1, 2 ); $ Start_h = ($ old_h-$ end_h)/2; $ start_w = 0; $ end_w = $ old_w;} else {$ end_w = round ($ old_h * $ scale1, 2); $ start_w = ($ old_w-$ end_w)/2; $ start_h = 0; $ end_h = $ old_h;} $ width = $ new_w; $ height = $ new_h;} elseif ($ cut = 2) {// left top crop $ scale1 = round ($ new_w/$ new_h, 2 ); $ scale2 = round ($ old_w/$ old_h, 2); if ($ scale1> $ scale2) {$ end_h = round ($ old_w/$ scale1, 2 ); $ end_w = $ old_w;} else {$ end_w = ro Und ($ old_h * $ scale1, 2); $ end_h = $ old_h;} $ start_w = 0; $ start_h = 0; $ width = $ new_w; $ height = $ new_h;} // load the source image $ createFun = 'imagecreatefrom '. $ type; $ oldimg = $ createFun ($ this-> img); // Create a thumbnail if ($ type! = 'GIF' & function_exists ('imagecreatetruecolor') {$ newimg = imagecreatetruecolor ($ width, $ height);} else {$ newimg = imagecreate ($ width, $ height);} // copy the image if (function_exists ("ImageCopyResampled") {ImageCopyResampled ($ newimg, $ oldimg, 0, 0, $ start_w, $ start_h, $ width, $ height, $ end_w, $ end_h);} else {ImageCopyResized ($ newimg, $ oldimg, 0, 0, $ start_w, $ start_h, $ width, $ height, $ end_w, $ end_h );} // Set the interlace scan for jpeg images $ type = 'jpeg '& imageinterlace ($ newimg, 1); // generate an image $ imageFun = 'image'. $ type ;! @ $ ImageFun ($ newimg, $ filename) & die ('failed to save! Check whether the directory exists and is writable? '); ImageDestroy ($ newimg); ImageDestroy ($ oldimg); return $ filename;} return false;} // water (save address, watermark image, Watermark Position, transparency) public function water ($ filename, $ water, $ pos = 0, $ pct = 80) {// load the watermark image $ info = $ this-> getImageInfo ($ water ); if (! Empty ($ info [0]) {$ water_w = $ info [0]; $ water_h = $ info [1]; $ type = $ info ['type']; $ fun = 'imagecreatefrom '. $ type; $ waterimg = $ fun ($ water);} else {return false ;} // load the background image $ info = $ this-> getImageInfo ($ this-> img); if (! Empty ($ info [0]) {$ old_w = $ info [0]; $ old_h = $ info [1]; $ type = $ info ['type']; $ fun = 'imagecreatefrom '. $ type; $ oldimg = $ fun ($ this-> img);} else {return false;} // cut the watermark $ water_w> $ old_w & $ water_w = $ old_w; $ water_h> $ old_h & $ water_h = $ old_h; // watermark position switch ($ pos) {case 0: // random $ posX = rand (0, ($ old_w-$ water_w); $ posY = rand (0, ($ old_h-$ water_h); break; case 1: // 1 indicates that the top position is left $ posX = 0; $ posY = 0; Break; case 2: // 2 is centered at the top $ posX = ($ old_w-$ water_w)/2; $ posY = 0; break; case 3: // 3: Top-right: $ posX = $ old_w-$ water_w; $ posY = 0; break; case 4: // 4: center-left: $ posX = 0; $ posY = ($ old_h-$ water_h)/2; break; case 5: // 5 Center $ posX = ($ old_w-$ water_w)/2; $ posY = ($ old_h-$ water_h)/2; break; case 6: // 6 is the center of the right $ posX = $ old_w-$ water_w; $ posY = ($ old_h-$ water_h)/2; break; case 7: // 7 is left at the bottom $ posX = 0; $ PosY = $ old_h-$ water_h; break; case 8: // 8 is centered at the bottom $ posX = ($ old_w-$ water_w)/2; $ posY = $ old_h-$ water_h; break; case 9: // 9 is the bottom right corner $ posX = $ old_w-$ water_w; $ posY = $ old_h-$ water_h; break; default: // random $ posX = rand (0, ($ old_w-$ water_w); $ posY = rand (0, ($ old_h-$ water_h )); break;} // set the image color mixing mode imagealphablending ($ oldimg, true); // add the watermark imagecopymerge ($ oldimg, $ waterimg, $ posX, $ posY, 0, 0, $ water_w, $ water_h, $ pct); $ fun = 'image'. $ type ;! @ $ Fun ($ oldimg, $ filename) & die ('failed to save! Check whether the directory exists and is writable? '); Imagedestroy ($ oldimg); imagedestroy ($ waterimg); return $ filename ;}}