什么阻止base64_decode适用于大文件/图像

时间:2014-08-20 08:23:27

标签: php image post base64 png

我已经实现了三个php脚本,其中两个是在stackoverflow上提出的,用于解决较大图像的base64_decode无效的问题。在这一点上,我放弃了PHP脚本,我只是想知道可能需要哪些服务器升级才能使其工作。所有三个都在一台服务器上以任何分辨率工作,但在另一台服务器上,获得的最大分辨率约为1680 x 800(文件大小约为140 kb)。除此之外,它失败并输出一个空图像(文件大小为0 kb)。数据从Flash应用程序发布。我可以在两台服务器之间看到的唯一区别是php内存限制(256 MB对128 MB)。这可能吗?谢谢你的任何建议!

尝试的php脚本复制如下:

版本1:

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

版本2:

$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;

版本3:

$data = $_POST['imagedata'];
$target = 'img_'.date('Y-m-d-H-s').'.png';

$whandle = fopen($target,'w');
stream_filter_append($whandle, 'convert.base64-decode',STREAM_FILTER_WRITE);

fwrite($whandle,$data);
fclose($whandle);
header("Content-Length: " . filesize($target));
header("Content-Disposition: attachment; filename=" . basename($target));
readfile($target);
unlink($target);

1 个答案:

答案 0 :(得分:2)

可能与内存有关的问题:帖子大小限制或php内存使用限制。

  • 检查您的脚本&#39;使用xdebug进行内存消耗:http://xdebug.org/docs/execution_trace。您的脚本不应超过php.ini文件中的memory_limit参数:http://php.net/manual/fr/ini.core.php#ini.memory-limit
  • 检查你的php.ini是否有post_max_size(应该大于你的最大图像大小)
  • 检查您的网络服务器是否对发布数据有任何限制。
  • 检查你的日志:如果base64_encode编码不正确,那么应该会出现错误消息。