Bash-在HTML中将图片网址更改为base64 [英] Bash - change image urls to base64 in html

查看:76
本文介绍了Bash-在HTML中将图片网址更改为base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

28I tried to make a script that's converting images source from normal links to base64 encoding in html files. But there is a problem: sometimes, sed tells me

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

script.sh: line 25: /bin/sed: Argument list too long

这是代码:

#!/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无法处理它.

I think the $k argument is too long for sed when the image is bigger than ~128ko; sed can't process it.

我如何使其工作?

提前谢谢!

PS1:非常抱歉,代码非常丑陋

PS1: and sorry for the very very ugly code

PS2:或者我该如何在python中做到这一点? PHP的?我开着!

PS2: or how do I do that in python ? PHP ? I'm open !

推荐答案

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

Your base64 encoded image can be multiple megabytes, while the system may place a limit on the maximum length of parameters (traditionally around 128k). Sed is also not guaranteed to handle lines over 8kb, though versions like GNU sed can deal with much more.

如果要尝试使用sed,请在文件而不是命令行中提供说明.代替

If you want to try with your sed, provide the instructions in a file rather than on the command line. Instead of

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

使用

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

这篇关于Bash-在HTML中将图片网址更改为base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆