PHP implements image proportional scaling and Logo watermark function examples,

Source: Internet
Author: User
Tags function examples imagejpeg

PHP implements image proportional scaling and Logo watermark function examples,

This article describes how PHP achieves image proportional scaling and Logo watermarking. We will share this with you for your reference. The details are as follows:

/*** Proportional scaling function (saved) * @ 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 imageUpdateSize ($ picname, $ maxx = 100, $ maxy = 100, $ pre = "s _") {$ info = getimageSize ($ picname ); // get the basic information of the image $ w = $ info [0]; // get the width $ h = $ info [1]; // obtain the height // obtain the image type and create the corresponding image resource switch ($ info [2]) {case 1: // g If $ im = imagecreatefromgif ($ picname); break; case 2: // jpg $ im = imagecreatefromjpeg ($ picname); break; case 3: // png $ im = imagecreatefrompng ($ picname); break; default: die ("incorrect image type! ");} // 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) $ nim = imagecreatetruecolor ($ nw, $ nh ); // execute proportional scaling imagecopyresampled ($ nim, $ im, $ nw, $ nh, $ w, $ h ); // output image (the output is of the corresponding type based on the source image type) $ picinfo = pathinfo ($ picname ); // parse the name and path of the source image $ newpicname = $ picinfo ["dirname"]. "/". $ pre. $ picinfo ["basename"]; switch ($ info [2]) {case 1: imagegif ($ nim, $ newpicname); break; case 2: imagejpeg ($ nim, $ newpicname); break; case 3: imagepng ($ nim, $ newpicname); break;} // release the image resource imagedestroy ($ im); imagedestroy ($ nim ); // return result return $ newpicname;} // test: // echo imageUpdateSize (". /images/bg.jpg ", 200,200," ss _");//. /images/s_bg.jpg
/*** 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 imageUpdateLogo ($ picname, $ logo, $ pre = "n _") {$ picnameinfo = getimageSize ($ picname); // obtain the basic information of the image source $ logoinfo = getimageSize ($ logo ); // obtain the basic information of the logo image // var_dump ($ logoinfo); // create the corresponding image source switch ($ picnameinfo [2]) based on the image type {case 1: // Gif $ im = imagecreatefromgif ($ picname); break; case 2: // jpg $ im = imagecreatefromjpeg ($ picname); break; case 3: // png $ im = imagecreatefrompng ($ picname); break; default: die ("incorrect image type! ") ;}// Create the corresponding image source switch ($ logoinfo [2]) {case 1: // gif $ logoim = imagecreatefromgif ($ logo) based on the logo image type ); break; case 2: // jpg $ logoim = imagecreatefromjpeg ($ logo); break; case 3: // png $ logoim = imagecreatefrompng ($ logo); break; default: die ("Incorrect logo image type! ");} // Execute image watermark processing imagecopyresampled ($ im, $ logoim, $ picnameinfo [0]-$ logoinfo [0], $ picnameinfo [1]-$ logoinfo [1], 0, 0, $ logoinfo [0], $ logoinfo [1], $ logoinfo [0], $ logoinfo [1]); // output image (the output is of the corresponding type based on the source image type) $ picinfo = pathinfo ($ picname ); // parse the name and path of the source image $ newpicname = $ picinfo ["dirname"]. "/". $ pre. $ picinfo ["basename"]; switch ($ picnameinfo [2]) {case 1: imagegif ($ im, $ newpicname); break; case 2: imagejpeg ($ im, $ newpicname); break; case 3: imagepng ($ im, $ newpicname); break;} // release the image resource imagedestroy ($ im); imagedestroy ($ logoim ); // return result return $ newpicname;} // test echo imageUpdateLogo (". /images/bg2.jpg ",". /images/s_1.png ");

PS: Here are some useful image processing tools for your reference:

Online image conversion BASE64 tool:
Http://tools.jb51.net/transcoding/img2base64

ICO icon generation tool:
Http://tools.jb51.net/aideddesign/ico_img

Online Email icon creation tools:
Http://tools.jb51.net/email/emaillogo

Online image format conversion (jpg/bmp/gif/png) tool:
Http://tools.jb51.net/aideddesign/picext

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.