Home > Backend Development > PHP Tutorial > php给图片添加文字水印

php给图片添加文字水印

WBOY
Release: 2016-06-23 13:29:39
Original
808 people have browsed it

PHP对图片的操作用到GD库,这里我们介绍如何给图片添加文字水印。

大致分为四步:

1.打开图片

2.操作图片

3.输出图片

4.销毁图片

下面我们上代码来具体讲解每步的实现过程:

<?php /*打开图片*///1.配置图片路径$src = "bg.jpg";//2.获取图片信息$info = getimagesize($src);//3.通过编号获取图像类型$type = image_type_to_extension($info[2],false);//4.在内存中创建和图像类型一样的图像$fun = "imagecreatefrom".$type;//5.图片复制到内存$image = $fun($src);/*操作图片*///1.设置字体的路径$font = "msyh.ttf";//2.填写水印内容$content = "水印文字♂some special words are supported.";//3.设置字体颜色和透明度$color = imagecolorallocatealpha($image, 50, 50, 50, 50);//4.写入文字imagettftext($image, 20, 0, 0, 30, $color, $font, $content);/*输出图片*///浏览器输出header("Content-type:".$info['mime']);$fun = "image".$type;$fun($image);//保存图片$fun($image,'bg_res.'.$type);/*销毁图片*/imagedestroy($image);
Copy after login

代码解释如下:

本实例需要一张图片和一个字体文件,和php代码放在同一目录下

字体文件的加载可以参考以下文章:

点击打开链接


PHP相关的图像操作还有很好玩的验证码制作:

汉字验证码

图片验证码

字符验证码

版权声明:本文为博主原创文章,未经博主允许不得转载。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template