0

I'm trying to decode images using base64_decode, and it works fine except for larger images. I was wondering if it might be due to a memory limit or some other server setting. It exports with the maximum resolution on a site with a 256 MB PHP memory limit, and not a site with a 128 MB PHP memory limit. But I would surprised if generating a relatively small image would require so much memory. The maximum resolution obtained is around 1680 x 800, file size around 140 kb. Beyond this, it fails and it outputs an empty image (filesize 0 kb). The data is posted from a Flash app.

This is the function:

$png = base64_decode($_POST['imagedata']);
header('Content-Type: image/png');
header("Content-Disposition: attachment; filename=".$_GET['name']);
echo $png;

I also tried replacing the above line with base64_decode(chunk_split($_POST['imagedata'])); but this doesn't make any difference.

Adding the following also made no difference: header('Connection: Keep-alive');

The following alternative also didn't work.

$encoded = $_POST['imagedata'];
$decoded = ""; 
for ($i=0; $i < ceil(strlen($encoded)/256); $i++) 
 $decoded = $decoded . base64_decode(substr($encoded,$i*256,256));    
header('Connection: Keep-alive');
header('Content-Type: image/png');
header("Content-Disposition: attachment; filename=".$_GET['name']);
echo $decoded;

I have run out of ideas, so if you have any ideas of what could be the issue, that would be really appreciated. Since the above php code seems to work for everyone else, I was wondering if it could be a particular server setting that needs to be changed (other than PHP memory).

In case it is something to do with content-encoding, with a successfully decoded small image the network debugging panel outputs the following: "Content-Encoding:gzip" (otherwise when it fails, it outputs: "Content-Length:0").

Fred
  • 49
  • 1
  • 7

0 Answers0