Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)归海一刀
published in(发表于) 2014/2/17 7:30:16
PHP实例程序:实现给上传图片加水印图案的做法_[PHP教程]

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 "图片不符合!!";
}
?>







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759