49

我有一个网站,用户可以上传图片...

上传后,我需要将我的徽标(水印)添加到图像中。

我该怎么做?

重要的是,水印位于可见的角落,例如,我见过会即时生成水印的网站,并将标记放在主图像的背景为“相同颜色”的任何位置,因此如果您知道我的意思,水印就会突出。

有人有关于这个的好教程或文章吗?或者知道我需要找到水印位置的php中的任何函数?

4

7 回答 7

57

PHP手册中的一个很好的例子:

// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
于 2010-02-10T07:41:59.720 回答
22

使用此功能
水印图片的类型必须为“png”

 function watermark_image($target, $wtrmrk_file, $newcopy) {
    $watermark = imagecreatefrompng($wtrmrk_file);
    imagealphablending($watermark, false);
    imagesavealpha($watermark, true);
    $img = imagecreatefromjpeg($target);
    $img_w = imagesx($img);
    $img_h = imagesy($img);
    $wtrmrk_w = imagesx($watermark);
    $wtrmrk_h = imagesy($watermark);
    $dst_x = ($img_w / 2) - ($wtrmrk_w / 2); // For centering the watermark on any image
    $dst_y = ($img_h / 2) - ($wtrmrk_h / 2); // For centering the watermark on any image
    imagecopy($img, $watermark, $dst_x, $dst_y, 0, 0, $wtrmrk_w, $wtrmrk_h);
    imagejpeg($img, $newcopy, 100);
    imagedestroy($img);
    imagedestroy($watermark);
}

watermark_image('image_name.jpg','watermark.png', 'new_image_name.jpg');
于 2014-11-14T20:08:38.900 回答
12

水印图像的良好示例并位于中心

<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stampimg.png');
$im = imagecreatefrompng('mainimage.png');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

$imgx = imagesx($im);
$imgy = imagesy($im);
$centerX=round($imgx/2);
$centerY=round($imgy/2);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, $centerX, $centerY, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
于 2012-12-20T15:19:44.167 回答
3

我找到了一个更好的解决方案,它通过 .htaccess 动态添加水印,您可以在此处找到教程:

通过htaccess给图片添加水印

上传自定义 .htaccess 文件、watermark.php scrypt 和您的 watermark.png 图像后,该文件夹及其子文件夹中的所有图像都会显示水印,但是您仍将原始文件保留在服务器中。

希望对我有帮助的人有所帮助。

于 2012-10-11T16:13:06.220 回答
2

这可以使用GDImageMagick等图像处理库来完成。这是一个教程,解释了使用 GD 的方法:

http://articles.sitepoint.com/article/watermark-images-php

于 2010-02-10T07:44:30.880 回答
2

ImageMagick可以很好地解决这个问题。我以前做过。不过,整个业务有点痛苦。特别是如果您想要花哨的混合模式等。

于 2010-02-10T07:51:56.477 回答
2
// Load the stamp and the photo to apply the watermark to

$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpg');
$save_watermark_photo_address = 'watermark_photo.jpg';

// Set the margins for the stamp and get the height/width of the stamp image

$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 

imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
// header('Content-type: image/png');

imagejpeg($im, $save_watermark_photo_address, 80); 
imagedestroy($im);
于 2014-01-24T06:28:42.447 回答