Alpha-Channels / Masks

Informations

Author: Valentin Schmidt
License: Freeware
Version: 1.4
Requirements: FPDF 1.6

Description

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.

Source


Warning: show_source(fpdf_alpha.php): failed to open stream: No such file or directory in /opt/lampp/htdocs/cfapp_pdf/fpdf181/fpdf_alpha_v1.4/index.php on line 37

Warning: show_source(): Failed opening 'fpdf_alpha.php' for highlighting in /opt/lampp/htdocs/cfapp_pdf/fpdf181/fpdf_alpha_v1.4/index.php on line 37

Example

Here's an example that demonstrates both specifying alpha channels as separate "mask" images and using PNG's with internal alpha channels.

<?php

require('fpdf_alpha.php');

$pdf=new PDF_ImageAlpha();

$pdf->AddPage();

$pdf->SetFont('Arial','',16);
$pdf->MultiCell(0,8str_repeat('Hello World! '180));

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// A) provide image + separate 8-bit mask (best quality!)

//first embed mask image (w, h, x and y will be ignored, the image will be scaled to the target image's size)
$maskImg $pdf->Image('alpha.png'0,0,0,0''''true); 

//embed image, masked with previously embedded mask
$pdf->Image('img.png',5,10,100,0,'',''false$maskImg);

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// B) same as A), but using a JPG file (+ mask defined as PNG)

// Notice: although B) uses the same mask as A), we have to embed a copy of the original mask, 
// in other words: each mask can only be used once (TODO: this propably could be avoided)

//first embed mask image (w, h, x and y will be ignored, the image will be scaled to the target image's size)
$maskImg2 $pdf->Image('alpha2.png'0,0,0,0''''true);

//embed image, masked with previously embedded mask
$pdf->Image('img.jpg',105,10,100,0,'',''false$maskImg2);

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// C) use alpha channel from PNG (alpha channel converted to 7-bit by GD, lower quality)
$pdf->ImagePngWithAlpha('image_with_alpha.png',55,100,100,0);

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// D) same as C), but using Image()-method that recognizes the alpha channel
$pdf->Image('image_with_alpha.png',55,190,100,0,'PNG');

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// send PDF to browser
$pdf->Output();

?>

View the result here.

Download

ZIP