PHP image proportional scaling and adding detailed Logo watermark code and comments

Source: Internet
Author: User
PHP image proportional scaling and adding detailed Logo watermark code and comments
 Baidu's ""/*** proportional scaling function (implemented in storage mode) * @ param string $ picName scaled processing image source * @ param int $ maxx maximum width of the scaled image * @ param int $ maxy maximum height of the scaled image * @ param string $ prefix of the image name after pre scaling * @ return String the name of the image to be returned (for example, a.jpg => s_a.jpg */function imageNewageUpdateSize ($ picName, $ maxx = 100, $ maxy = 100, $ pre = "s _") {$ imageNewageInfo = getimageNewageSize ($ picName ); // obtain the basic information of the image $ w = $ imageNewageInfo [0]; // Obtain the width $ h = $ imageNewageInfo [1]; // Obtain the height/ /Obtain the image type and create the corresponding image resource switch ($ imageNewageInfo [2]) {case 1: // gif $ imageNew = imageNewagecreatefromgif ($ picName); break; case 2: // jpg $ imageNew = imageNewagecreatefromjpeg ($ picName); break; case 3: // png $ imageNew = imageNewagecreatefrompng ($ picName); break; default: die ("image type error! ");} // Calculate the zoom ratio if ($ maxx/$ w)> ($ maxy/$ h) {$ B = $ maxy/$ h ;} else {$ B = $ maxx/$ w;} // calculates the scaled size. $ nw = floor ($ w * $ B ); $ nh = floor ($ h * $ B); // create a new image source (target Image) $ nimageNew = imageNewagecreatetruecolor ($ nw, $ nh ); // execute proportional scaling imageNewagecopyresampled ($ nimageNew, $ imageNew, $ nw, $ nh, $ w, $ h ); // output image (the output is of the corresponding type based on the source image type) $ picimageNewageInfo = pathimageNewageInfo ($ picName ); // Parse the name and path of the source image $ newpicName = $ picimageNewageInfo ["dirname"]. "/". $ Pre. $ picimageNewageInfo ["basename"]; switch ($ imageNewageInfo [2]) {case 1: imageNewagegif ($ nimageNew, $ newpicName); break; case 2: imageNewagejpeg ($ nimageNew, $ newpicName); break; case 3: imageNewagepng ($ nimageNew, $ newpicName); break;} // release the image resource imageNewagedestroy ($ imageNew); imageNewagedestroy ($ nimageNew ); // return result return $ newpicName;} // call echo imageNewageUpdateSize (". /imageNewages/fig ", 400,400," ss _"); // The image you want to add yourself/*** add a logo image watermark to an image (saved) * @ param string $ picName source of the image to be processed * @ param string $ logo Watermark Image * @ param string $ prefix name of the image name after pre-processing * @ return String returns the name of the image to be returned (with parameters, for example, a.jpg => n_a.jpg * /function imageNewageUpdateLogo ($ picName, $ logo, $ pre = "n _") {$ picNameimageNewageInfo = getimageNewageSize ($ picName); // get the basic information of the image source $ logoimageNewageInfo = getimageNewageSize ($ logo ); // obtain the basic information of the logo image // var_dump ($ logoimageNew AgeInfo); // create the corresponding image source switch based on the image type ($ picNameimageNewageInfo [2]) {case 1: // gif $ imageNew = imageNewagecreatefromgif ($ picName); break; case 2: // jpg $ imageNew = imageNewagecreatefromjpeg ($ picName); break; case 3: // png $ imageNew = imageNewagecreatefrompng ($ picName); break; default: die ("incorrect image type! ") ;}// Create the corresponding image source switch based on the logo image type ($ logoimageNewageInfo [2]) {case 1: // gif $ logoimageNew = imageNewagecreatefromgif ($ logo ); break; case 2: // jpg $ logoimageNew = imageNewagecreatefromjpeg ($ logo); break; case 3: // png $ logoimageNew = imageNewagecreatefrompng ($ logo); break; default: die ("incorrect logo image type! ") ;}// Perform image watermark processing the height of the source image-the width of the source image of the logo image-the width of the logo image imageNewagecopyresampled ($ imageNew, $ logoimageNew, $ picNameimageNewageInfo [0]-$ logoimageNewageInfo [0], $ picNameimageNewageInfo [1]-$ logoimageNewageInfo [1], 0,0, $ logoimageNewageInfo [0], $ region [1], $ logoimageNewageInfo [0], $ logoimageNewageInfo [1]); // output image (the output is of the corresponding type based on the source image type) $ picimageNewageInfo = pathimageNewageInfo ($ picName ); // Parse the name and path of the source image $ newpicName = $ picimageNewageInfo ["dirname"]. "/". $ pre. $ picimageNewageInfo ["basename"]; switch ($ picNameimageNewageInfo [2]) {case 1: imageNewagegif ($ imageNew, $ newpicName); break; case 2: imageNewagejpeg ($ imageNew, $ newpicName); break; case 3: imageNewagepng ($ imageNew, $ newpicName); break;} // release the image resource imageNewagedestroy ($ imageNew); imageNewagedestroy ($ logoimageNew ); // return result return $ newpicName;} // Test // echo imageNewageUpdateLogo (". /imageNewages/leyangjun.jpg ",". /imageNewages/logo.png "); // add your image and the image watermark with your logo OK

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.