Page 1 of 1

output a base64_encoded image

Posted: Thu Apr 06, 2006 7:40 am
by ed209
I am reading a mixture of plain text and images. There are various types of each i.e. gif, jpeg, png...

I would like to display them all on the same page but once you set a header, you can't then set another one for a different content type. Is there an easy way round this?

Code: Select all

loop going through different parts being read{

$type = "image/jpeg"; // or image/gif or image/png or text/plain
$file = "some ascii string";

header("Content-type: $type");
echo($file);

}
At the moment I am sending $type and $file as GET to another script, but I think they're too long and my images are corrupted. Alternatively, I could check for images and

Code: Select all

echo '<img src="see_the_file.php?type='.$type.'&file='.urlencode(base64_encode($file)).'" />
but I think I'll still have the same problem.

Posted: Thu Apr 06, 2006 9:43 am
by feyd
images are sent in separate streams apart from the page code, so your second one is the only real way. If $file is a filename then yes, the images will be corrupt as echo will not load the file referenced readfile() is useful for this. If $file is the binary data, the resultant URL could indeed by quite long, long enough to get truncated by your browser or the server depending on various settings of course. In this case, I'd either write the data to an actual file or store it in a database to make referencing simple.

Posted: Thu Apr 06, 2006 9:50 am
by ed209
the files were only small, but even so they would not send as part of a URL. In the end I just saved the files to disc and viewed them manually. It was only for testing though.

Code: Select all

function create_image($type, $img_data){
	$myFile = microtime().".".$type;
	$fh = fopen($myFile, 'w');
	fwrite($fh, $img_data);
	fclose($fh);
	chmod($myFile, 0777);
}

create_image("jpg", "some ascii str");