How to add watermark to pictures in PHP
This article mainly introduces the use of PHP to add watermarks to images. Interested coders can refer to the source code of this article.
In order to prevent the hard-worked pictures from being stolen, many photos will be added with watermarks. You can directly add watermarks with picture tools and then upload them. However, the function of adding watermarks to pictures can be implemented in PHP. This article will add watermarks to the code. The programmers introduced two ways to add watermarks to images in PHP. Interested programmers can refer to the source code of this article.
Method 1: The simplest watermarking method in PHP
<?php $img = imagecreatefromjpeg($filename); $logo = imagecreatefromjpeg($filename); /*imagecraetefromjpeg-由文件或URL创建一个新图像 imagecreatefromjpeg(string $filename) 如果启用了fopen包装器,URL可以作为文件名*/ imagecopy($img,$logo,15,15,0,0,$width,$height); /*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是截取的图片的就是水印的长度和宽度*/ $url = 'http://www.stchat.cn/data/attachment/forum/201506/12/100759pidbdaydh8dy7iby.jpg'; $content = file_get_contents($url);//把url写入到content这个变量里面 /*file_get_contents--将整个文件读入到一个字符串*/ $filename = 'tmp.jpg'; file_put_contents($filename,$content); //把所有内容放到filename这个变量里面,第一个存放的是背景图片 /*file_put_contents(string $filename,mixed $data)将一个字符串写入一个文件 filename要被写入数据的文件名 data要写入的数据,类型可以是string,array或者是stream资源*/ $url = ''; file_put_contents('logo.png',file_get_contents($url)); //第二个是水印的图片 $img = imagecreatefromjpeg($filename); $logo = imagecreatefrompng('logo.png'); $size = getimagesize('logo.png'); /*getimagesize()获得图像大小*/ imagecopy($img,$logo,15,15,0,0,$size[0],$size[1]); header("centent-type:image/jpeg"); imagejpeg(img); ?>
Method 2: PHP to add pictures Add text watermark
<?php /*给图片加文字水印的方法*/ $dst_path = 'http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg'; $dst = imagecreatefromstring(file_get_contents($dst_path)); /*imagecreatefromstring()--从字符串中的图像流新建一个图像,返回一个图像标示符,其表达了从给定字符串得来的图像 图像格式将自动监测,只要php支持jpeg,png,gif,wbmp,gd2.*/ $font = './t1.ttf'; $black = imagecolorallocate($dst, 0, 0, 0); imagefttext($dst, 20, 0, 10, 30, $black, $font, 'Hello world!'); /*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 */ list($dst_w,$dst_h,$dst_type) = getimagesize($dst_path); /*list(mixed $varname[,mixed $......])--把数组中的值赋给一些变量 像array()一样,这不是真正的函数,而是语言结构,List()用一步操作给一组变量进行赋值*/ /*getimagesize()能获取到什么信息? getimagesize函数会返回图像的所有信息,包括大小,类型等等*/ switch($dst_type){ case 1://GIF header("content-type:image/gif"); imagegif($dst); break; case 2://JPG header("content-type:image/jpeg"); imagejpeg($dst); break; case 3://PNG header("content-type:image/png"); imagepng($dst); break; default: break; /*imagepng--以PNG格式将图像输出到浏览器或文件 imagepng()将GD图像流(image)以png格式输出到标注输出(通常为浏览器),或者如果用filename给出了文件名则将其输出到文件*/ } imagedestroy($dst); ?>
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
ThinkphpMethod to implement SMS verification registration function
PHP implementation Tool class that can accurately verify ID number
phpEncapsulated single file upload class
The above is the detailed content of How to add watermark to pictures in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

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)

Hot Topics











PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
