图片水印

if (@fopen($dst_path,'r')) {
//图片名称(不带后缀)
$basename = strstr(basename($dst_path),'.',true);
//图片后缀
$suffix = strstr(basename($dst_path),'.',false);
// $suffix = explode('.',$dst_path);
$base = basename($dst_path);
$store_route = strstr($dst_path,$base,true);
//重组图片路径
$store_watermark_image = $store_route.$basename.'_watermark'.$suffix;

if (@fopen($store_watermark_image,'r')) {
//水印图片路径,存在
return $store_watermark_image;
}else{
//图片名称(不带后缀)
//图片宽高
$image_size = @getimagesize($dst_path);

if (($image_size[0]) == 60 && ($image_size[1] == 60)) {
//图片大小60X60
$src_path = BASE_UPLOAD_PATH . '/tmp/watermark_60.jpg';

}elseif (($image_size[0]) == 240 && ($image_size[1] == 240)) {
//图片大小240X240
$src_path = BASE_UPLOAD_PATH . '/tmp/watermark_240.jpg';

}elseif (($image_size[0]) == 320 && ($image_size[1] == 320)) {
//图片大小320X320
$src_path = BASE_UPLOAD_PATH . '/tmp/watermark_320.jpg';

}elseif (($image_size[0]) == 360 && ($image_size[1] == 360)) {
//图片大小360X360
$src_path = BASE_UPLOAD_PATH . '/tmp/watermark_360.jpg';

}elseif (($image_size[0]) == 800 && ($image_size[1] == 800)) {
//图片大小800X800
$src_path = BASE_UPLOAD_PATH . '/tmp/watermark_800.jpg';

}elseif (($image_size[0]) == 1280 && ($image_size[1] == 1280)) {
//图片大小1280X1280
$src_path = BASE_UPLOAD_PATH . '/tmp/watermark_1280.jpg';

}
if ($dst_path && $src_path) {
//获取水印图片的宽高
$image_siz_watermark = getimagesize($src_path);
//创建图片的实例
$dst = imagecreatefromstring(file_get_contents($dst_path));
$src = imagecreatefromstring(file_get_contents($src_path));

//将水印图片复制到目标图片上,最后个参数80是设置透明度,这里实现半透明效果
$img = imagecopymerge($dst, $src, 0, $image_size[1]-$image_siz_watermark[1], 0, 0, $image_siz_watermark[0], $image_siz_watermark[1], 80);
if ($img) {
//如果水印图片本身带透明色,则使用imagecopy方法
//输出图片
list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
//写入地址拼接
$img_header = strstr($store_route, 'shop');
$img_path = BASE_UPLOAD_PATH . DS . $img_header;
switch ($dst_type) {
case 1://GIF
imagegif($dst,$img_path.$basename.'_watermark.gif');
$store_watermark_image = $store_route.$basename.'_watermark.gif';
break;
case 2://JPG
imagejpeg($dst, $img_path.$basename.'_watermark.jpg');
$store_watermark_image = $store_route.$basename.'_watermark.jpg';
break;
case 3://PNG
imagepng($dst,$img_path.$basename.'_watermark.png');
$store_watermark_image = $store_route.$basename.'_watermark.png';
break;
default:
break;
}
imagedestroy($dst);
imagedestroy($src);
return $store_watermark_image;
}else{
return $dst_path;
}

}

}
}

posted @ 2018-11-26 18:22  进击的巨人X  阅读(180)  评论(0编辑  收藏  举报