php - 将base64字符串解密为jpeg图像时损坏的图像
问题描述
我的 android 应用程序正在将位图图像(临时捕获)作为 base64 编码字符串发送到远程服务器。代码如下。
ByteArrayOutputStream baos = new ByteArrayOutputStream();
takenImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
现在我正在使用下面的代码解密图像
function base64_to_jpeg($base64_string, $output_file) {
// open the output file for writing
$ifp = fopen( $output_file, 'wb' );
#spliting the string on commas
#$data[ 0 ] == "data:image/png;base64"
#$data[ 1 ] == <actual base64 string>
$data = explode( ',', $base64_string );
// we could add validation here with ensuring count( $data ) > 1
fwrite( $ifp, base64_decode( $data[ 1 ] )); #
// clean up the file resource
fclose( $ifp );
return $output_file;
}
现在我收到一个错误
Undefined offset : 1 at base64_decode($data[1])
这意味着 data:image/png;base64 部分之后没有字符串。但我已经测试过该字符串在发布请求之前就在那里。如果我考虑到整个接收到的字符串,那么我会得到一个绿色阴影图像,无论拍摄的图像如何,它都是恒定的。
我错过了什么吗?
解决方案
推荐阅读
- c# - 在 ASP.NET Core Razor 页面中加载带有数据的选项卡(没有实体框架)
- python - 初学者“艰难地学习 Python 3”前 5 和 6. Chromebook w/ Linux beta vs repl.it
- ruby-on-rails - Rails API - 获取未经许可的参数上传文件
- python - 尝试为 .gg 网站运行 scrapy shell 命令,但返回一个空括号
- javascript - 带有 JavaScript 和 Vue 的 Bootstrap 4 模态:$ 未在 eval 中定义
- unity3d - 双跳有时会变成三级跳
- ios - AudioKit 5 iOS 当前播放时间
- jquery - 将数字字符串中的第一个数字限制为 2 到 9
- python - 求解正整数系数的线性向量加法
- python-3.x - pandas.Series.append() 将新值添加为额外列,而不是将其附加到系列的末尾