Skip to content Skip to sidebar Skip to footer

How To Decode Base64 Tag Before (or During) The Readfile("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

Solution 1:

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);

functiondata_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.

Solution 2:

load the readfile result into a variable and use this Regex

data:image/png;base64,\*\*(.+?)\*\*

Post a Comment for "How To Decode Base64 Tag Before (or During) The Readfile("mypage.html")"