用GD PHP水印PNG图像时的局部黑色背景 [英] Partial black background when watermarking PNG image with GD PHP

查看:770
本文介绍了用GD PHP水印PNG图像时的局部黑色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拼凑了一个PHP类,使用PHP的GD函数执行各种与图像相关的函数。

I have pieced together a PHP class to perform various image related functions using GD functions of PHP.

它适用于所有图像类型。旋转,翻转,调整大小,裁剪以及较小程度的水印。

It works great for all image types. Rotate, flip, resize, crop and to a lesser extent, watermark.

除了后者之外的所有工作都完美无缺。例如,经过一些更改后,旋转的PNG图像保持其透明度,而在它们丢失之前,背景变为黑色。看来常见的问题。但现在一切都在工作。

All but the latter work perfectly. For example after a few changes, rotated PNG images retained their transparency whereas before they were losing that and the background turning black. Common problem, it appears. But all working now.

我仍然陷入困境的是用另一个PNG图像为PNG图像添加水印。它似乎与JPG和其他图像一起使用。这是代码(简化):

Where I'm still getting stuck is watermarking a PNG image with another PNG image. It appears to work fine with JPG and other images. This is the code (simplified):

public function writeWatermarkSimple()
{
    $watermarkFile = 'watermark.png';
    $watermarkImage = imagecreatefrompng($watermarkFile);

    imagealphablending($watermarkImage, false);
    imagesavealpha($watermarkImage, true);

    $imageFile = 'image.png';
    $baseImage = imagecreatefrompng($imageFile);

    imagealphablending($baseImage, false);
    imagesavealpha($baseImage, true);

    $marginH = imagesx($baseImage) - imagesx($watermarkImage);
    $marginV = imagesy($baseImage) - imagesy($watermarkImage);

    $cut = imagecreatetruecolor(imagesx($watermarkImage), imagesy($watermarkImage));
    imagecopy($cut, $baseImage, 0, 0, $marginH, $marginV, imagesx($watermarkImage), imagesy($watermarkImage));
    imagecopy($cut, $watermarkImage, 0, 0, 0, 0, imagesx($watermarkImage), imagesy($watermarkImage));

    imagecopymerge($baseImage, $cut, $marginH, $marginV, 0, 0, imagesx($watermarkImage), imagesy($watermarkImage), 80);

    if (!imagepng($baseImage, 'watermarked_image.png'))
    {
        return false;
    }

    return true;
}

这已与人们基于a提供的各种指南和建议拼凑在一起类似的问题。再次,完美地使用JPG图像和PNG水印,但不是PNG& PNG。

This has been pieced together with various guides and advice people have given based on a similar issue. Again, working perfectly with JPG images and PNG watermarks, but not PNG & PNG.

一些示例图片:

http://i.imgur.com/hHRWinj.png - 这是我正在使用的水印。
http://i.imgur.com/6sy8Ncs.png - 这是我正在应用水印。
http://i.imgur.com/ghovYLm.png - 这是最终结果。

http://i.imgur.com/hHRWinj.png - This is the watermark I'm using. http://i.imgur.com/6sy8Ncs.png - This is the image I'm applying the watermark to. http://i.imgur.com/ghovYLm.png - This is the end result.

我觉得有趣的是,水印的任何部分覆盖在图像的非透明部分上工作正常。其余部分都有黑色背景。

The bit I find interesting is that any part of the watermark that is overlaid on a non-transparent portion of the image is working fine. Just the rest of it has the black background.

这让我相信我很亲密,我希望你这些优秀人才的专业知识可能会让我走向解决方案。

This leads me to believe I'm close, and I hope that the expertise of you fine people may lead me to the solution.

非常感谢您阅读。

推荐答案

所以,我并没有放弃使用GD来找到正确的答案。但是,我非常高兴地发现使用ImageMagick可以使用更少的代码来获得多达30行的代码:

So, I'm not giving up on finding the correct answer to do this using GD. However, I was overjoyed to find that what needed up to 30 lines of code with GD can be achieved using much less with ImageMagick:

    $image = new Imagick();
    $image->readimage($this->_image);

    $watermark = new Imagick();
    $watermark->readimage($this->_watermark->_getImage());
    $watermark->evaluateImage(Imagick::EVALUATE_DIVIDE, 2, Imagick::CHANNEL_ALPHA);

    $image->compositeImage($watermark, imagick::COMPOSITE_OVER, $marginH, $marginV);

所以这是之前(使用GD):
http://i.imgur.com/AlS0TcO.png

So this is before (with GD): http://i.imgur.com/AlS0TcO.png

之后(使用ImageMagick和上面的代码):
http://i.imgur.com/zBxlC3R .png

And after (with ImageMagick and the code above): http://i.imgur.com/zBxlC3R.png

如果有人的答案纯粹是GD,那么我会非常感激。

If anyone has an answer that is purely GD then I'd be immensely grateful.

这篇关于用GD PHP水印PNG图像时的局部黑色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆