首頁  >  文章  >  後端開發  >  如何使用PHP為圖片添加浮水印?

如何使用PHP為圖片添加浮水印?

Linda Hamilton
Linda Hamilton原創
2024-10-18 14:02:03792瀏覽

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.

以上是如何使用PHP為圖片添加浮水印?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn