简体   繁体   English

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

[英]Insert PNG with alpha channel using FPDF (PHP)

In official documentation of FPDF, it said alpha channel is not supported for PNG. 在FPDF的官方文档中,它说PNG不支持alpha通道。

Is there any workaround? 有没有解决方法?

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. 如果您需要将透明图像放在另一个上面:使用PHP内置函数将一个图像复制到另一个图像上。 Then you will end up with a new picture, containing boths images. 然后你将得到一张包含两张图片的新图片。 Save as a non-alpha png, and insert. 保存为非alpha png,然后插入。

There's an example here of 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. 如果要在图片下方显示文本:首先插入图片,然后将文本写入文档。

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. 此脚本允许使用带alpha通道的图像(PNG或JPG)。 The alpha-channel can be either supplied as separate 8-bit PNG ("mask"), or, for PNGs, also an internal alpha-channel can be used. alpha通道可以作为单独的8位PNG(“掩码”)提供,或者对于PNG,也可以使用内部alpha通道。 For the latter, the GD 2.x extension is required. 对于后者,需要GD 2.x扩展名。

Specifying a separate mask image has several advantages: - no GD required. 指定单独的蒙版图像有几个优点: - 不需要GD。 - 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) - 更好的质量(完整的8位alpha通道,而GD内部仅支持7位alpha通道) - 速度更快(嵌入式alpha通道的提取必须以像素方式完成)

function Image(string file, float x, float y [, float w [, float h [, string type [, mixed link [, boolean isMask [, int maskImg]]]]]]) function Image(字符串文件,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. 与原始Image() - 方法相同的参数,带有2个附加(可选)参数:isMask:如果指定且为true,则图像用作其他图像的掩码。 In this case, the parameters x, y, w and h will be ignored and the mask image itself is not visible on the page. 在这种情况下,将忽略参数x,y,w和h,并且屏幕图像本身在页面上不可见。 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. maskImg:图像资源的数量(由先前调用的Image()返回,isMask参数设置为true),将用作此图像的掩码。

function ImagePngWithAlpha(string file, float x, float y [, float w [, float h [, mixed link]]]) 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. 与原始Image() - 方法的参数相同,但没有类型参数。

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中需要('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!!! 谢谢!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM