3

With this code, I am trying to create an image of a signature from JSON and then display the image directly in the browser without saving it. The problem I have is I want to display this image within a HTML document so the header information has already been modified. Is there a way to display an image from it's raw image data within an HTML document? Or any other way around this issue?

$json = $ticket->sig; // get the json representing the signature
$img = sigJsonToImage($json);  //get image resource from json

    // HERE I WANT TO DISPLAY THE IMAGE BUT GET Cannot modify header information
header('Content-Type: image/png');
imagepng($img, null, 0, PNG_NO_FILTER);
imagedestroy($img);
2
  • What do you mean by signature? Commented May 16, 2014 at 18:38
  • There is a drawing pad in the app that is used to draw a signature. This signature is saved as JSON in a database Commented May 16, 2014 at 18:39

2 Answers 2

11

Sure, you can use two things together for this.

The HTML Image tag supports base64 encoded image. It can be big for html, but will work. Also you can output image to buffer using ob_start and ob_end functions. See https://www.php.net/manual/pt_BR/function.ob-start.php

So for PHP file you can do:

$json = $ticket->sig; // get the json representing the signature
$img = sigJsonToImage($json);  //get image resource from json

ob_start(); // Start buffering the output
imagepng($img, null, 0, PNG_NO_FILTER);
$b64 = base64_encode(ob_get_contents()); // Get what we've just outputted and base64 it
imagedestroy($img);
ob_end_clean();

// Print the HTML tag with the image embedded
echo '<img src="data:image/png;base64,'.$b64.'"/>';

This will make a HTML Img tag with the embedded image. Hope is this what you want.

PS: Keep in mind that this will increase the loading time for your HTML data, since browser opens another thread for downloading the image if you just link it.

Sign up to request clarification or add additional context in comments.

1 Comment

beautiful. This is exactly what I am looking for. I will note that Orion's answer worked, but I am also converting the html to pdf and it did not work for that part of the app.
2

You can try this

<img src="/path/to/yourimage.php">

In yourimage.php.

$json = $ticket->sig; // get the json representing the signature
$img = sigJsonToImage($json);  //get image resource from json

    // HERE I WANT TO DISPLAY THE IMAGE BUT GET Cannot modify header information
header('Content-Type: image/png');
imagepng($img, null, 0, PNG_NO_FILTER);
imagedestroy($img);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.