如何在linux bash / shell中对图像进行base64编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16918602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to base64 encode image in linux bash / shell
提问by dash00
I'm trying to base64 encode an image in a shell script and put it into variable:
我正在尝试在 shell 脚本中对图像进行 base64 编码并将其放入变量中:
test="$(printf DSC_0251.JPG | base64)"
echo $test
RFNDXzAyNTEuSlBH
I've also tried something like this:
我也试过这样的事情:
test=\`echo -ne DSC_0251.JPG | base64\`
but still with no success.
但仍然没有成功。
I want to do something like this:
我想做这样的事情:
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
I found this http://www.zzzxo.com/q/answers-bash-base64-encode-script-not-encoding-right-12290484.html
我发现这个http://www.zzzxo.com/q/answers-bash-base64-encode-script-not-encoding-right-12290484.html
but still have had no success.
但仍然没有成功。
采纳答案by chepner
You need to use cat
to get the contentsof the file named 'DSC_0251.JPG', rather than the filename itself.
您需要使用cat
来获取名为“DSC_0251.JPG”的文件的内容,而不是文件名本身。
test="$(cat DSC_0251.JPG | base64)"
However, base64
can read from the file itself:
但是,base64
可以从文件本身读取:
test=$( base64 DSC_0251.JPG )
回答by David Jashi
回答by Eduardo Cuomo
Single line result:
单行结果:
base64 -w 0 DSC_0251.JPG
For HTML
:
对于HTML
:
echo "data:image/jpeg;base64,$(base64 -w 0 DSC_0251.JPG)"
As file:
作为文件:
base64 -w 0 DSC_0251.JPG > DSC_0251.JPG.base64
In variable:
在变量中:
IMAGE_BASE64="$(base64 -w 0 DSC_0251.JPG)"
In variable for HTML
:
在变量中HTML
:
IMAGE_BASE64="data:image/jpeg;base64,$(base64 -w 0 DSC_0251.JPG)"
Get you readable data back:
恢复可读数据:
base64 -d DSC_0251.base64 > DSC_0251.JPG
回答by Andrey Izman
Base 64 for html:
html 的 Base 64:
file="DSC_0251.JPG"
type=$(identify -format "%m" "$file" | tr '[A-Z]' '[a-z]')
echo "data:image/$type;base64,$(base64 -w 0 "$file")"
回答by DavidBu
To base64 it and put it in your clipboard:
对它进行 base64 并将其放入剪贴板:
file="test.docx"
base64 -w 0 $file | xclip -selection clipboard
回答by Victor Choy
If you need input from termial, try this
如果你需要来自终端的输入,试试这个
lc=`echo -n "xxx_${yyy}_iOS" | base64`
-n
option will not input "\n" character to base64 command.
-n
选项不会向 base64 命令输入“\n”字符。