I'm parsing a js file server side, with regex, in order to find all the elements of template I have to change before actually sending the code to the client. Things work ok for files but not for images.
The key idea is to classically encode them in base64, and use atob()
to decode. I don't know if PHP is able to encode an image in base64 while doing a regex (?)
If yes, that could explain that I get no results. If not, it would be so nice if someone could take a little time to help me to understand the problem illustrated below.
Parser (PHP) :
$content = file_get_contents('myfile.js');
$pattern = '/__image\[(.*)\]__/i';
$url = '$1';
$complete = preg_replace($pattern, base64_encode(file_get_contents($url)), $content);
echo json_encode($complete);
Client side controller (JS) :
... //result of an ajax call
content = JSON.parse(responseText);
eval(content); //data are safe
File which contains the call to the image (JS) :
config.design = {
logo : {
...
},
image : {
...
background : background : 'url(data:image/jpg;base64,__image[css/images/logo/logo.jpg]__)'
},
baseline : {
...
}
};
Result :
background-image: url(data:image/jpg;base64,); //[Error] Failed to load resource ... (kCFErrorDomainCFNetwork erreur -10). (data:image/jpg;base64,false, line 0)