首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用php为图片添加‘水印’

使用php为图片添加‘水印’
EN

Stack Overflow用户
提问于 2010-02-10 15:39:04
回答 6查看 87.2K关注 0票数 48

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

我需要添加我的标志(水印)的图像,一旦他们被上传。

我如何做到这一点呢?

重要的是,水印是在一个角落,它将是可见的,例如,我看到的网站上生成一个动态水印,并将标记的地方,主图像的背景是“相同的颜色”,所以水印突出,如果你明白我的意思。

有没有人对此有很好的教程或文章?或者知道php中的任何函数,我需要找到水印的位置?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2010-02-10 15:42:00

PHP手册中的good example

代码语言:javascript
运行
复制
// 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);
票数 57
EN

Stack Overflow用户

发布于 2014-11-15 04:08:39

使用此函数

水印图片类型必须为"png“

代码语言:javascript
运行
复制
 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');
票数 21
EN

Stack Overflow用户

发布于 2012-10-12 00:13:06

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

Add watermark to images through htaccess

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

希望这对别人有帮助,就像对我有帮助一样。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2235152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档