图片添加水印
Posted 码蚁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片添加水印相关的知识,希望对你有一定的参考价值。
<?php
wather(‘3.jpg‘);
//图片水印的封装
function wather($soruce,$water=‘2.jpg‘,$position=9,$alpha = 100, $type = "jpeg",$path = ‘test‘,$isRanName = true)
{
//打开图片
$soruceRes = open($soruce);
$waterRes = open($water);
//获取图片的大小 算出位置
$soruceInfo = getimagesize($soruce);
$waterInfo = getimagesize($water);
//算出位置
switch($position)
{
case 1:
$x = 0;
$y = 0;
break;
case 2:
$x = ($soruceInfo[0] - $waterInfo[0]) / 2;
$y = 0;
break;
case 3:
$x = $soruceInfo[0] - $waterInfo[0];
$y = 0;
break;
case 4:
$x = 0;
$y = ($soruceInfo[1] - $waterInfo[1]) / 2;
break;
case 5:
$x = ($soruceInfo[0] - $waterInfo[0]) / 2;
$y = ($soruceInfo[1] - $waterInfo[1]) / 2;
break;
case 6:
$x = $soruceInfo[0] - $waterInfo[0];
$y = ($soruceInfo[1] - $waterInfo[1]) / 2;
break;
case 7:
$x = 0;
$y = $soruceInfo[1] - $waterInfo[1];
break;
case 8:
$x = ($soruceInfo[0] - $waterInfo[0]) / 2;
$y = $soruceInfo[1] - $waterInfo[1];
break;
case 9:
$x = $soruceInfo[0] - $waterInfo[0];
$y = $soruceInfo[1] - $waterInfo[1];
break;
default:
$x = mt_rand(0,$soruceInfo[0] - $waterInfo[0]);
$y = mt_rand(0,$soruceInfo[1] - $waterInfo[1]);
break;
}
//把x y 求出来的值供两张图片合并的时候用
imagecopymerge($soruceRes,$waterRes,$x,$y,0,0, $waterInfo[0],$waterInfo[1],$alpha);
$func = ‘image‘.$type;
//处理path路径 是否启用随机文件名
if($isRanName)
{
$name = uniqid().‘.‘.$type;
}else{
$pathinfo = pathinfo($soruce);
//var_dump($pathinfo);
$name = $pathinfo[‘filename‘].‘.‘.$type;
}
$path = rtrim($path,‘/‘).‘/‘.$name;
$func($soruceRes,$path);
imagedestroy($soruceRes);
imagedestroy($waterRes);
}
//打开图片的函数
function open($path)
{
//判断文件是否存在
if(!file_exists($path))
{
exit(‘文件不存在‘);
}
$info = getimagesize($path);
//var_dump($info );
switch($info[‘mime‘])
{
case ‘image/jpeg‘:
case ‘image/jpg‘:
case ‘image/pjpeg‘:
$res = imagecreatefromjpeg($path);
break;
case ‘image/png‘;
$res = imagecreatefrompng($path);
break;
case ‘image/gif‘:
$res = imagecreatefromgif($path);
break;
case ‘image/wbmp‘:
case ‘image/bmp‘:
$res = imagecreatefromwbmp($path);
break;
}
return $res;
}
以上是关于图片添加水印的主要内容,如果未能解决你的问题,请参考以下文章