Comment Base64 Encoder l'image sous linux bash / shell

j'essaie de base64 Encoder une image dans un script shell et la mettre en variable:

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

j'ai aussi essayé quelque chose comme ça:

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

mais sans succès.

je veux faire quelque chose comme ça:

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

j'ai trouvé ce http://www.zzzxo.com/q/answers-bash-base64-encode-script-not-encoding-right-12290484.html

mais n'ont toujours pas eu de succès.

62
demandé sur the Tin Man 2013-06-04 17:03:22

4 réponses

, Vous devez utiliser cat pour obtenir le contenu du fichier nommé " DSC_0251.JPG', plutôt que le nom du fichier lui-même.

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

Toutefois, base64 peut lire à partir du fichier lui-même:

test=$( base64 DSC_0251.JPG )
98
répondu chepner 2013-06-04 13:09:52

il y a une commande Linux pour cela: base64

base64 DSC_0251.JPG >DSC_0251.b64

pour attribuer un résultat à une utilisation variable

test=`base64 DSC_0251.JPG`
33
répondu David Jashi 2013-06-04 13:54:40

une Seule ligne de résultat:

base64 -w 0 DSC_0251.JPG

pour HTML :

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

fichier:

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

dans variable:

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

dans variable pour HTML :

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

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

27
répondu Eduardo Cuomo 2015-10-01 20:02:32

Base 64 pour html:

file="DSC_0251.JPG"
type=$(identify -format "%m" "$file" | tr '[A-Z]' '[a-z]')
echo "data:image/$type;base64,$(base64 -w 0 "$file")"
2
répondu Andrey Izman 2017-08-01 22:00:45