简体   繁体   中英

PHP / FPDF + imagepng

I've spent now days trying to solve a problem, but I totally failed ...

I do have a simple PNG file, with only 2 indexed colors and need to change the first index color dynamically ... so far no problem ... (just for example i change the color hardcoded to some green) and deliver the outcome ... calling this php file in a browser works perfectly:

<?php
$imgname = '../images/pdf/sidebar01.png';
$im = imagecreatefrompng($imgname);
imagecolorset($im,0, 0,150,0);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>

BUT, now I would need to use this image during creating a pdf file with FPDF:

$pdf->Image('http://server/phps/getColoredLogopart.php',50,1,15,'PNG');

and this line gives me the creeps ... I only get error messages from FPDF like

Fatal error: Uncaught Exception: FPDF error: Unsupported image type

I have tried so many different variations of headers, outputs, filetypes ... nothing has worked.

Is there anybody out there, who managed to solve this issue? ... I really have no further clue on how to get this working

Ok, new idea: When reading this:

http://www.fpdf.org/en/doc/image.htm

It says: Image(string file,...

A URL is a link, not a file. That's why there's an optional link parameter and the end.

So something like this could work:

$pdf->Image('/var/www/hosts/mydomain/httpdocs/img/getColoredLogopart.php',50,1,15,'PNG');

I'm also not sure 1-bit PNG's will be supported, try an 8-bit version.

hm .. well .. regarding the image .. actually it is a 8-bit PNG ... only thing is, that with the 256 possible colors, only the first 2 colours are set ... but I have tried that with many different variants of the png file itself, no success in any way.

$pdf->Image('http://server.com/images/pdf/sidebar01.png',50,1,15,'PNG'); // this works and will be displayed
$pdf->Image('http://server.com/phps/getColoredLogopart.php',50,1,15,'PNG'); // this line fails and I get the error message 

second, according to the FPDF api, you can provide the $pdf->image() either with a path to an image file OR via an URL ... I have also used that on other PDFs, this works ... the only difference at the code is, that I load the image data from a BLOB DB field and send it over via:

header("Content-type: image/jpeg");
print $imagedata;



$pdf->Image('http://server.com/phps/getImage.php?id='.$selectedAcId.'&type=acflr',150,100,50,0,'PNG');

and no problem whatsoever within fpdf, the image will be displayed without any errors.

BUT I need to change a color in the image before displaying it ... so i wrote this little php file to just load the image, change the color and sent the imagedata on to the requested site ...

<?php
$imgname = '../images/pdf/sidebar01.png';
$im = imagecreatefrompng($imgname);
imagecolorset($im,0, 0,150,0);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>

And in here something happens to the png file, that FPDF doesnt like and starts niggling ... " Fatal error: Uncaught Exception: FPDF error: Unsupported image type "

So maybe its the problem with the scripting in the php file that delivers the PNG imagedata ... as I said in the beginning, accessing the png directly in FPDF works, but the png imagedata from the php file seems to be something different at some point. (AND I am really unsure and new to image manipulation in php ... so maybe this is the point to change something???)

So, I hope there is someone out there who still has some suggestions ... thanks a lot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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