user3604203
user3604203

Reputation:

imagecreatefrompng error for QR Code produced by PHP page when using TCPDF

This is weird, the first line of the following code (TCPDF - PDF creating code) works but sometimes does NOT work for no apparent reason. I hate intermittent errors. When it fails the error message is the same as line 3 shown below.

The second line works fine. I believe this is the better way to code it, not sure why though.

The third line does NOT work but I think would be more stable if it did.

The error says:

Warning: imagecreatefrompng(/var/www/vhosts/path-to-file/tcpdf/../../path/includes/php/phpqrcode.php?text=textforqrcode): failed to open stream: No such file or directory.

The ../../ is used to get to the correct directory, I know this is correct as it's the same as for line 2, includes and images are on the same level in the directory structure.

The thing is the path is correct. I guess this doesn't work because it's a file produced by a php file but this does work on line 1 where it's produced as a kind of http object.

Here's the code I've tried so far:

$pdf->Image('http://' . $serverHost . '/includes/php/phpqrcode.php?text='.$random, 15, 77, 30, 30, 'PNG', '', '', true, 150, '', false, false, 0, false, false, false);

$pdf->Image(__DIR__ . '/../../path/images/template/refunded.gif', 15, 77, 30, 16, 'GIF', '', '', true, 150, '', false, false, 0, false, false, false);

$pdf->Image(__DIR__ . '/../../path/includes/php/phpqrcode.php?text='.$random, 12, 250, 20, 20, 'PNG', '', '', true, 150, '', false, false, 0, false, false, false);

I've also tried this but it doesn't like it, similar error message:

$pdf->Image( K_PATH_IMAGES . '../../../../path/includes/php/phpqrcode.php?text='.$random, 12, 250, 20, 20, 'PNG', '', '', true, 150, '', false, false, 0, false, false, false);

(../../../../ because the relative path changed).

I get the feeling I'm banging my head against a wall...

Upvotes: 0

Views: 1111

Answers (2)

user3604203
user3604203

Reputation:

As suggested in the comments, I used copy() to save the image onto the server, used the image from the server in the PDF then deleted the image at the end of the script. Simples! Thank you @onik

Upvotes: 0

aleksikallio
aleksikallio

Reputation: 1932

You can't refer to a local file with query string parameters. If phpqrcode.php expects the text param to come from a GET param, you'll have to load it through the server. If you refer to the file directly, it will not run the PHP code but give you the PHP file instead directly. And in this case, it can't even find the file because it's looking for a file called phpqrcode.php?text=asd, not phpqrcode.php.

As far as I can see, the first way is the correct one, but if you're having problems with it then we need to know what library are you using to generate the QR code.

Upvotes: 1

Related Questions