Home  >  Article  >  Backend Development  >  How to Add Watermarks to Images Using PHP?

How to Add Watermarks to Images Using PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-10-18 14:02:03724browse

How to Add Watermarks to Images Using PHP?

How to Watermark Images Using PHP

Problem:

Users need to upload images to a website and have a watermark (logo) added to them. The watermark should be placed prominently, such as in a corner where it will be visible.

Solution:

To add a watermark to images uploaded to a PHP website, you can use the following steps:

<code class="php">// Load the stamp (watermark) and the photo to be watermarked
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');

// Set margins for the stamp and get its dimensions
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Calculate the position of the stamp on the photo
$x = imagesx($im) - $sx - $marge_right;
$y = imagesy($im) - $sy - $marge_bottom;

// Copy the stamp onto the photo
imagecopy($im, $stamp, $x, $y, 0, 0, $sx, $sy);

// Output the watermarked image and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);</code>

This code snippet can be integrated into your PHP website to automatically add a watermark to uploaded images.

The above is the detailed content of How to Add Watermarks to Images Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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