Home > Daily Programming > PHP Knowledge > How to add watermark to pictures in php? (Picture + video tutorial)

How to add watermark to pictures in php? (Picture + video tutorial)

藏色散人
Release: 2020-02-22 17:49:55
Original
7613 people have browsed it

In the process of website development, sometimes the webmaster is required to implement the PHP function of adding watermarks to images in order to declare that it is a resource of this website. Then PHP adding text watermarks to images is also a common method used by most websites to prevent theft.

This article will give you a detailed introduction to the specific implementation method of PHP adding text watermark, which can be used as a reference for friends in need.

Below we will explain it in detail through specific code examples.

First we need to obtain an image information:

<?php
$img = "timg.jpg";
// 获取图片信息
$info = getimagesize($img);
echo "<pre class="brush:php;toolbar:false">";
var_dump($info);
Copy after login

Here we use the getimagesize function to obtain the complete information of the timg.jpg image, as shown below:

How to add watermark to pictures in php? (Picture + video tutorial)

As shown in the figure, the fields here respectively represent the width, height and suffix type of the image.

PHP adds watermark to imagesThe complete operation code example is as follows:

<?php
$img = "timg.jpg";
// 获取图片信息
$info = getimagesize($img);
// 通过图片的编号来获取图片类型
$type = image_type_to_extension($info[&#39;2&#39;], false);
// 在内容中创一个和我们这个图片一样的图片
$ext = "imagecreatefrom{$type}";
// 把图片复制到内存中
$image = $ext($img);
$content = "PHP中文网";
$color = imagecolorallocatealpha($image, 0, 0, 0, 0);
imagettftext($image, 50, 0, 20, 100, $color, &#39;./123.ttf&#39;, $content);
header("content-type:" . $info[&#39;mime&#39;]);
$func = "image{$type}";
$func($image);
Copy after login

In the above code, the image_type_to_extension() function is used to get the file suffix of the image type. We first get the image type through the image number, and then use imagecreatefrom to create and copy an image in memory that is the same as the image in our example.

Then continue to set the content, font type, color, size and position of the text watermark.

There are several important functions involved here:

imagecolorallocatealpha function is used to set image transparency.

imagettftext function means using a certain type of font you want to write the specified text into the image.

Finally accessed through the browser, the effect of the watermark image presented is as follows:

How to add watermark to pictures in php? (Picture + video tutorial)

As shown in the figure, we have successfully used itPHP adds watermark to pictures. You can set the font type, size, angle or display position according to your own preferences.

This article is about the specific operation method of PHP to add watermark to pictures. It is simple and easy to understand. I hope it will be helpful to everyone!

For more PHP related knowledge, you can pay attention to PHP video tutorial, everyone is welcome to learn and reference!

The above is the detailed content of How to add watermark to pictures in php? (Picture + video tutorial). For more information, please follow other related articles on the PHP Chinese website!

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