FastCGI sending image data as response C++ - html

The application I'm working on needs to be able to send images as a response. I'm having a bit of trouble getting this to work properly. I tried making a response message string with "Content type images/jpeg" after making a simple get request, and what pops up in the browser is a broken image symbol, so I guess it knows it's supposed to be an image. My images are stored in the database as hexidecimal. How do I format the response for the FCGXFPrintF(...) message to make an image pop up. I tried
Content-Type: image/jpeg\r\n\r\n
<a bunch of hexidecimal>
but that doesn't work. Can someone give me an example of how to format the message, and an example of actual image data for me to test (because my image data might be broken)
Thanks

Assuming you have an image called image.jpg, paste this into a file called go, then type
php -f go
and you will see what your C++ should generate:
<?php
header("Content-type: image/jpeg");
readfile('image.jpg');
?>

Related

Google Chrome show ajax response

I am using Google Chrome Developer Tools to try to see the response of some AJAX url's.
The problem is that when I click on the NETWORK TAB, then on the link, then on RESPONSE, I see this text : "THIS REQUEST HAS NO RESPONSE DATA AVAILABLE".
I have been using FIREBUG and I am 100% sure there is a response from that page.
Can somebody help with this ?
Thank you !
You can try manually checking if there's a response or not
So, generally when dealing with ajax, in most cases we use the POST, You can create a 'same structured' page to handle same input/response but using Get method and print the output data as normal.
This way you can see if there's any response/errors in your script very easily

HTTP POST call embedded in a static html page

I want to embed an HTTP call which returns a png image into an html page.
I will run the page on github's gh-pages if that makes a difference.
The url is: http://public.opencpu.org/R/user/SChamberlain/ropensci_dash/apistatus10/png
Note that sometimes the url doesn't return the png as the service is sometimes down.
I want to call that url using POST method, and simply return the png into the page. Ideally the http call would be run on page recycle as well.
Do I use an iframe, form, javascript?
Thanks! Scott
[I might as well answer properly]
When a server is feeding you image data and you want to load it, you might as well treat it like an image and use an img tag. The problem with this approach in your particular case is that you said that the server can sometimes be down and this would mean that if you simply appended the image without checking for a broken link first you will get that ugly broken image space.
However, the simplicity of using an image is still feasible. By using a little javascript with a load event handler you can append the image if and only if it loads successfully. If you get any kind of error code from the request the load event will never fire and you won't append a broken image.
Here's the concept in vanilla-js:
img = document.createElement("img");
img.onload = function(e){document.getElementsByTagName("body")[0].appendChild(img);};
img.src = "http://public.opencpu.org/R/user/SChamberlain/ropensci_dash/apistatus10/png";
You can also see it in action here: http://jsfiddle.net/BwJeC/
You could use XHR to load the binary data from the external, dynamic page and set the content of the image.
This question, is very much similar
Or this
I really don't understand why you want to POST.
Anyway if you want to query a picture with a POST data you could, may have to do a Js XHR call and return the the image as base64 and then do something like:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" />
The best would still be to to a simple get call:
<img src="/dynamicallyGeneratedPicture" />
with a route or script that return a document of type image.

Embedd Images from MongoDB gridFS into my HTML-Page

I saved some files via MongoDBs gridFS into my database.
I know how to retrieve files just with php in my browser:
header('Content-type: '.$object->file['filetype']);
echo $object->getBytes();
That works perfectly fine. But I now wan't to put the image in a context.
e.g.
<img><?php echo $object->getBytes(); ?></img>
If I put the code
echo $object->getBytes();
in a htmlpage, I just get the image like this:
����JFIFHH��C !"$"$��C���Y"�� ��O!"12AQaq�BR��#b��3Cr��$S����4���%cs�5&��DT�������&!1A"Q2q�#Ba��?�( �#�EUDDUU�#��(�?2� ��.h#k���.��d�M���(��=�]��Nk��E�gZz�GI�Вr���~+x������<{YZSm�n�N I�DAmDU������G���U:���H�6J̺'!lV�������D:H2��S��7��U;����7����������n��wV$�ʞ�i|��wP����?Qx۾�/�K'�:=���i�E�0)�E��4OU�G�W��`t] �������.�j��='�/�:9�V����봺a_}2����a_�D��J[r�J���f�6�B<���ѿc���=8�Q��1!("��B���⅝4<#��L���K�Iy�"���?�6�6�a7�k����%�F6�y��� v��
and so on...
does anybody know how I can embedd an image into a website???
Thanks
You should add a new PHP page that returns the image (with appropriate content-type and content-length headers) like your first sample above. Say that page is called "image.php". In your "main" page, then, you should construct an image tag like:
<img src="/path/to/image.php?..."/>
Where "..." is replaced with some query string to look up and serve the image you want (the string representation of the GridFS file's ObjectId in the _id field might do well here, or some other unique identifier).
If you're looking for in-doc images you could try
<img src="data:image/png;base64,����JFIFHH��C !"$"$��C���Y..." />
But dcrosta's method may be preferred depending on your overall intent.
You'll have to plug in the correct image type and encoding (or re-encode).

Using AJAX and return a picture

I have a problem receiving and opening a picture via AJAX.
If I call the following page:
http://127.0.0.1:8889/ex?sql=SELECT+Image+FROM+Persons+WHERE+Number+Like+%27%2501%27
a picture is displayed from a blob field in IE8.
Now I would like to open this into a div after someone pressed a key (using AJAX)?
Trying to use xhr.responseText does not work (I get an error. Using it on a text response works). So it seems that my problem is to grab the result from the ajax request.
How can I do this?
Some code and the error message:
var picReturn = xhr.responseText;
=> Could not continue due to the following error: c00ce514
You have three options:
Place the resultant data in an iframe. Not very practical.
Take the result and place it in am image source as a data:uri. Not supported in older browsers and limited to 32/64Kb depending on the browser.
Skip the AJAX and write a web service and use that as your url. This is the best option.
You don't say what language you're using server-side but you essentially want to open a web response, set the header to "image/jpeg" and return your stream.

Download a picture OnClick

How do you actually download a picture when you click on it? Is there some kind of a javascript code to do just that? Here is how i show the image with pure HTML.
<img src="myPic.png" border="0">
Assuming by "download" you mean "Cause the user's browser to throw up the 'save or open' dialogue" — you can't.
You could link to another URL offering the same file but with the Content-Disposition header set to attachment. The specifics of how you would provide such a resource at that URL would depend on the server side capabilities on offer to you.
Most people right-click on the image and choose "Save image as..."
The alternate is to link to use a server-side script that sets a "Content-type" and "Content-disposition" header. In PHP, that would be something like this example from the docs:
header('Content-Type: image/png'); // or 'image/jpg' or 'image/gif'
header('Content-Disposition: attachment; filename="filename.png"');
readfile('original.png');
UPDATE: Since you say the image is generated by a PHP script in the first place, there are a few options:
Put the URL (sig.php?...) as the parameter to readfile. This will mean double processing for anyone who clicks to download.
Cache the output from your image generation script to the filesystem, then pass that file to readfile.
Edit the image generation script to accept an extra parameter like mode=download and then where you are about to output the image, if the parameter is present, set those two headers above.
I trick this out a bit - I zip the picture and put it somewhere. Then, instead of using a fancy script or server-side stuff, I make the picture's link the location of the .zip file. The user get's warned/asked if they want to download this zip and voila...
But this is usually in an area where the user is someone who would want the image...
Do you want to open the picture in a new window/tab? Or do you want to download it to the users computer? If you want the user to save the image, then you need to set the content-type of the file they receive:
<?php
$file = $_GET['file'];
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
?>
Remember to check the input so people can't download source files.