Use php to generate thumbnails and add watermarks

Source: Internet
Author: User
This simple GD Library operation [php] & lt ;? Php/*** Yan 18 public welfare PHP lecture Forum: http://www.zixue.it micro blog: small... this simple GD Library operation [php] $ Dinfo ['height'] | $ winfo ['width']> $ dinfo ['width']) {return false;} // read the two images on the canvas! However, the image may be png or jpeg. what function is used to read the image? $ Dfunc = 'imagecreatefrom '. $ dinfo ['text']; $ wfunc = 'imagecreatefrom'. $ winfo ['text']; if (! Function_exists ($ dfunc) |! Function_exists ($ wfunc) {return false;} www.2cto.com // dynamically load the function to create the canvas $ dim = $ dfunc ($ dst ); // create the canvas to be operated $ wim = $ wfunc ($ water); // Create a watermark canvas // calculate and paste the coordinates based on the watermark position switch ($ pos) {case 0: // $ posx = 0; $ posy = 0; break; case 1: // $ posx = $ dinfo ['width']-$ winfo ['width']; $ posy = 0; break; case 3: // $ posx = 0 in the lower left corner; $ posy = $ dinfo ['height']-$ winfo ['height']; break; default: $ posx = $ dinfo ['wid Th ']-$ winfo ['width']; $ posy = $ dinfo ['height']-$ winfo ['height'];} // add the watermark imagecopymerge ($ dim, $ wim, $ posx, $ posy, 0, 0, $ winfo ['width'], $ winfo ['height'], $ alpha); // save if (! $ Save) {$ save = $ dst; unlink ($ dst); // delete the source image} $ createfunc = 'image '. $ dinfo ['ext ']; $ createfunc ($ dim, $ save); imagedestroy ($ dim); imagedestroy ($ wim); return true ;} /** proportional scaling of the thumbnail generated by thumb, left white on both sides **/public static function thumb ($ dst, $ save = NULL, $ width = 200, $ height = 200) {// first, judge whether the image to be processed does not exist. $ dinfo = self: imageInfo ($ dst); if ($ dinfo = false) {return false ;} // calculate the scaling ratio $ calc = min ($ width/$ di Nfo ['width'], $ height/$ dinfo ['height']); // create the canvas of the original graph $ dfunc = 'imagecreatefrom '. $ dinfo ['ext ']; $ dim = $ dfunc ($ dst); // Create a thumbnail canvas $ tim = imagecreatetruecolor ($ width, $ height ); // create a white fill thumbnail canvas $ white = imagecolorallocate ($ tim, 255,255,255); // fill the thumbnail canvas imagefill ($ tim, 0, 0, $ white ); // Copy and scale down $ dwidth = (int) $ dinfo ['width'] * $ calc; $ dheight = (int) $ dinfo ['height'] * $ calc; $ paddingx = (int) ($ width-$ Dwidth)/2; $ paddingy = (int) ($ height-$ dheight)/2; imagecopyresampled ($ tim, $ dim, $ paddingx, $ paddingy, 0, 0, $ dwidth, $ dheight, $ dinfo ['width'], $ dinfo ['height']); // save the image if (! $ Save) {$ save = $ dst; unlink ($ dst) ;}$ createfunc = 'image '. $ dinfo ['ext ']; $ createfunc ($ tim, $ save); imagedestroy ($ dim); imagedestroy ($ tim); return true ;}} // print_r (ImageTool: imageInfo ('. /home.jpg ');/* www.2cto.com echo ImageTool: water ('. /home.jpg ','. /smallfeng.png', 'home1.jpg ', 0 )? 'OK': 'fail '; echo ImageTool: water ('./home.jpg ','./smallfeng.png', 'home2.jpg ', 1 )? 'OK': 'fail '; echo ImageTool: water ('./home.jpg ','./smallfeng.png', 'home3.jpg ', 2 )? 'OK': 'fail '; echo ImageTool: water ('./home.jpg ','./smallfeng.png', 'home4.jpg ', 3 )? 'OK': 'fail ';*/

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.