Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to include images as inline data #219

Open
dov opened this issue Mar 19, 2019 · 5 comments
Open

Add option to include images as inline data #219

dov opened this issue Mar 19, 2019 · 5 comments

Comments

@dov
Copy link

dov commented Mar 19, 2019

I'm currently using npm install inliner for embedding images in an html file into the document. This step is necessary for disconnecting the html from external references.

It would be nice if premailer also had this option.

@peterbe
Copy link
Owner

peterbe commented Mar 19, 2019

You mean, base64 inlining? Like, <img src="data:image/png;base64,xxxxxx">.

Perhaps a "better" solution was if premailer could expose the parsed lxml document in some form so that you can make those kinds of manipulations yourself.

Something like this:

import base64
import premailer

p = premailer.Premailer(html_string)
for img in p.cssselect('img.thumbnail'):
    fn = uri_to_local_file_path(img.attr('src'))
    b64 = base64.b64encode(open(fn, "rb").read())
    img.attr('src', f'data:image/png;base64,{b64}')
inlined_html = p.transform()

That way, all feature requests, like this issue, would be relatively taken care of.

Would you be interested in adding a feature like this @dov ?

@peterbe
Copy link
Owner

peterbe commented Mar 19, 2019

Due to organic slow growth the premailer API has evolved quite a bit. It used to be that you do:

p = premailer.Premailer(html_string)
print(p.transform())

But, if you have many different html_strings you can instead share 1 instance for all, like:

p = premailer.Premailer(**my_awesome_options)
for html_string in html_strings:
    print(p.transform(html_string))

So it's not a dead obvious place where to put the preprocessing. But it could be like this:

def my_mutating_pre_processor(instance):
    assert instance is p  # just for demonstration
    for img in instance.cssselect('img.thumbnail'):
        fn = uri_to_local_file_path(img.attr('src'))
        b64 = base64.b64encode(open(fn, "rb").read())
        img.attr('src', f'data:image/png;base64,{b64}')

p = premailer.Premailer(pre_processor=my_mutating_pre_processor, **my_awesome_options)
for html_string in html_strings:
    print(p.transform(html_string))

Let me know if you're interested in writing a patch. I can help with running tests and design.

@dov
Copy link
Author

dov commented Mar 19, 2019

This is indeed my intention, and your suggestion for an API through the pre_processor function certainly makes sense. If I start working on a patch for this, I'll let you know.

@23tux
Copy link

23tux commented Apr 2, 2022

Is there any news on this?

@peterbe
Copy link
Owner

peterbe commented Apr 4, 2022

Is there any news on this?

Not that I know. I still stand by my 2019 comment above I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants