PHP给图片添加水印
作者:程序员11 时间:2020-05-14 人气:651 QQ交流群\邮箱:1003265987@qq.com
php给图片添加水印
要展示的内容
//指定图片路径 $src = 'timg.jpg'; //获取图片信息 $info = getimagesize($src); $width = $info['0']; $height = $info['1']; //获取图片扩展名 $type = image_type_to_extension($info[2],false); //动态的把图片导入内存中 $fun = "imagecreatefrom{$type}"; $image = $fun('timg.jpg'); // font file 字体样式 $fontfile =dirname(dirname(__FILE__)).'1111.ttf'; // font color 字体颜色 $color = imagecolorallocate($image, 0x00, 0x00, 0x00); //水印 $text = "1200呃呃呃呃呃ssssss"; //计算字符串的宽度 $pos = imagettfbbox(30 , 0, $fontfile, $text); //字符串所占宽度为:$pos[2] - $pos[0] $w = $pos[2] - $pos[0]; //字符串所占高度为:$pos[5] - $pos[3]注意这个为负数,需要转化成正的 $h = $pos[5] - $pos[3]; // write text to image with specified font 绘制文字 这里面的x,y自己在做微调 imagettftext($image, 30, 0, $width-$w-10, $height-(-$h)+10, $color, $fontfile, $text); //指定输入类型 header('Content-type:'.$info['mime']); //动态的输出图片到浏览器中 $func = "image{$type}"; $func($image); //生成的海报文件名 $haibao_filename = md5('123').".jpg"; imagejpeg($image, $haibao_filename, 95); //输出到目标文件 //销毁图片 imagedestroy($image); ?>
需要的文件如下图
温馨提示:
欢迎阅读本文章,觉得有用就多来支持一下,没有能帮到您,还有很多文章,希望有一天能帮到您。
- 上一篇:PHP获取ip
- 下一篇:PHP添加水印图片和文字