您的位置 首页 php

PHP+GD库实现打文字水印,图片水印,php给图片打水印

现在自媒体泛滥,每个自媒体账号都会给自己的文章图片打上自己的水印,那么水印是怎么实现的呢?

今天就给大家看看水印的具体实现,话不多说,直接上代码:

<?php
//告诉浏览器以jpeg图像的方式显示
 header ("Content-type:image/jpeg;charset=utf-8");
//创建画布
$width = 750;
$height = 1334;
//新建一个空白图像资源
$image = imagecreate($width, $height);
//创建背景颜色
$white = imagecolorallocate($image, 255, 255, 255);
//创建字体颜色
$red = imagecolorallocate($image, 255, 0, 0);
//字符, 转码 
$font = mb_convert_encoding('segmentfault', "html-entities", "utf-8"); 
//开始绘画
imagettftext ($image, 50, 0, 200, 200, $red, 'msyh.ttc','segmentfault.com');
imagettftext ($image, 60, 0, 200, 300, $red, 'msyh.ttc',$font);
//生成图像
imagejpeg($image,"3. JPG ");
//目标图像
$dst_path = "3.jpg";
//水印图片
$src_path = 'sf. png ';
//创建图片的实例
$dst = imagecreatefromstring(file_get_contents($dst_path));
$src = imagecreatefromstring(file_get_contents($src_path));
//获取水印图片的宽高
list($src_w, $src_h) = getimagesize($src_path);
//将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果,两个20是控制水印坐标位置
// imagecopymerge($dst, $src, 20, 20, 0, 0, $src_w, $src_h, 50);
//如果水印图片本身带透明色,则使用imagecopy方法
imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
//输出图片
list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
switch ($dst_type) {
 case 1://GIF
 header('Content-Type: image/gif');
 imagegif($dst);
  break ;
 case 2://JPG
 header('Content-Type: image/jpeg');
 imagejpeg($dst);
 break;
 case 3://PNG
 header('Content-Type: image/png');
 imagepng($dst);
 break;
 default:
 break;
}
imagedestroy($dst);
imagedestroy($src);
$shuiyin = "gd.php";
//销毁资源
imagedestroy($image);
 

实现水印的图片

好了 效果就已经实现了,你还不快去自己试试吗?

如果想要学习交流PHP的朋友,可以关注小编,私信【学习交流】手机用户可以直接私信,电脑端尚未开放此功能,需要下载app,我已经设置了自动回复,具体后续会自动回复各位。

文章来源:智云一二三科技

文章标题:PHP+GD库实现打文字水印,图片水印,php给图片打水印

文章地址:https://www.zhihuclub.com/153749.shtml

关于作者: 智云科技

热门文章

网站地图