使用 FPDF (PHP) 插入带有 alpha 通道的 PNG

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3058625/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:34:01  来源:igfitidea点击:

Insert PNG with alpha channel using FPDF (PHP)

phppngfpdf

提问by Raptor

In official documentation of FPDF, it said alpha channel is not supported for PNG.

在 FPDF 的官方文档中,它说 PNG 不支持 alpha 通道。

Is there any workaround?

有什么解决方法吗?

回答by qualbeen

If you need to put a transparent image on top of another: use PHPs build in functions to copy one image onto the other one. Then you will end up with a new picture, containing boths images. Save as a non-alpha png, and insert.

如果您需要将透明图像放在另一个图像之上:使用 PHP 内置函数将一个图像复制到另一个图像上。然后你会得到一张包含两个图像的新图片。另存为非 alpha png,然后插入。

There's an example hereof the code needed to combine the images.

这里有一个例子在这里的图像合并所需要的代码。

If you want text to be visible under you picture: insert the picture first, then write you text into the document.

如果您希望在图片下方显示文本:首先插入图片,然后在文档中写入文本。

回答by Cemen

Try this extension for FPDF:

试试这个 FPDF 扩展:

http://valentin.dasdeck.com/php/fpdf/fpdf_alpha/

http://valentin.dasdeck.com/php/fpdf/fpdf_alpha/

Short description from the page:

页面的简短描述:

This script allows to use images (PNGs or JPGs) with alpha-channels. The alpha-channel can be either supplied as separate 8-bit PNG ("mask"), or, for PNGs, also an internal alpha-channel can be used. For the latter, the GD 2.x extension is required.

Specifying a separate mask image has several advantages: - no GD required. - Better quality (full 8-bit alpha channel, whereas GD internally only supports 7-bit alpha channels) - much faster (extraction of embedded alpha-channel has to be done pixel-wise)

function Image(string file, float x, float y [, float w [, float h [, string type [, mixed link [, boolean isMask [, int maskImg]]]]]])

Same parameters as for original Image()-method, with 2 additional (optional) parameters: isMask: if specified and true, the image is used as mask for other images. In this case, the parameters x, y, w and h will be ignored and the mask image itself is not visible on the page. maskImg: number of image resource (as returned by previously called Image() with isMask parameter set to true) that will be used as mask for this image.

function ImagePngWithAlpha(string file, float x, float y [, float w [, float h [, mixed link]]])

Same parameters as for original Image()-method, but without a type parameter.

此脚本允许使用带有 alpha 通道的图像(PNG 或 JPG)。alpha 通道可以作为单独的 8 位 PNG(“掩码”)提供,或者对于 PNG,也可以使用内部 alpha 通道。对于后者,需要 GD 2.x 扩展。

指定单独的蒙版图像有几个优点: - 不需要 GD。- 更好的质量(完整的 8 位 alpha 通道,而 GD 内部仅支持 7 位 alpha 通道) - 更快(嵌入式 alpha 通道的提取必须按像素完成)

function Image(string file, float x, float y [, float w [, float h [, string type [, 混合链接 [, boolean isMask [, int maskImg]]]]]])

与原始 Image() 方法的参数相同,带有 2 个附加(可选)参数: isMask:如果指定且为真,则图像用作其他图像的掩码。在这种情况下,参数 x、y、w 和 h 将被忽略,并且掩码图像本身在页面上不可见。maskImg:将用作此图像的遮罩的图像资源数(由先前调用的 Image() 返回,isMask 参数设置为 true)。

function ImagePngWithAlpha(string file, float x, float y [, float w [, float h [, 混合链接]]])

与原始 Image() 方法的参数相同,但没有类型参数。

回答by lcainswebdeveloper

This worked for me thanks folks. I basically included the extension above (http://valentin.dasdeck.com/php/fpdf/fpdf_alpha/) and then extended classes as follows:

这对我有用,谢谢大家。我基本上包括了上面的扩展(http://valentin.dasdeck.com/php/fpdf/fpdf_alpha/),然后扩展类如下:

In fpdf_tpl.php require('PDF_ImageAlpha.php');

在 fpdf_tpl.php 中 require('PDF_ImageAlpha.php');

class FPDF_TPL extends PDF_ImageAlpha

In PDF_ImageAlpha.php:

在 PDF_ImageAlpha.php 中:

class PDF_ImageAlpha extends FPDF{
Inside of here I chaged the image() function to F_image() to avoid clashing (probably       should have used namespaces). With a quick search and replace you will see that this needs replacing 2 more times.
}

Then in my workhorse.php file I called the function F_image() instead of image() and this fixed my issue.

然后在我的 workhorse.php 文件中,我调用了函数 F_image() 而不是 image() ,这解决了我的问题。

Thanks!!!

谢谢!!!