Home > Backend Development > PHP Tutorial > Add image watermark to uploaded images in php_PHP tutorial

Add image watermark to uploaded images in php_PHP tutorial

WBOY
Release: 2016-07-21 14:52:14
Original
1122 people have browsed it

 

    相信大家都知道怎么用PHP为图片增加文字水印,可是如果水印效果为图片呢?该怎么办?别担心,下文就为您分解。我也是近日用到此功能,所以去百度上搜集到了这篇文档,希望对各位有所帮助.

 


 
   
     
   
   
     
   
   
     










上传图片

       
         
           
           
         
         
         
           
         
       
路径:

               



         

       
 
 
 


Dragon’s Heart Column phpshao.cublog.cn
if($_POST['action']=='doup')
{
$uptypes=array('image/jpg','image/jpeg','image/pjpeg','image/gif');//Upload image file type list
$wFile=$_FILES['upfile'];//Get 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 a jpg or gif image, add a watermark
If(strstr($wFile['type'],"jp"))
{ //If the uploaded image type is jpg, pjpeg, jpeg, use imagecreatefromjpeg to read the target file
$im = imageCreatefromjpeg($wFile['tmp_name']);
$wfilew=imagesx($im);//Get the width of the image
$wfileh=imagesy($im);//Get the height of the image
}
else
{//Otherwise, if the uploaded image type is gif, use imagecreatefromgif to read the target file
$im = imageCreatefromgif($wFile['tmp_name']);
$wfilew=imagesx($im);//Get the width of the image
$wfileh=imagesy($im);//Get the height of the image
}
//Set blending mode
Imagealphablending($im, true);
//Read watermark file
$im2 = imagecreatefrompng($waterimg);//If the watermark image is jpg, this can be changed to $im2 = imagecreatefromjpeg($waterimg)
//$white = imagecolorallocate($im2, 255, 255, 255);
// imagecolortransparent($im2,$white); //Set transparent color, these two sentences are not needed
$waterw=imagesx($im2);//Get the width of the watermark image
$waterh=imagesy($im2);//Get the height of the watermark image
//Randomly put watermarks into pictures
$randval = rand(0,9);//Generate random numbers between 0-9
if($randval==0||$randval==3||$randval==2||$randval==8||$randval==7){//More positions can be improved here
$wimgx=5;$wimgy=5;//Place it in the upper left corner
}else{
$wimgx=$wfilew-5-$waterw;$wimgy=$wfileh-5-$waterh;//Place in the upper right corner
}
//Copy the watermark to the target file
imagecopy($im, $im2, $wimgx, $wimgy, 0, 0, $waterw,$waterh);
 
//Output picture
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 picture does not match!!";
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371650.htmlTechArticleI believe everyone knows how to use PHP to add text watermarks to images, but what if the watermark effect is an image? what to do? Don’t worry, we’ll break it down for you below. I have also used this function recently...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template