【问题标题】:How to base64 encode image in linux bash / shell如何在 linux bash / shell 中对图像进行 base64 编码
【发布时间】:2013-05-30 21:27:58
【问题描述】:

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

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

我也试过这样的:

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

但仍然没有成功。

【问题讨论】:

    标签: linux image shell variables base64


    【解决方案1】:

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

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

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

    test=$( base64 DSC_0251.JPG )
    

    【讨论】:

    • 用 cat 就可以了,非常感谢。我知道它可以从文件中读取,但将其存储在变量中仍然存在问题,因此 test="$(cat DSC_0251.JPG | base64)" 对我有用。
    • 什么问题?上面的两个命令应该产生相同的结果,除了第一个是useless use of cat
    • 你是对的。这是我应该做的$RESPONSE="$(curl -v -X POST -d '{"image":`base64|$DIR$IMAGE`,"location":$LOCATION,"time_created":$TIMECREATED}' -H 'Content-type: text/plain; charset=UTF8' --max-time 180 -s $URL)";
    • cat vlc.jpg | base64 -w 0 - 以防有人希望输出为字符串以进行复制和粘贴。
    • base64 DSC_0251.JPG 有什么问题?当程序将文件作为参数时,无需运行cat 过滤器(base64 [OPTIONS] [FILE]
    【解决方案2】:

    编码

    在 Linux 上

    单行结果:

    base64 -w 0 DSC_0251.JPG
    

    对于HTML

    echo "data:image/jpeg;base64,$(base64 -w 0 DSC_0251.JPG)"
    

    作为文件:

    base64 -w 0 DSC_0251.JPG > DSC_0251.JPG.base64
    

    在变量中:

    IMAGE_BASE64="$(base64 -w 0 DSC_0251.JPG)"
    

    HTML 的变量中:

    IMAGE_BASE64="data:image/jpeg;base64,$(base64 -w 0 DSC_0251.JPG)"
    

    在 OSX 上

    OSX上,base64二进制不同,参数也不同。如果你想在 OSX 上使用它,你应该删除-w 0

    单行结果:

    base64 DSC_0251.JPG
    

    对于HTML

    echo "data:image/jpeg;base64,$(base64 DSC_0251.JPG)"
    

    作为文件:

    base64 DSC_0251.JPG > DSC_0251.JPG.base64
    

    在变量中:

    IMAGE_BASE64="$(base64 DSC_0251.JPG)"
    

    HTML 的变量中:

    IMAGE_BASE64="data:image/jpeg;base64,$(base64 DSC_0251.JPG)"
    

    通用 OSX/Linux

    作为外壳函数

    @base64() {
      if [[ "${OSTYPE}" = darwin* ]]; then
        # OSX
        if [ -t 0 ]; then
          base64 "$@"
        else
          cat /dev/stdin | base64 "$@"
        fi
      else
        # Linux
        if [ -t 0 ]; then
          base64 -w 0 "$@"
        else
          cat /dev/stdin | base64 -w 0 "$@"
        fi
      fi
    }
    
    # Usage
    @base64 DSC_0251.JPG
    cat DSC_0251.JPG | @base64
    

    作为 Shell 脚本

    创建base64.sh 文件,内容如下:

    #!/usr/bin/env bash
    if [[ "${OSTYPE}" = darwin* ]]; then
      # OSX
      if [ -t 0 ]; then
        base64 "$@"
      else
        cat /dev/stdin | base64 "$@"
      fi
    else
      # Linux
      if [ -t 0 ]; then
        base64 -w 0 "$@"
      else
        cat /dev/stdin | base64 -w 0 "$@"
      fi
    fi
    

    使其可执行:

    chmod a+x base64.sh
    

    用法:

    ./base64.sh DSC_0251.JPG
    cat DSC_0251.JPG | ./base64.sh
    

    解码

    恢复可读数据:

    base64 -d DSC_0251.base64 > DSC_0251.JPG 
    

    【讨论】:

    • -w 0这个参数在很多情况下很重要。前任。如果我们直接运行到命令行。谢谢!
    • 在 OSX 上顺便说一句,我收到了 base64: invalid option -- w。当它被删除时,我得到Unable to open '0': No such file or directory。请参阅this
    • @coblr 在OSX中,你需要删除-w 0
    【解决方案3】:

    有一个 Linux 命令:base64

    base64 DSC_0251.JPG >DSC_0251.b64
    

    将结果分配给变量使用

    test=`base64 DSC_0251.JPG`
    

    【讨论】:

    • base64 -d DSC_0251.b64 > DSC_0251.JPG 可以让您恢复可读数据。
    【解决方案4】:

    如果你需要终端输入,试试这个

    lc=`echo -n "xxx_${yyy}_iOS" |  base64`
    

    -n 选项不会在 base64 命令中输入“\n”字符。

    【讨论】:

    • -n 选项是使用 echo 时的关键。
    【解决方案5】:

    基于 64 的 html:

    file="DSC_0251.JPG"
    type=$(identify -format "%m" "$file" | tr '[A-Z]' '[a-z]')
    echo "data:image/$type;base64,$(base64 -w 0 "$file")"
    

    【讨论】:

      【解决方案6】:

      base64 并将其放入剪贴板:

      file="test.docx"
      base64 -w 0 $file  | xclip -selection clipboard
      

      【讨论】:

        猜你喜欢
        • 2012-10-26
        • 2018-10-26
        • 1970-01-01
        • 2014-11-18
        • 1970-01-01
        • 2021-10-06
        • 2011-08-29
        • 2017-11-11
        • 1970-01-01
        相关资源
        最近更新 更多