php微擎功能-图片加水印
在微擎中对图片进行水印生成, 水印生成的位置在图片的右下角, 按比例进行缩放添加.
直接上源码!
/**
* 给图片添加水印
* @param $dst_file string 原图位置(绝对位置或网络位置)
* @param $logo_file string 水印图片位置
* @return string 加入水印后的图片地址
*/
public static function addWaterMark($dst_file, $logo_file)
{
// 创建图片实例
$dst = imagecreatefromstring(file_get_contents($dst_file));
$logo = imagecreatefromstring(file_get_contents($logo_file));
// 获取水印图片的宽高
list($logo_w, $logo_h) = getimagesize($logo_file);
list($dst_w, $dst_h, $dst_type) = getimagesize($dst_file);
$logoNewHeight = $dst_h / 6;
$logoNewWidth = $logo_w * $logoNewHeight/$logo_h;
// 将水印图片复制到目标图片上,最后一个参数50 是设置透明度,这里为半透明效果
// imagecopymerge($dst, $logo, 10, 10, 0, 0, $logo_w, $logo_h, 50);
// 如果水印图片本身带透明色, 则使用imagecopy方法
imagecopyresized($dst, $logo, 10, $dst_h-$logoNewHeight-10, 0, 0, $logoNewWidth, $logoNewHeight, $logo_w, $logo_h);
//输出图片
$strs="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
$filename=substr(str_shuffle($strs),mt_rand(0,strlen($strs)-11),10);
$toFilePath = 'water_marked/'.time().'_'.$filename;
switch($dst_type){
case 1://GIF
$toFilePath .= '.gif';
imagegif($dst, ATTACHMENT_ROOT.$toFilePath);
break;
case 2://JPG
$toFilePath .= '.jpg';
imagejpeg($dst, ATTACHMENT_ROOT.$toFilePath);
break;
case 3://PNG
$toFilePath .= '.png';
imagepng($dst, ATTACHMENT_ROOT.$toFilePath);
break;
default:
break;
}
return $toFilePath;
}