Insert PNG with alpha channel using FPDF (PHP)
In official 开发者_如何学运维documentation of FPDF, it said alpha channel is not supported for PNG.
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. Then you will end up with a new picture, containing boths images. Save as a non-alpha png, and insert.
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:
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.
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:
In fpdf_tpl.php require('PDF_ImageAlpha.php');
class FPDF_TPL extends PDF_ImageAlpha
In 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.
Thanks!!!
精彩评论