How to base64 encode image in linux bash / shell

前端 未结 6 1854
说谎
说谎 2021-01-29 23:30

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

6条回答
  •  伪装坚强ぢ
    2021-01-29 23:47

    Single line result:

    base64 -w 0 DSC_0251.JPG
    

    For 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:

    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 
    

    See: http://www.greywyvern.com/code/php/binary2base64

提交回复
热议问题