Paste PNG with alpha using FPDF (PHP)

In official FPDF documentation, he said that the alpha channel is not supported for PNG.

Is there a workaround?

+4
source share
3 answers

If you need to put a transparent image on top of another: use PHP, create functions to copy one image to another. Then you will get a new image containing both images. Save as a non-alpha file and paste.

Here is an example of the code needed to combine the images.

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

+4
source

Try this extension for FPDF:

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

Short description from the page:

This script allows you to use images (PNG) or JPG) with alpha channels. the alpha channel can be delivered as a separate 8-bit PNG ("mask") or, for PNG, as well as internal alpha channel. For the latter, an extension of GD 2.x is required.

Defining a separate mask image has several advantages: - GD is not required. - Better quality (full 8-bit alpha channel, while GD only supports 7-bit alpha channels internally) - much faster (you need to extract the embedded alpha channel pixel-wise)

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

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

ImagePngWithAlpha function (line file, float x, float y [, float w [, float h [, mixed reference]]])

The same parameters as for the original Image () - a method, but without the type Parameter.

+5
source

It helped me thanks to people. I basically included the extension above ( http://valentin.dasdeck.com/php/fpdf/fpdf_alpha/ ) and then the 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 problem.

Thanks!!!

+1
source

Source: https://habr.com/ru/post/1313041/


All Articles