如何在Linux bash/shell中对图像进行base64编码 [英] How to base64 encode image in linux bash / shell

查看:1684
本文介绍了如何在Linux bash/shell中对图像进行base64编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在shell脚本中对图像进行base64编码,然后将其放入变量中:

I'm trying to base64 encode an image in a shell script and put it into variable:

test="$(printf DSC_0251.JPG | base64)"
echo $test
RFNDXzAyNTEuSlBH

我也尝试过这样的事情:

I've also tried something like this:

test=\`echo -ne DSC_0251.JPG | base64\`

但仍然没有成功.

我想做这样的事情:

curl -v -X POST -d '{"image":$IMAGE_BASE64,"location":$LOCATION,"time_created":$TIMECREATED}' -H 'Content-type: text/plain; charset=UTF8' http://192.168.1.1/upload

我找到了这个 http ://www.zzzxo.com/q/answers-bash-base64-encode-script-not-encoding-right-12290484.html

但仍然没有成功.

推荐答案

您需要使用cat来获取名为'DSC_0251.JPG'的文件的内容,而不是文件名本身

You need to use cat to get the contents of the file named 'DSC_0251.JPG', rather than the filename itself.

test="$(cat DSC_0251.JPG | base64)"

但是,base64可以从文件本身读取:

However, base64 can read from the file itself:

test=$( base64 DSC_0251.JPG )

这篇关于如何在Linux bash/shell中对图像进行base64编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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