PHP development: How to implement anti-hotlinking and watermarking functions

WBOY
Release: 2023-09-20 16:50:02
Original
988 people have browsed it

PHP development: How to implement anti-hotlinking and watermarking functions

PHP development: How to implement anti-hotlinking and watermarking functions, specific code examples are required

Introduction:
In the Internet era, image anti-hotlinking and watermarking functions are very important Importantly, these functions can prevent other websites from stealing your image resources, and can also protect the copyright of your images. This article will introduce how to use PHP language to implement anti-leeching and watermarking functions, and provide specific code examples.

1. Implementation of the anti-hotlinking function:
The anti-hotlinking function refers to directly quoting your image resources on other websites. In order to prevent this from happening, we can make judgments on the server side and only allow specific request sources to access images.

The following is a code example using PHP to implement the anti-hotlinking function:

<?php
$referer = $_SERVER['HTTP_REFERER']; // 获取请求来源
$allowed_domains = array('www.example.com', 'subdomain.example.com'); // 定义允许的请求来源域名列表

$allowed = false;
foreach ($allowed_domains as $domain) {
    if (strpos($referer, $domain) !== false) {
        $allowed = true;
        break;
    }
}

if (!$allowed) {
    // 如果请求来源不在允许的域名列表中,则返回403 Forbidden状态码
    header('HTTP/1.1 403 Forbidden');
    exit;
}

// 允许请求来源访问图片
// 这里是输出图片的代码
?>
Copy after login

In the above code, we first obtain the source page of the request through $_SERVER['HTTP_REFERER']. Then, we define the list of allowed domain names $allowed_domains, which can be modified according to the actual situation. Next, we use a loop to check if the request source is in the list of allowed domains, and if so, set the $allowed variable to true. Finally, if $allowed is false, a 403 Forbidden status code is returned, indicating that access is denied.

2. Implementation of the watermark function:
The watermark function refers to adding some identification information to the picture to protect the copyright and source of the picture. We can use PHP's GD library to implement the image watermark function.

The following is a code example using PHP to implement the image watermark function:

<?php
$source_image = 'path/to/source/image.jpg'; // 原始图片路径
$watermark_image = 'path/to/watermark/image.png'; // 水印图片路径
$save_path = 'path/to/save/image.jpg'; // 保存图片路径

$source = imagecreatefromjpeg($source_image);
$watermark = imagecreatefrompng($watermark_image);

$source_width = imagesx($source);
$source_height = imagesy($source);

$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);

$offset_x = ($source_width - $watermark_width) / 2; // 水印位置居中
$offset_y = ($source_height - $watermark_height) / 2;

imagecopy($source, $watermark, $offset_x, $offset_y, 0, 0, $watermark_width, $watermark_height); // 合并图片

imagejpeg($source, $save_path); // 保存图片

imagedestroy($source);
imagedestroy($watermark);
?>
Copy after login

In the above code, we first use the imagecreatefromjpeg() and imagecreatefrompng() functions to create the resources of the original image and the watermark image respectively. . Then, we obtain the width and height information of the original image and the watermark image through the imagesx() and imagesy() functions in order to adjust the watermark position.

Next, we calculate the position of the watermark. Here we place the watermark in the center of the original image. Then, use the imagecopy() function to merge the watermark image onto the original image.

Finally, we use the imagejpeg() function to save the merged image to the specified path. In order to save server resources, be sure to call the imagedestroy() function to destroy the resources of the original image and watermark image after saving the image.

Conclusion:
This article introduces how to use PHP language to implement anti-leeching and watermarking functions, and provides specific code examples. By implementing these functions, you can protect your image resources from being stolen and protect your image copyright. Hope this article is helpful to you.

The above is the detailed content of PHP development: How to implement anti-hotlinking and watermarking functions. For more information, please follow other related articles on the PHP Chinese website!

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