php 使用php为图像添加“水印”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2235152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add 'Watermark' to images with php
提问by pesar
I have a website where users may upload images...
我有一个网站,用户可以在其中上传图片...
I need to add my logo (watermark) to the images once they are uploaded.
上传图像后,我需要将我的徽标(水印)添加到图像中。
How can I do so?
我怎么能这样做?
And it is important that the watermark is in a corner where it will be visible, for example I have seen websites which generates a watermark on the fly, and puts the mark wherever the background of the main image is "the same color" so the watermark sticks out if you know what I mean.
并且重要的是水印位于它可见的角落,例如我看到网站会即时生成水印,并将标记放在主图像背景为“相同颜色”的任何位置,因此如果你知道我的意思,水印就会突出。
Anybody have a good tutorial or article about this? Or know of any function in php which I would need to find the position of the watermark?
有人有关于这个的好教程或文章吗?或者知道我需要在 php 中找到水印位置的任何函数?
回答by XUE Can
A good examplein the PHP manual:
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);
回答by iraqi_love4ever
use this function
the type of watermark image must be "png"
使用此功能
水印图片的类型必须是“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');
回答by Sailee Shahir
Good Example of watermark image and positioned at the center
水印图像的好例子并位于中心
<?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);
?>
回答by Alberto S.
I found a much better solution which add a watermark dinamically through .htaccess, you can find the tutorial here:
我找到了一个更好的解决方案,它通过 .htaccess 动态添加水印,你可以在这里找到教程:
Add watermark to images through htaccess
After uploading the custom .htaccess file, the watermark.php scrypt, and your watermark.png image, all the images in the folder and its subfolders will show the watermark, however, you will still keep the original file in the server.
上传自定义 .htaccess 文件、watermark.php scrypt 和您的 watermark.png 图片后,文件夹及其子文件夹中的所有图片都会显示水印,但您仍将原始文件保留在服务器中。
Hope that helps someone the same that it helped to me.
希望能帮助到对我有帮助的人。
回答by Jimmy Cuadra
This can be done using an image manipulation library such as GDor ImageMagick. Here is a tutorial that explains a way to do it using GD:
这可以使用图像处理库(例如GD或ImageMagick )来完成。这是一个教程,解释了使用 GD 执行此操作的方法:
回答by Gabriel Hurley
ImageMagickworks well for that. I've done it before. The whole business is a bit of a pain, though. Especially if you want fancy blending modes and the like.
ImageMagick在这方面效果很好。我以前做过。不过,整个业务有点痛苦。特别是如果您想要花哨的混合模式等。
回答by Baz Love
// 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);

