Php: Adding image watermarks to uploaded images _ PHP Tutorial

Source: Internet
Author: User
Tags transparent color
Php adds an image watermark to the uploaded image. I believe everyone knows how to use PHP to add a text watermark to an image. But what if the watermark effect is an image? What should I do? Don't worry. we will break it down later. I also recently used this function. I believe everyone knows how to use PHP to add text watermarks to images. But what if the watermark effect is image? What should I do? Don't worry. we will break it down later. I also recently used this function, so I have collected this document from Baidu. I hope it will be helpful to you.


Http://phpshao.cublog.cn target = _ blank> Dragon heart column phpshao.cublog.cn
If ($ _ POST ['action'] = 'doup ')
{
$ Uptypes = array ('image/jpg ', 'image/jpeg', 'image/pjpeg ', 'image/GIF'); // upload the image file type list
$ WFile = $ _ FILES ['upfile']; // Obtain the file path
$ Waterimg = "water.gif"; // watermark image path // print_r ($ wFile );
If (in_array ($ wFile ['type'], $ uptypes ))
{// Check the file type. if the uploaded file is jpg or gif, add a watermark.
If (strstr ($ wFile ['type'], "jp "))
{// If the image type to be uploaded is jpg, pjpeg, or jpeg, use imagecreatefromjpeg to read the target file.
$ Im = imageCreatefromjpeg ($ wFile ['tmp _ name']);
$ Wfilew = imagesx ($ im); // Obtain the image width.
$ Wfileh = imagesy ($ im); // get the image height}
Else
{// Otherwise, if the uploaded image type is gif, use imagecreatefromgif to read the target file.
$ Im = imageCreatefromgif ($ wFile ['tmp _ name']);
$ Wfilew = imagesx ($ im); // Obtain the image width.
$ Wfileh = imagesy ($ im); // get the image height
}
// Set the mixed mode
Imagealphablending ($ im, true );
// Read the watermark file
$ Im2 = imagecreatefrompng ($ waterimg); // if the watermark image is jpg, you can change it to $ im2 = imagecreatefromjpeg ($ waterimg) // $ white = imagecolorallocate ($ im2, 255,255,255 );
// Imagecolortransparent ($ im2, $ white); // Set the transparent color. do not set the two sentences to $ waterw = imagesx ($ im2); // Obtain the width of the watermark image.
$ Waterh = imagesy ($ im2); // Obtain the watermark image height.
// Randomly add the watermark to the image
$ Randval = rand (); // A random number is generated between 0-9.
If ($ randval = 0 | $ randval = 3 | $ randval = 2 | $ randval = 8 | $ randval = 7) {// more locations can be added here
$ Wimgx = 5; $ wimgy = 5; // put the upper left corner
} Else {
$ Wimgx = $ wfilew-5-$ waterw; $ wimgy = $ wfileh-5-$ waterh; // put the upper right corner
} // Copy the watermark to the target file
Imagecopy ($ im, $ im2, $ wimgx, $ wimgy, 0, 0, $ waterw, $ waterh );

// Output image
If (strstr ($ wFile ['type'], "jp") {// same as above
Imagejpeg ($ im, $ wFile ['tmp _ name']);
} Else {
Imagegif ($ im, $ wFile ['tmp _ name']);
}
Imagedestroy ($ im );
Imagedestroy ($ im2 );
Copy ($ wFile ['tmp _ name'], $ wFile ['name']); // Upload
}
Else echo "the image does not match !! ";
}
?>

Why? What should I do? Don't worry. we will break it down later. I also recently used this function...

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.