PHP shares two methods of adding watermark to images
Oct 11, 2016 pm 02:03 PMThis 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創(chuàng)建一個(gè)新圖像 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)坐標(biāo); $src_x,$src_y是截取水印的圖片的開(kāi)始坐標(biāo) $width,$height是截取的圖片的就是水印的長(zhǎng)度和寬度</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寫(xiě)入到content這個(gè)變量里面</span><span style="color: #008000;"> /*</span><span style="color: #008000;">file_get_contents--將整個(gè)文件讀入到一個(gè)字符串</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;">把所有內(nèi)容放到filename這個(gè)變量里面,第一個(gè)存放的是背景圖片</span><span style="color: #008000;"> /*</span><span style="color: #008000;">file_put_contents(string $filename,mixed $data)將一個(gè)字符串寫(xiě)入一個(gè)文件 filename要被寫(xiě)入數(shù)據(jù)的文件名 data要寫(xiě)入的數(shù)據(jù),類(lèi)型可以是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;">第二個(gè)是水印的圖片</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()--從字符串中的圖像流新建一個(gè)圖像,返回一個(gè)圖像標(biāo)示符,其表達(dá)了從給定字符串得來(lái)的圖像 圖像格式將自動(dòng)監(jiān)測(cè),只要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由圖像創(chuàng)建函數(shù)返回的圖像資源 size要使用的水印的字體大小 angle(角度)文字的傾斜角度,如果是0度代表文字從左往右,如果是90度代表從上往下 x,y水印文字的第一個(gè)文字的起始位置 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 $......])--把數(shù)組中的值賦給一些變量 像array()一樣,這不是真正的函數(shù),而是語(yǔ)言結(jié)構(gòu),List()用一步操作給一組變量進(jìn)行賦值</span><span style="color: #008000;">*/</span> <span style="color: #008000;">/*</span><span style="color: #008000;">getimagesize()能獲取到什么信息? getimagesize函數(shù)會(huì)返回圖像的所有信息,包括大小,類(lèi)型等等</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格式輸出到標(biāo)注輸出(通常為瀏覽器),或者如果用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 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

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Grass Wonder Build Guide | Uma Musume Pretty Derby
4 weeks ago
By Jack chen
Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them
3 weeks ago
By DDD
Uma Musume Pretty Derby Banner Schedule (July 2025)
4 weeks ago
By Jack chen
Windows Security is blank or not showing options
4 weeks ago
By 下次還敢
RimWorld Odyssey Temperature Guide for Ships and Gravtech
3 weeks ago
By Jack chen

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)