PHP: code used to add a watermark to a downstream image

Source: Internet
Author: User
Tags imagecopy imagejpeg
PHP: the program code used to add a watermark to the uploaded image. The program used PHP to add a watermark to the uploaded image creates a graph by determining the file type, and then copies it to the original image, fill in and create a rectangle for writing the image string () or determine the watermark type in the original image program: one is a string, and the other is to add a graphic object on it. If you are familiar with the GD library of PHP, it is not difficult to understand this article! * ******* PHP: the program code that watermarks uploaded images
The program that uses PHP to add a watermark to the uploaded image is to create a graph by determining the file type, copy it to the original image, fill it with and create a rectangle for writing it to imagestring () or determine the watermark type in the original image program: one is a string, and the other is to add a graphic object on it. If you are familiar with the GD library of PHP, it is not difficult to understand this article!
/*************************************** * ************* Parameter description: $ max_file_size: the size limit of the uploaded file. unit: BYTE $ destination_folder: upload file path $ watermark: whether to append the watermark (1 indicates adding the watermark, and others indicates not adding the watermark). usage: 1. set PHP. remove the SN of the line "extension = php_gd2.dll" in the INI file because the GD library is used. 2. change extension_dir = to the directory where your php_gd2.dll is located; **************************************** * ********** // list of uploaded File Types $ uptypes = array ('image/jpg ', 'image/jpeg ', 'image/png', 'image/pjpeg ', 'image/g If ', 'image/bmp', 'image/x-png '); $ max_file_size = 2000000; // The size of the uploaded file is limited. the unit is BYTE $ destination_folder = "uploadimg /"; // upload file path $ watermark = 1; // whether to append the watermark (1 is a watermark, others are not a watermark); $ watertype = 1; // watermark type (1 is a text, 2 for the image) $ waterposition = 1; // watermark position (1 for the lower left corner, 2 for the lower right corner, 3 for the upper left corner, 4 for the upper right corner, 5 for the center ); $ waterstring = "http://www.hackhome.com/"; // watermark string $ waterimg = "xplore.gif"; // watermark image $ imgpreview = 1; // whether to generate a preview image (1 is generated, others are not generated); $ imgpreviewsize = 1/2; // Thumbnail ratio?>



ZwelL image upload program
 File name: ". $ destination_folder. $ fname ."
"; Echo" width: ". $ image_size [0]; echo" length: ". $ image_size [1]; echo"
Size :". $ file ["size"]. "bytes"; if ($ watermark = 1) {$ iinfo = getimagesize ($ destination, $ iinfo); $ nimage = imagecreatetruecolor ($ image_size [0], $ image_size [1]); $ white = imagecolorallocate ($ nimage, 255,255,255); $ black = imagecolorallocate ($ nimage, 0, 0); $ red = imagecolorallocate ($ nimage,, 0, 0); imagefill ($ nimage, $ white); switch ($ iinfo [2]) {case 1: $ simage = imagecreatefromgif ($ destination); break; case 2: $ simage = imagecreatefromjpeg ($ destination); break; case 3: $ simage = imagecreatefrompng ($ destination); break; case 6: $ simage = imagecreatefromwbmp ($ destination ); break; default: die ("unsupported file types"); exit;} imagecopy ($ nimage, $ simage, 0, 0, 0, $ image_size [0], $ image_size [1]); imagefilledrectangle ($ nimage, 1, $ image_size [1]-15, 80, $ image_size [1], $ white); switch ($ watertype) {case 1: // add the watermark string imagestring ($ nimage, 2, 3, $ image_size [1]-15, $ waterstring, $ black); break; case 2: // watermark image $ simage1 = imagecreatefromgif ("xplore.gif"); imagecopy ($ nimage, $ simage1,); imagedestroy ($ simage1); break ;} switch ($ iinfo [2]) {case 1: // imagegif ($ nimage, $ destination); imagejpeg ($ nimage, $ destination); break; case 2: imagejpeg ($ nimage, $ destination); break; case 3: imagepng ($ nimage, $ destination); break; case 6: imagewbmp ($ nimage, $ destination ); // imagejpeg ($ nimage, $ destination); break;} // overwrite the original uploaded file imagedestroy ($ nimage); imagedestroy ($ simage );} if ($ imgpreview = 1) {echo"
Image preview:
"; Echo" ";}}?>

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.