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

Linda Hamilton
發布: 2024-10-18 14:02:03
原創
723 人瀏覽過

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中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!