Joseph
Joseph

Reputation: 1744

Parse php generated png in html

I got a class that generates a png showing the result on a single page

header('Content-Type: image/png');
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');

imagepng($png,'test.png');
imagedestroy($png);

Instead of above, that prints the png successfully, I need to use the script so it generates several dynamically created pngs and be able to parse them in an html page.

Therefore I tried to uncomment the headers and return $png:

return $png;

and on the other side parse it

$png = $obj->pngGeneratorFuntion(1,30,'blank');
imagepng($png);
imagedestroy($png);

The results looks like this

�PNG  IHDR.i ��PLTE���U��~tRNS@��fIDAT�c�$=�*y ���K�S����)IEND�B`��PNG  IHDR.i ��PLTE���U��~tRNS@��fIDAT�c�$i]���a�P�{O��;>IEND�B`��PNG  IHDR.i ��PLTE���U��~tRNS@��fIDAT�c�$Y����a�P�OMY�)"IEND�B`��PNG  IHDR.i ��PLTE���U��~tRNS@��fIDAT�c�$yZ/y ���TKX{U#8MIEND�B`��PNG  IHDR.i ��PLTE���U��~tRNS@��fIDAT�c�$<�2y ���>�Sq��M�IEND�B`�

and print_r($png) gives

Resource id #7

How can I achieve this. Thanks!

Upvotes: 0

Views: 2393

Answers (1)

Mihai Iorga
Mihai Iorga

Reputation: 39704

You can try base64_encode() this if you want to insert the image directly in HTML tags:

$png = $obj->pngGeneratorFuntion(1,30,'blank');
echo '<img src="data:image/png;base64,'.base64_encode($png).'">';

But it's not a good solution to insert image data directly into HTML

You can make a separate file image_process.php and send the data to that file with $_GET parameters and return image content. Then you can use:

<img src="image_process.php?text=ImageText" alt="" />

Upvotes: 3

Related Questions