douba8758 2011-11-29 10:09
浏览 56
已采纳

如何在readfile之前(或期间)解码base64标记<img src>(“mypage.html”)

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);
  • 写回答

2条回答 默认 最新

  • dpbrrczhlwbv849228 2011-11-29 10:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值