Home php教程 php手册 简单的php图片上传自动给图片加水印

简单的php图片上传自动给图片加水印

May 25, 2016 pm 04:46 PM
thumbnail

给自己网站上的图片添加水印,在图片上留下属于自己的信息,这有两个好处:一是宣传自己的品牌,二是就算带图片的文章被转载了,也会自动的留下了版权。

本文主要实现了php图片上传自动给图片加水印,在下篇中我会给出用php添加文字水印的代码。

php给图片加水印其实很简单,没有想象的那么神奇,就是利用了php里面的一个GD库。说起GD库,其实就是php里面一个专门用于处理图片有关的扩展,比如我们熟悉的验证码、生成缩略图,包括今天所说的php给图片加水印都是要有GD库的支持。

提示:新安装的php环境,可能需要开启GD库,开启方法:打开php.ini,去掉extension=php_gd2.dll前面的“;”然后重启apache/iis即可。

实现并不复杂,核心代码还不到20行,主要是掌握几个函数就行了。

实现源码:

<?php
$ori_img = "images/13801220684507.jpg"; //原图
$water_img = "logo.png"; //水印
$new_img = "images/daixiaorui.jpg"; //生成水印后的图片
$original = getimagesize($ori_img); //得到图片的信息,可以print_r($original)发现它就是一个数组
$watermark = getimagesize($water_img);
$s_original = imgCreateFrom($ori_img, $original[2]); //$original[2]是图片类型,其中1表示gif、2表示jpg、3表示png
$s_watermark = imgCreateFrom($water_img, $watermark[2]);
$posX = $original[0] - $watermark[0] - 6; //X坐标(右下角)
$poxY = $original[1] - $watermark[1] - 3; //Y坐标(右下角)
//打水印
imagecopy($s_original, $s_watermark, $posX, $poxY, 0, 0, $watermark[0], $watermark[1]);
//header("Content-type:image/jpeg");
//imagejpeg($s_original); //向浏览器输出图片
$loop = imagejpeg($s_original, $new_img); //生成新的图片(jpg格式),如果用imagepng可以生成png格式
if ($loop) {
    echo "水印添加成功!";
}
//根据文件类型 创建一个新图象
function imgCreateFrom($img_src, $val) {
    switch ($val) {
        case 1:
            $img = imagecreatefromgif($img_src);
            break;
        case 2:
            $img = imagecreatefromjpeg($img_src);
            break;
        case 3:
            $img = imagecreatefrompng($img_src);
            break;
    }
    return $img;
}
?>
Copy after login

给图片添加水印除了用基础的GD库实现,其实还可以ImageMagick这个工具,它的功能更强大,不过一般没多大需求,就用上面这个代码了。

永久地址:

转载随意~请带上教程地址吧^^

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

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to disable taskbar thumbnail preview in Win11? Turn off the taskbar icon display thumbnail technique by moving the mouse How to disable taskbar thumbnail preview in Win11? Turn off the taskbar icon display thumbnail technique by moving the mouse Feb 29, 2024 pm 03:20 PM

This article will introduce how to turn off the thumbnail function displayed when the mouse moves the taskbar icon in Win11 system. This feature is turned on by default and displays a thumbnail of the application's current window when the user hovers the mouse pointer over an application icon on the taskbar. However, some users may find this feature less useful or disruptive to their experience and want to turn it off. Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another drawback is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. but

How to customize folder thumbnails in Windows 11 22H2 How to customize folder thumbnails in Windows 11 22H2 Apr 30, 2023 pm 04:52 PM

Windows 1122H2 is the first feature update for Windows 11 and should bring a ton of new features and much-needed improvements. One of the improvements is the ability to preview folder thumbnails of files within a folder. If you don't like the look of folder thumbnails in Windows 11, here's how you can change it. A set of custom icons for folder thumbnails in Windows 11 (courtesy of Reddit's LEXX911) that lets you zoom in on previews and change folder icon styles. You still have to deal with individual file previews (in Windows 7, for example, folder thumbnails can display multiple images at the same time), but you can make it larger and more convenient. important hint:

How to display thumbnails in Vscode_How to display thumbnails in Vscode How to display thumbnails in Vscode_How to display thumbnails in Vscode Apr 02, 2024 pm 02:43 PM

1. First enter Visual Studio Code and click [File] in the upper left corner. 2. Then click [Preferences]. 3. Click the [Settings] item. 4. Then click [Text Editor-Thumbnail]. 5. Finally, in the thumbnail item, turn on [Control whether to display thumbnails].

Detailed explanation of the steps to generate thumbnails in PHP Detailed explanation of the steps to generate thumbnails in PHP Sep 13, 2023 am 08:40 AM

Detailed explanation of the steps to generate thumbnails with PHP, specific code examples are required. In today's era of rapid Internet development, pictures are an indispensable part of web pages, but high-resolution pictures will not only take up a lot of bandwidth, but also affect the user's web page loading. speed. Therefore, we often need to generate thumbnails from original images to reduce image size and file size. As a popular server-side scripting language, PHP provides rich image processing functions that can be used to generate thumbnails. The following will introduce in detail the steps to generate thumbnails in PHP.

Automatic thumbnail generation using JavaScript Automatic thumbnail generation using JavaScript Jun 16, 2023 pm 12:51 PM

With the development of the Internet, pictures have become an indispensable part of web pages. But as the number of images increases, the loading speed of images has become a very important issue. In order to solve this problem, many websites use thumbnails to display images, but in order to generate thumbnails, we need to use professional image processing tools, which is a very troublesome thing for some non-professionals. Then, using JavaScript to achieve automatic thumbnail generation becomes a good choice. How to use JavaS

How to display thumbnails on USB drive How to display thumbnails on USB drive Feb 12, 2024 pm 08:36 PM

I often encounter customers who say that a few minor computer problems are very troublesome at critical times. In summary, there are the following. The reason why the file cannot be copied to the USB flash drive may be that the partition format of the USB flash drive is FAT32 instead of NTFS. You can try changing the partition format of the USB flash drive to NTFS so that you can copy large files. Second, the pictures are not displayed as thumbnails, so you have to click on them one by one to find them. The third type is that the page suddenly becomes larger or smaller. If you don’t know these three situations, they can be solved with a few simple operations. 1. Change the U disk partition to NTFS. The default partition format of a newly purchased U disk is generally FAT32. Under normal circumstances, there is no problem in using it. However, when you need to copy files larger than 4G, a prompt that cannot be copied will appear, which brings some trouble to use. for

PowerToys 0.53.1 adds G-code support for thumbnails and preview panes in File Explorer PowerToys 0.53.1 adds G-code support for thumbnails and preview panes in File Explorer Apr 13, 2023 pm 11:19 PM

Just over a month removed from the last 0.51.1 release, Microsoft has gone ahead and released 0.53.1 today. Microsoft notes that the v0.53 release cycle was designed as a maintenance sprint, but with some "amazing community support" it ended up being a huge success! Microsoft says they're still working on improving the installer, but it should be significantly improved with this release. The three main things to check out are G-code support in the file browser preview pane and thumbnails, the new network, and the search plugin for running from PowerToys

How to insert a table in a Powerpoint presentation How to insert a table in a Powerpoint presentation Apr 13, 2023 pm 10:37 PM

Tables in PowerPoint presentations help display data in a structured format when you present. The numbers shown in the table have a better impact on the audience. Microsoft Powerpoint has several ways to add tables to your presentations. You can choose to add a new table or copy from an existing table in another application such as Excel or Word. Method 1: Insert a table by selecting rows and columns on the grid Step 1: On the PowerPoint thumbnail, right-click Master Slide and select New Slide. Step 2: Go to Insert and click Table. Step 3: From the drop-down menus, select the total number of rows and columns required for the table. Notice

See all articles