PHP to upload pictures and image watermark _php Tutorial

Source: Internet
Author: User
Tags transparent color
I believe we all know how to use PHP for images to add text watermark, but if the watermark effect is a picture it? What should I do? Don't worry, the following will break it down for you. I also recently used this feature, so to Baidu on the collection of this document, I hope you have some help.


Dragon's Heart column phpshao.cublog.cn
if ($_post[' action ']== ' Doup ')
{
$uptypes =array (' image/jpg ', ' image/jpeg ', ' image/pjpeg ', ' image/gif ');//Upload picture file type list
$wFile =$_files[' upfile '];//get the file path
$waterimg = "water.gif";//Watermark Picture path//print_r ($wFile);
if (In_array ($wFile [' type '], $uptypes))
{//Check file type, add watermark if upload file is jpg or GIF picture
if (Strstr ($wFile [' type '], "JP"))
{//If the upload image type is jpg,pjpeg,jpeg, the target file is read with Imagecreatefromjpeg
$im = Imagecreatefromjpeg ($wFile [' tmp_name ']);
$wfilew =imagesx ($im);//Get the width of the picture
$wfileh =imagesy ($im);//Get the picture high}
Else
{//otherwise if the upload image type is gif, use Imagecreatefromgif to read the target file
$im = Imagecreatefromgif ($wFile [' tmp_name ']);
$wfilew =imagesx ($im);//Get the width of the picture
$wfileh =imagesy ($im);//Get the picture high
}
Setting the blending mode
Imagealphablending ($im, true);
Read Watermark File
$im 2 = imagecreatefrompng ($waterimg);//If the watermark image is jpg, then this go can be changed to $IM2 = Imagecreatefromjpeg ($waterimg)//$white = Imagecolorallocate ($im 2, 255, 255, 255);
Imagecolortransparent ($im 2, $white); Set the transparent color, these two sentences can not $waterw=imagesx ($im 2);//Obtain the width of the watermark picture
$waterh =imagesy ($im 2);//Get a high watermark image
Random release to the picture
$randval = rand (0,9);//Generate random numbers between 0-9
if ($randval ==0| | $randval ==3| | $randval ==2| | $randval ==8| | $randval ==7) {//Here can also be improved to put more locations
$WIMGX =5; $wimgy =5;//the upper left corner
}else{
$WIMGX = $wfilew-$waterw; $wimgy = $wfileh-$waterh;//top-right corner
}//copy watermark to target file
Imagecopy ($im, $im 2, $WIMGX, $wimgy, 0, 0, $waterw, $waterh);

Output picture
if (Strstr ($wFile [' type '], "JP")) {//Ibid.
Imagejpeg ($im, $wFile [' tmp_name ']);
}else{
Imagegif ($im, $wFile [' tmp_name ']);
}
Imagedestroy ($im);
Imagedestroy ($im 2);
Copy ($wFile [' Tmp_name '], $wFile [' name ']); Upload
}
else echo "picture does not match!! ";
}
?>

http://www.bkjia.com/PHPjc/371650.html www.bkjia.com true http://www.bkjia.com/PHPjc/371650.html techarticle I believe we all know how to use PHP for images to add text watermark, but if the watermark effect is a picture it? What should I do? Don't worry, the following will break it down for you. I also recently used this feature ...

  • 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.