1

This question was created because my last question was marked as a duplicate when it wasn't a duplicate.

I have a GET API call that is returning back a UTF-8 PNG image. I am making the call from PHP and want to display the PNG thumbnail embedded in the webpage. Can someone help me figure out how to display the image inline with the rest of the webpage? So far I am just taking the CURL response and echoing it out to the page. Here is what I see. Thank you in advance.

$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'$url');
curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
curl_setopt($curl,CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
$headers = array( 'X-Tableau-Auth: ' . $GLOBALS['authToken'] );
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$resp = curl_exec($curl);
echo $resp;
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($resp, 0, $header_size);
$body = substr($resp, $header_size);
echo $body;
curl_close($curl);

enter image description here

1 Answer 1

5

One way to do so is to encode the content of the image in base64, then embedded it in your page as

echo '<img src="data:image/png;base64,' . base64_encode($body) . '">';

To know more about the "data" URL scheme read RFC2397

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

2 Comments

You just saved my sanity!
That saved my day. Fantastic!

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.