我怎么能大串的base64图像数据转换回用PHP的形象? [英] How can I convert a large string of base64 image data back to an image with PHP?

查看:175
本文介绍了我怎么能大串的base64图像数据转换回用PHP的形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的base64图像数据的大串(约200K)。当我尝试用正确的标题输出去codeD数据到数据转换,脚本死了,因为如果没有足够的内存。我没有得到任何错误在我的Apache日志。这个例子code下面我有工作的小图像。我该如何去code大图像?

 < PHP
//功能显示图像功能display_img($ IMG code,$型){
     标题(内容类型:图像/'.$型);
     标题(内容长度:.strlen($ IMG code));
     回声base64_de code($ IMG code);
}$ IMG code =的file_get_contents(image.txt);//直接显示图像
display_img($ IMG code,JPG);?>


解决方案

的base64 -en codeD数据干净地分离每4个字节(即3个字节的明文都设有coded到4个字节的base64恩codeD文字的),你可以在你的B64字符串分割成4个字节的倍数,并分别对其进行处理:

 而(不是字符串结尾){
   采取下一个4096字节//例如 - 4096是2 ^ 12,因此是4的倍数
   //你可以使用更大的块,取决于你的内存限制
   基于64位德code他们
   去codeD结果追加到一个文件,或一个字符串,或将其发送到输出
}

如果你有一个有效的base64字符串,这将相同的工作一下子解码这一切。

I have a large string of base64 image data (about 200K). When I try to convert that data by outputting the decoded data with the correct header, the script dies, as if there isn't enough memory. I get no error in my Apache logs. The example code I have below works with small images. How can I decode a large image?

<?php
// function to display the image

function display_img($imgcode,$type) {  
     header('Content-type: image/'.$type);   
     header('Content-length: '.strlen($imgcode));   
     echo base64_decode($imgcode);  
}

$imgcode = file_get_contents("image.txt");  

// show the image directly  
display_img($imgcode,'jpg');

?>

解决方案

Since base64-encoded data is cleanly separated every 4 bytes (i.e. 3 bytes of plaintext are encoded into 4 bytes of base64-encoded text), you could split your b64 string into multiples of 4 bytes, and process them separately:

while (not at end of string) {
   take next 4096 bytes // for example - 4096 is 2^12, therefore a multiple of 4
   // you could use much larger blocks, depends on your memory limits
   base64-decode them
   append the decoded result to a file, or a string, or send it to the output
}

If you have a valid base64 string, this will work identically to decoding it all at once.

这篇关于我怎么能大串的base64图像数据转换回用PHP的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆