Home >php教程 >php手册 >PHP shares two methods of adding watermark to images

PHP shares two methods of adding watermark to images

WBOY
WBOYOriginal
2016-10-11 14:03:051171browse

This article introduces to programmers two methods of adding watermarks to images in PHP. Interested programmers can refer to the source code of this article.

Method 1: The simplest watermarking method using PHP

<?<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;"> http://www.manongjc.com</span>
<span style="color: #800080;">$img</span> = imagecreatefromjpeg(<span style="color: #800080;">$filename</span><span style="color: #000000;">);
</span><span style="color: #800080;">$logo</span> = imagecreatefromjpeg(<span style="color: #800080;">$filename</span><span style="color: #000000;">);
</span><span style="color: #008000;">/*</span><span style="color: #008000;">imagecraetefromjpeg-由文件或URL创建一个新图像
imagecreatefromjpeg(string $filename)
如果启用了fopen包装器,URL可以作为文件名</span><span style="color: #008000;">*/</span><span style="color: #000000;">
imagecopy(</span><span style="color: #800080;">$img</span>,<span style="color: #800080;">$logo</span>,15,15,0,0,<span style="color: #800080;">$width</span>,<span style="color: #800080;">$height</span><span style="color: #000000;">);
</span><span style="color: #008000;">/*</span><span style="color: #008000;">imagecopy($dst_im,$src_im,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h)
$dst_im是背景图像,就是需要添加水印的图片
$src_im是水印图片;$dst_x,#dst_y需要把水印放到背景图片的(x,y)坐标;
$src_x,$src_y是截取水印的图片的开始坐标
$width,$height是截取的图片的就是水印的长度和宽度</span><span style="color: #008000;">*/</span>
<span style="color: #800080;">$url</span> = 'http://www.stchat.cn/data/attachment/forum/201506/12/100759pidbdaydh8dy7iby.jpg'<span style="color: #000000;">;
</span><span style="color: #800080;">$content</span> = <span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$url</span>);<span style="color: #008000;">//</span><span style="color: #008000;">把url写入到content这个变量里面</span><span style="color: #008000;">
/*</span><span style="color: #008000;">file_get_contents--将整个文件读入到一个字符串</span><span style="color: #008000;">*/</span>
<span style="color: #800080;">$filename</span> = 'tmp.jpg'<span style="color: #000000;">;
</span><span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$filename</span>,<span style="color: #800080;">$content</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">把所有内容放到filename这个变量里面,第一个存放的是背景图片</span><span style="color: #008000;">
/*</span><span style="color: #008000;">file_put_contents(string $filename,mixed $data)将一个字符串写入一个文件
filename要被写入数据的文件名
data要写入的数据,类型可以是string,array或者是stream资源</span><span style="color: #008000;">*/</span>
<span style="color: #800080;">$url</span> = ''<span style="color: #000000;">;
</span><span style="color: #008080;">file_put_contents</span>('logo.png',<span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$url</span><span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;">第二个是水印的图片</span>
<span style="color: #800080;">$img</span> = imagecreatefromjpeg(<span style="color: #800080;">$filename</span><span style="color: #000000;">);
</span><span style="color: #800080;">$logo</span> = imagecreatefrompng('logo.png'<span style="color: #000000;">);
</span><span style="color: #800080;">$size</span> = <span style="color: #008080;">getimagesize</span>('logo.png'<span style="color: #000000;">);
</span><span style="color: #008000;">/*</span><span style="color: #008000;">getimagesize()获得图像大小</span><span style="color: #008000;">*/</span><span style="color: #000000;">
imagecopy(</span><span style="color: #800080;">$img</span>,<span style="color: #800080;">$logo</span>,15,15,0,0,<span style="color: #800080;">$size</span>[0],<span style="color: #800080;">$size</span>[1<span style="color: #000000;">]);
</span><span style="color: #008080;">header</span>("centent-type:image/jpeg"<span style="color: #000000;">);
imagejpeg(img);
</span>?>

Method 2: Add text watermark to pictures with php

<?<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;"> http://www.manongjc.com/article/593.html</span><span style="color: #008000;">
/*</span><span style="color: #008000;">给图片加文字水印的方法</span><span style="color: #008000;">*/</span>
<span style="color: #800080;">$dst_path</span> = 'http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg'<span style="color: #000000;">;
</span><span style="color: #800080;">$dst</span> = imagecreatefromstring(<span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$dst_path</span><span style="color: #000000;">));
</span><span style="color: #008000;">/*</span><span style="color: #008000;">imagecreatefromstring()--从字符串中的图像流新建一个图像,返回一个图像标示符,其表达了从给定字符串得来的图像
图像格式将自动监测,只要php支持jpeg,png,gif,wbmp,gd2.</span><span style="color: #008000;">*/</span>
  
<span style="color: #800080;">$font</span> = './t1.ttf'<span style="color: #000000;">;
</span><span style="color: #800080;">$black</span> = imagecolorallocate(<span style="color: #800080;">$dst</span>, 0, 0, 0<span style="color: #000000;">);
imagefttext(</span><span style="color: #800080;">$dst</span>, 20, 0, 10, 30, <span style="color: #800080;">$black</span>, <span style="color: #800080;">$font</span>, 'Hello world!'<span style="color: #000000;">);
</span><span style="color: #008000;">/*</span><span style="color: #008000;">imagefttext($img,$size,$angle,$x,$y,$color,$fontfile,$text)
$img由图像创建函数返回的图像资源
size要使用的水印的字体大小
angle(角度)文字的倾斜角度,如果是0度代表文字从左往右,如果是90度代表从上往下
x,y水印文字的第一个文字的起始位置
color是水印文字的颜色
fontfile,你希望使用truetype字体的路径
http://www.manongjc.com/article/1302.html </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">list</span>(<span style="color: #800080;">$dst_w</span>,<span style="color: #800080;">$dst_h</span>,<span style="color: #800080;">$dst_type</span>) = <span style="color: #008080;">getimagesize</span>(<span style="color: #800080;">$dst_path</span><span style="color: #000000;">);
</span><span style="color: #008000;">/*</span><span style="color: #008000;">list(mixed $varname[,mixed $......])--把数组中的值赋给一些变量
像array()一样,这不是真正的函数,而是语言结构,List()用一步操作给一组变量进行赋值</span><span style="color: #008000;">*/</span>
<span style="color: #008000;">/*</span><span style="color: #008000;">getimagesize()能获取到什么信息?
getimagesize函数会返回图像的所有信息,包括大小,类型等等</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">switch</span>(<span style="color: #800080;">$dst_type</span><span style="color: #000000;">){
  </span><span style="color: #0000ff;">case</span> 1:<span style="color: #008000;">//</span><span style="color: #008000;">GIF</span>
    <span style="color: #008080;">header</span>("content-type:image/gif"<span style="color: #000000;">);
    imagegif(</span><span style="color: #800080;">$dst</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
  </span><span style="color: #0000ff;">case</span> 2:<span style="color: #008000;">//</span><span style="color: #008000;">JPG</span>
    <span style="color: #008080;">header</span>("content-type:image/jpeg"<span style="color: #000000;">);
    imagejpeg(</span><span style="color: #800080;">$dst</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
  </span><span style="color: #0000ff;">case</span> 3:<span style="color: #008000;">//</span><span style="color: #008000;">PNG</span>
    <span style="color: #008080;">header</span>("content-type:image/png"<span style="color: #000000;">);
    imagepng(</span><span style="color: #800080;">$dst</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
  </span><span style="color: #0000ff;">default</span>:
    <span style="color: #0000ff;">break</span><span style="color: #000000;">;
  </span><span style="color: #008000;">/*</span><span style="color: #008000;">imagepng--以PNG格式将图像输出到浏览器或文件
  imagepng()将GD图像流(image)以png格式输出到标注输出(通常为浏览器),或者如果用filename给出了文件名则将其输出到文件</span><span style="color: #008000;">*/</span><span style="color: #000000;">
}
imagedestroy(</span><span style="color: #800080;">$dst</span><span style="color: #000000;">);
</span>?>

Original address: http://www.manongjc.com/article/593.html

Statement:
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