如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
用到的函数
//imagecopy
//1,2水印图和原图资源
//3,4 水印图的起始点坐标
//5,6 原图的起始点坐标
//7,8 宽高
//9 透明度 (0-100)
imagecopymerge($img , $water, $x, $y, 0, 0, $water_w, $water_h, 50);
案例
<?php
header("content-type:text/html;chsrset=urf-8");
date_default_timezone_set("PRC");
function picMark ($src_img, $water_img, $postion=1)
{
//获取原图信息
$srcinfo = getimagesize($src_img);
//水印图信息
$waterinfo = getimagesize($water_img);
//判断
if (!$srcinfo || !$waterinfo) {
echo "非法图片";
return false;
}
//得到原图和水印图宽高
list($src_w, $src_h) = $srcinfo;
list($water_w, $water_h) = $waterinfo;
if (($water_w > $src_w) || ($water_h > $src_h)) {
echo "水印图过大";
return false;
}
list($src_type, $src_subtype) = explode('/', $srcinfo['mime']);
list($water_type, $water_subtype) = explode('/', $waterinfo['mime']);
//生成打开函数的名称
$open_src = 'imagecreatefrom'.$src_subtype;
$open_water = 'imagecreatefrom'.$water_subtype;
//保存图片的函数名
$save_img = 'image'.$src_subtype;
//水印位置
switch ($postion) {
case 1:
$x = 0;
$y = 0;
break;
case 2:
$x = $src_w /2 - $water_w/2;
$y = 0;
break;
default:
break;
}
//打开图片资源
$img = $open_src($src_img);
$water = $open_water($water_img);
imagecopymerge($img , $water, $x, $y, 0, 0, $water_w, $water_h, 50);
$ext = pathinfo($src_img, PATHINFO_EXTENSION);
//生成保存路径
$save_img_name = './cut/'.date('YmdHis').uniqid().'.'.$ext;
//保存图片
$save_img($img, $save_img_name);
//关闭
imagedestroy($img);
imagedestroy($water);
return $save_img_name;
}
echo picMark('./imgs/01.png', './imgs/qq.png',1);