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. next we will break it down for you & lt; formaction = & quot; & lt ;? =$ _ SERVER [& amp; #39; PHP_SELF & amp; #39;]? & Gt; & quot; method = & quot; post & quot; enctype =
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 for you below
Longhearted 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 !! ";
}
?>