Bash - 在html中将图片网址更改为base64

时间:2013-07-07 14:41:58

标签: python html bash base64

28我尝试制作一个脚本,将图像源从普通链接转换为html文件中的base64编码。 但是有一个问题:有时候,sed告诉我

  

script.sh:第25行:/ bin / sed:参数列表太长

这是代码:

#!/bin/bash
# usage: ./script.sh file.html


mkdir images_temp

for i in `sed -n '/<img/s/.*src="\([^"]*\)".*/\1/p' $1`;

    do echo "######### download the image";
    wget -P images_temp/ $i;

    #echo "######### convert the image for size saving";
    #convert -quality 70 `echo ${i##*/}` `echo ${i##*/}`.temp;

    #echo "######### rename temp image";
    #rm `echo ${i##*/}` && mv `echo ${i##*/}`.temp `echo ${i##*/}`;

    echo "######### encode in base64";
    k="`echo "data:image/png;base64,"`$(base64 -w 0 images_temp/`echo ${i##*/}`)";

    echo "######### deletion of images_temp pictures";
    rm images_temp/*;

    echo "######### remplace string in html";
    sed -e "s|$i|$k|" $1 > temp.html;

    echo "######### remplace final file";
    rm -rf $1 && mv temp.html $1;

    sleep 5;
done;

我认为当图像大于~128ko时,$ k参数对于sed来说太长了; sed无法处理它。

如何让它发挥作用?

提前谢谢!

PS1:抱歉非常难看的代码

PS2:或者我如何在python中执行此操作? PHP?我开了!

1 个答案:

答案 0 :(得分:1)

您的base64编码图像可以是多兆字节,而系统可能会限制参数的最大长度(传统上大约为128k)。 Sed也不能保证处理超过8kb的行,但像GNU sed这样的版本可以处理更多。

如果您想尝试使用sed,请在文件中而不是在命令行中提供说明。而不是

sed -e "s|$i|$k|" $1 > temp.html;

使用

echo "s|$i|$k|" > foo.sed
sed -f foo.sed "$1" > temp.html