Replacing all images in a CSS file with base64 encoded strings from the command line -


Actually I'm trying to write a clever single line which outputs base 64 encoded strings based on that location. One way to have an image is therefore:

  background-image: url (path / to / my / image.png);   

one ?? | Will be turned on in:

  background-image: url (data: image / png; base64, abvr 0 ?? | ++ tc = =);   

I usually turn an image into my base 64 encoded string:

  openssl enc -base64 -in path / to / my / image .png   

Which base64A outputs ?? But with newlines in it. It is determined by piping via tr :

  openssl enc -base64 -in path / to / my / image.png | Tr-d '\ n'   

which produces only one long base 64 encoded string. PBCP (on Mac OS) is sent to the clipboard, such as:

  openssl enc -base64 -in path / to / my / image.png. Tr-d '\ n' | PBCP   

It's great for ad hawk by changing an occasional image with its base64 convergence, but I want to do it all the way in the url (path / to) Is / is whatever is. To change) in the file by their respective base 64 strings. Make sure that there are only real paths and there is no data here - not outside the scope here):

I am trying to replace the stuff with sed , But I got stuck in this awesome documentation is not very difficult to find in the url (รข ?? |) pattern in the CSS file, but the bit between the bracket Need to be replaced by output and Uje so no goofy if it all possible, there is a help or a signal (I also need to examine the awk?) Would be greatly appreciated! A 'You can not do this without proper scripts.' Certainly, too!

with < Code> encodile device
  #! / Bin / bash awk -F '[[]]' -avik = '' '' '/ background-image: url (. *) / {Cmd = sprintf ("openssl enc -base64 -in% s | tr -d % C \\ n% c ", $ 2, q, q) cmd | Getline B64 Close (CMD) $ 0 = $ 1 "(Data: Image / PNG; Base64," B64 ");" } 1 '/ path / to / css / file   

Proof of concept

See for an example


Base 64 Tools
  #! / Bin / bash awk -F '[(]]' '/ background-image: url (. *) / {Cmd = sprintf ("Base64 -w0% s", $ 2) CMD | Getline B64 Close (CMD) $ 0 = $ 1 "(Data: Image / PNG; Base64," B64 ");" } 1 '/ path / to / css / file   

Proof of concept

See for an example

Comments