This code works fine (but I am concerned it will fail with large input files). Example one reads the whole file and does not loop, example 2 reads 3k chunks and loops until EOF.
$in = fopen("in.b64", 'r');
$out = fopen("out.png", 'wb');
if ((!$in) || (!$out)) die ("convert: i/o error in base64 convert");
$first = true;
while (!feof($in))
{
$b64 = fread($in,filesize($in_fn));
// strip content tag from start of stream
if ($first)
{
$b64 = substr($b64,strpos($b64,',')+1);
$first = false;
}
$bin = base64_decode($b64);
if (!fwrite($out,$bin)) die("convert write error in base64 convert");
}
fclose($in);
fclose($out);
While this code produces a corrupt image :
$in = fopen("in.b64", 'r');
$out = fopen("out.png", 'wb');
if ((!$in) || (!$out)) die ("convert: i/o error in base64 convert");
$first = true;
while (!feof($in))
{
$b64 = fread($in,3072);
// strip content tag from start of stream
if ($first)
{
$b64 = substr($b64,strpos($b64,',')+1);
$first = false;
}
$bin = base64_decode($b64);
if (!fwrite($out,$bin)) die("convert write error in base64 convert");
}
fclose($in);
fclose($out);