如何去code的base64标签< IMG SRC>之前(或期间)的ReadFile的(QUOT;&的mypage.html QUOT;) [英] How to decode base64 tag <img src> before (or during) the readfile("mypage.html")

查看:273
本文介绍了如何去code的base64标签< IMG SRC>之前(或期间)的ReadFile的(QUOT;&的mypage.html QUOT;)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能做这样的事情:

`ReadFile的(base64_de code_only_img_src_tags(的mypage.html));

我一直在寻找一个解决方案,但没有结果。这个想法是改变线路连接一个html文件给他去codeD线codeD,例如:

 < IMG src=\"data:image/png;base64,**iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEW/v7////+Zw/90AAAAEUlEQVQI12P4z8CAFWEX/Q8Afr8P8erzE9cAAAAASUVORK5CYII=**\">

要:

 < IMG SRC =/路径/到/图像/ Image.gif的>

我知道也许我应该解析code检测与IMG SRC标记线,然后去code这些线的标记** **的一部分,但我不知道该怎么做在ReadFile的过程中。

先谢谢了。

正如@mario说,我测试了code:

  $ newhtml =的file_get_contents('newhtml.html');功能data_to_img($比赛){
    列表(,IMG $,$型,$的base64,$结束)= $匹配;
    $斌= base64_de code($ BASE64);
    $ MD5 = MD5($斌); //生成一个新的临时文件名
    $ FN =$ MD5 $型;
    file_exists($ FN)或的file_put_contents($ FN,$斌);    回归$ IMG $ FN $结束; //新< IMG>标签
}

如果我尝试回声:

 回声$p$pg_replace_callbak('#(<img[^>]+src=\")data:image/(gif|png|jpeg);base64,([\\w=+/]+)(\"[^>]*>)#', data_to_img,$内容);

和它上面的HTML示例工作!现在,我想用我真正的HTML文件。我注意到,IMG SRC比我所提供的例子更长的时间。 IMG SRC,我有真实的例子是太长粘贴在这里,所以请狗图像和图像信息的第二个按钮的鼠标点击查看以base64 code。非常感谢!

HTML使用Base64图像文件

更新:希望这家伙曾与大的base64编纂和正则表达式同样的问题。

链接到类似的问题

UPDATE2:马里奥解决我的问题,非常感谢你的人。这里的code和正则表达式为preg_replace_callback:

 回声$p$pg_replace_callback('#(<img\\s(?>(?!src=)[^>])*?src=\")data:image/(gif|png|jpeg);base64,([\\w=+/]++)(\"[^>]*>)#', data_to_img,$内容);


解决方案

您可以这样做。但那种失败的目的,你必须要注意不要解压两次图像到临时目录(这将意味着)。

 回声$p$pg_replace_callback('#(<img\\s(?>(?!src=)[^>])*?src=\")data:image/(gif|png|jpeg);base64,([\\w=+/]++)(\"[^>]*>)#', data_to_img,$内容);功能data_to_img($比赛){
    列表(,IMG $,$型,$的base64,$结束)= $匹配;    $斌= base64_de code($ BASE64);
    $ MD5 = MD5($斌); //生成一个新的临时文件名
    $ FN =TMP / IMG / $ MD5 $类型。
    file_exists($ FN)或的file_put_contents($ FN,$斌);    回归$ IMG $ FN $结束; //新&LT; IMG&GT;标签
}

(我在这里忽略无效 ** 标记。)

在特别的你不能再加上 用readfile ,因为你需要捕捉文件的内容自己重写。然后,它仍然应该事先应用任务,在每个请求没有特设的。

I want to know if it's possible to do something like this:

`readfile(base64_decode_only_img_src_tags("mypage.html"));

I've been looking for a solution but without results. The idea is to change the lines encoded of an html file to his decoded line, for example:

<img src="data:image/png;base64,**iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEW/v7////+Zw/90AAAAEUlEQVQI12P4z8CAFWEX/Q8Afr8P8erzE9cAAAAASUVORK5CYII=**">

To:

<img src="/path/to/images/image.gif">

I know maybe I should parse the code to detect the lines with img src tag and then decode the ** marked part of these lines **, but I don't know how to do it during the readfile.

Thanks in advance.

Just as @mario said, I'm testing his code:

 $newhtml = file_get_contents('newhtml.html');

function data_to_img($match) {
    list(, $img, $type, $base64, $end) = $match;
    $bin = base64_decode($base64);
    $md5 = md5($bin);   // generate a new temporary filename
    $fn = "$md5.$type";
    file_exists($fn) or file_put_contents($fn, $bin);

    return "$img$fn$end";  // new <img> tag
}

If I try to echo:

 echo preg_replace_callbak('#(<img[^>]+src=")data:image/(gif|png|jpeg);base64,([\w=+/]+)("[^>]*>)#', "data_to_img", $content);

And it worked with the html example above!! Now I'm trying with my real html file. I noticed that the img src are longer than the example that I've provided. Real example of img src that I have is too long to paste here, so please second button mouse click on the dog image and image information to see the base64 code. Thanks a lot!!

html file with base64 images

UPDATE: Hope this guy had the same problem with base64 large codification and regex

Link to the similiar problem

UPDATE2: Mario solved my problem, thank you very much man. Here's the code and regex for preg_replace_callback:

echo preg_replace_callback('#(<img\s(?>(?!src=)[^>])*?src=")data:image/(gif|png|jpeg);base64,([\w=+/]++)("[^>]*>)#', "data_to_img", $content);

解决方案

You could do that. But it kind of defeats the purpose, and you would have to take care not to unpack images twice into the temporary directory (which this would imply).

echo preg_replace_callback('#(<img\s(?>(?!src=)[^>])*?src=")data:image/(gif|png|jpeg);base64,([\w=+/]++)("[^>]*>)#', "data_to_img", $content);

function data_to_img($match) {
    list(, $img, $type, $base64, $end) = $match;

    $bin = base64_decode($base64);
    $md5 = md5($bin);   // generate a new temporary filename
    $fn = "tmp/img/$md5.$type";
    file_exists($fn) or file_put_contents($fn, $bin);

    return "$img$fn$end";  // new <img> tag
}

(I've ignored the invalid ** markup here.)

In particular you can't combine that with readfile, as you need to capture the file contents yourself to rewrite it. And then it's still a task that should be applied beforehand, not ad-hoc on each request.

这篇关于如何去code的base64标签&lt; IMG SRC&GT;之前(或期间)的ReadFile的(QUOT;&的mypage.html QUOT;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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