Reputation: 1348
I am making a GET request to a REST API using PHP to call an image. In the response body, I get something like this:
ÿØÿàJFIF
`ÿÛC $.' ",#(7),01444'9=82<.342ÿÛC
òÛ敇P`:ëÇjË–ÖO$¬#39 ûÌp¿©ìö61iö6öP(X cP=†3ýhŸºð…ql"Ž œ–X§bÃë¸^g«h·Z>§-Ë’ê7#®@‘FÈŽÄWºm®KÇÖmÕ_žÒeŽJ9GæTþäæÙó÷›ó¢¶Í¯=( û+d]FÄÈ>QwþŒ\W¦¨® æÊG¶o#eÃÄOMêw/êw·)umÄrT¿B3@¬_ “×Ê{
2!!22222222222222222222222222222222222222222222222222ÿÀ0$"ÿÄ
ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ
ÿĵw!1AQaq"2B‘¡±Á #3RðbrÑ $4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚâãäåæçèéêòóôõö÷øùúÿÚ?óä·ï7ç]W„üÚè{«©äŽÁ
What should I do with this to make it a viewable image?
Upvotes: 1
Views: 3127
Reputation:
What should I do with this to make it a viewable image?
Assuming the context is an HTML document, use an <img />
tag to display the response in your document. Set its src
attribute to the URI of the script that requests the image:
<img src="/path/to/image.php?id=..." />
In your PHP script, you will simply want to echo
the binary image data after setting the appropriate headers (covered in Gordon's answer).
Upvotes: 1
Reputation: 317147
Send the appropriate image header
. Check the Content-Type header to see what format it is. In case this is not the image binary but compressed content, run it through gzuncompress
first.
Upvotes: 2