search
Homephp教程php手册PHP实例程序:实现给上传图片加水印图案的做法


 
   
     
   
   
     
   
   
     
   
   
     
   
   
     
   
   
     
   
 
上传图片
 

       
         
           
           
         
         
         
           
         
       
路径:

               
  


         

       
 
 
 




网页教学网webjx.com

if($_POST['action']=='doup')
{
$uptypes=array('image/jpg','image/jpeg','image/pjpeg','image/gif');//上传图片文件类型列表
$wFile=$_FILES['upfile'];//取得文件路径
$waterimg="water.gif";//水印图片路径
//print_r($wFile);
if(in_array($wFile['type'], $uptypes))
{//检查文件类型,若上传的文件为jpg或gif图片则加水印
   if(strstr($wFile['type'],"jp"))
    { //若上传图片类型为jpg,pjpeg,jpeg,则用imagecreatefromjpeg读取目标文件
    $im = imageCreatefromjpeg($wFile['tmp_name']);
$wfilew=imagesx($im);//取得图片的宽
$wfileh=imagesy($im);//取得图片的高
   }
   else
    {//否则若上传图片类型为gif,则用imagecreatefromgif读取目标文件
    $im = imageCreatefromgif($wFile['tmp_name']);
$wfilew=imagesx($im);//取得图片的宽
$wfileh=imagesy($im);//取得图片的高
}
   //设定混合模式
    imagealphablending($im, true);
    //读取水印文件
    $im2 = imagecreatefrompng($waterimg);//若水印图片为jpg,则此去可改为$im2 = imagecreatefromjpeg($waterimg)
 //$white = imagecolorallocate($im2, 255, 255, 255);
  // imagecolortransparent($im2,$white);   //设透明色,这两句可不要
$waterw=imagesx($im2);//取得水印图片的宽
$waterh=imagesy($im2);//取得水印图片的高
    //随机放水印到图片中
$randval = rand(0,9);//在0-9之间产生随机数
if($randval==0||$randval==3||$randval==2||$randval==8||$randval==7){//此处还可完善放更多位置
$wimgx=5;$wimgy=5;//放左上角
}else{
$wimgx=$wfilew-5-$waterw;$wimgy=$wfileh-5-$waterh;//放右上角
}
//拷贝水印到目标文件
imagecopy($im, $im2, $wimgx, $wimgy, 0, 0, $waterw,$waterh);
   
//输出图片
   if(strstr($wFile['type'],"jp")){ //同上
imagejpeg($im,$wFile['tmp_name']);
}else{
imagegif($im,$wFile['tmp_name']);
}
    imagedestroy($im);
    imagedestroy($im2);
copy($wFile['tmp_name'],$wFile['name']);  //上传
}
else echo "图片不符合!!";
}
?>



Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools