Reputation: 13620
My server is running php and I want it to be able to send me images. For instance "profile.jpg".
In C# i just populate a bitmap image with the source, but how do I create a source, a file that outputs the images binary data?
I have this:
<?php
$file = 'profil.jpg';
$image = imagecreatefromjpeg($file);
imagealphablending($image, false);
imagesavealpha($image, true);
// start buffering
ob_start();
imagepng($image);
$contents = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/jpeg;base64,".base64_encode($contents)."' />";
imagedestroy($image);
?>
But it outputs HTML, could i just echo base64_encode($contents)
?
Upvotes: 0
Views: 129
Reputation: 6966
I'd say you're missing the headers and instead of cleaning the buffer you should flush it, something like that (off the top of my head)
<?php
$file = 'profil.jpg';
$image = imagecreatefromjpeg($file);
imagealphablending($image, false);
imagesavealpha($image, true);
// start buffering
ob_start();
header('Content-Description: File Transfer');
header('Content-type: image/png');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
imagepng($image);
$contents = ob_get_contents();
// echo $contents;
// ob_end_flulsh();
/* OR */
ob_get_fllush();
echo "<img src='data:image/jpeg;base64,".base64_encode($contents)."' />";
imagedestroy($image);
?>
Something like that. You can of course add more information or have less in the header, depending on what you need at the other end. HTTP/1.1: Header Field Definitions. Also Output Control Fctns
Upvotes: 1