<?php /** * Created by PhpStorm. * User: 12858 * Date: 2019/9/23 * Time: 22:37 * * 给图片添加文字水印 * 做二维码海报 添加昵称 */ //图片路径[想要操作的图片] $imgPath = $_SERVER['DOCUMENT_ROOT'].'/images/a1.jpg'; //字体水印内容 $fontCon = 'hello word'; //字体库路径 $funFont = $_SERVER['DOCUMENT_ROOT'].'/font/simkai.ttf'; //$imgPath 图片路径[想要操作的图片] //$funFont 字体库路径 //$fontCon 字体水印内容 //函数调用 fontMark($imgPath,$fontCon,$funFont); function fontMark($imgPath,$fontCon,$funFont){ //获取图片基本信息 $imgInfo = getimagesize($imgPath); //通过图片的编号获取图片类型 jpg png jpeg $imgType = image_type_to_extension($imgInfo['2'],false); //在内存中创建一个和该图像类型一样的图像 // $funImg = imagecreatefrompng() // $funImg = imagecreatefromjpeg() // $funImg = imagecreatefromgif() $funImg = "imagecreatefrom{$imgType}"; //复制图片到内存 $image = $funImg($imgPath); //开始操作图片 //设置字体 颜色 //$image 内存图片 颜色 $fontColor = imagecolorallocatealpha($image,255,255,255,50); //写入文字 imagettftext($image,20,0,20,30,$fontColor,$funFont,$fontCon); //浏览器输出 header('Content-type:'.$imgInfo['mime']); $outImage = "image{$imgType}"; //输出图片 //$outImage($image); //保存图片 //新图片 名称及保存目录 $outPath = $_SERVER['DOCUMENT_ROOT'].'/markimg/'.date('YmdHis').'.'.$imgType; $outImage($image,$outPath); //释放内存 imagedestroy($image); }