PHP to upload pictures and image watermark

Source: Internet
Author: User
Tags transparent color reset

I believe we all know how to use PHP to add text to the image watermark, but if the watermark effect for the picture? What should I do? Don't worry, the following is the decomposition for you. I also recently used this function, so go to Baidu to collect this document, I hope to help you.

<form action= "<?=$_server[' php_self ']?>" method= "post" enctype= "Multipart/form-data" Name= "Form1" id= " Form1 ">


<table width= "486" border= "0" align= "center" cellpadding= "0" cellspacing= "0" >


<tr>


<td width= "498" height= "background=" "Image/top.gif" ><div align= "center" class= "style2" > Upload pictures < /div></td>


</tr>


<tr>


<td> </td>


</tr>


<tr>


<td><div align= "right" >


<table width= "426" border= "0" align= "center" cellpadding= "0" cellspacing= "0" >


<tr>


<td width= "height=" nowrap= "nowrap" > Path:</td>


<td width= "362" ><input name= "upfile" type= "file" id= "Upfile" size= "/></td>
"

</tr>


         


<tr>


<td height= "colspan=" 2 "><div align=" center ">


<input type= "Submit" name= "Submission" value= "submitted"/>


  


<input type= "reset" name= "Submit" value= "reset"/>


</div></td>


</tr>


</table>


</div>


<div align= "center" ></div></td>


</tr>


<tr>


<td> </td>


</tr><input type= "hidden" name= ' action ' value= ' Doup ' >


<tr>


<TD height= "background=" "Image/top.gif" > </td>


</tr>


<tr>


<td> </td>


</tr>


</table>


&lt;/form&gt; <br> <a href=http://phpshao.cublog.cn target=_blank> Dragon's Heart column phpshao.cublog.cn</a>
?
if ($_post[' action ']== ' Doup ')
{
$uptypes =array (' image/jpg ', ' image/jpeg ', ' image/pjpeg ', ' image/gif '); Upload picture file type list
$wFile =$_files[' upfile '];//get file path
$waterimg = "water.gif";//Watermark Picture Path Print_r ($wFile);
if (In_array ($wFile [' type '], $uptypes))
{//Check file type, if the uploaded file is a jpg or GIF picture then add watermark
if (Strstr ($wFile [' type '], "JP"))
{///If upload picture type is jpg,pjpeg,jpeg, read target file with Imagecreatefromjpeg
$im = Imagecreatefromjpeg ($wFile [' tmp_name ']);
$wfilew =imagesx ($im);//Get the width of the picture
$wfileh =imagesy ($im);//Get Picture high }
Else
{//Otherwise, if the uploaded picture type is GIF, read the target file with Imagecreatefromgif
$im = Imagecreatefromgif ($wFile [' tmp_name ']);
$wfilew =imagesx ($im);//Get the width of the picture
$wfileh =imagesy ($im);//Get the picture high
}
Set Blending mode
Imagealphablending ($im, true);
Read Watermark File
$im 2 = imagecreatefrompng ($waterimg);//If the watermark picture is JPG, then this can be changed to $IM2 = Imagecreatefromjpeg ($waterimg) $white = Imagecolorallocate ($im 2, 255, 255, 255);
Imagecolortransparent ($im 2, $white); Set the transparent color, the two sentences can not be $waterw =imagesx ($im 2);//Get the width of the watermark picture
$waterh =imagesy ($im 2);//Get watermark Picture High
Random water prints to the picture
$randval = rand (0,9);//Generate a random number between 0-9
if ($randval ==0| | $randval ==3| | $randval ==2| | $randval ==8| | $randval ==7) {//Here you can also improve your placement
$WIMGX =5; $wimgy =5;//in upper left corner
}else{
$WIMGX = $wfilew $waterw; $wimgy = $wfileh;
} 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!! ";
}
?>

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.