php生成文字图片水印

一、素材准备

电脑一个,图片一张,字体库一个,哦了

字体库在哪找呢?C盘——Fonts文件夹

搜微软雅黑就可以了

二、代码实现

<?php

//配置图片信息
$src="cat.jpg";

//获取图片信息
$info=getimagesize($src);

//通过图像的编号获图片的类型
$type=image_type_to_extension($info[2],false);   //.jpeg  如果不想要点 就false掉

//在内存中创建一个和我们图像类型一样的图像
$fun="imagecreatefrom{$type}";
$image=$fun($src);

//设置字体的路径
$font="msyhbd.ttc";

//填写我们的水印内容
$content="我爱你中国";

//设置字体的颜色和透明度
$col=imagecolorallocatealpha($image,255,255,255,50);

//写入文字
imagettftext($image,20,0,20,30,$col,$font,$content);

//浏览器输出
header('Content-type:'.$info['mime']);
$func = "image{$type}";
$func($image);

//保存图片
$func($image,'save.'.$type);

//销毁图片
imagedestroy($image);

?>

三、 本地运行即可

猜你喜欢

转载自blog.csdn.net/weixin_41689199/article/details/113735509