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

Binary image display in template #12

Closed
th3r00t opened this issue Nov 17, 2019 · 2 comments
Closed

Binary image display in template #12

th3r00t opened this issue Nov 17, 2019 · 2 comments

Comments

@th3r00t
Copy link
Owner

th3r00t commented Nov 17, 2019

Having some trouble rendering the bin data stored in the books table, cover column. I have created a custom filter, bin_2_img, and it seems to return the binary I'm looking for. The template is including the the returned data, in the requisite img src tag. however the image is still not rendering.

Merits of db stored images

One should endeavor not to store images in a database, instead storing a path

Of this I am very aware, however for the time being the images are ideally originally stored in side each epubs file. Epub files are semi-analogous to zip files. In order to minimize repeatedly opening and querying the file each time the image is needed I chose to store it instead.

Future releases will most likely be changed to extract the image prior to storing the book and renaming it to match the book title, or file name then storing it in the directory of the book, and a link to the new file in the db instead.

@th3r00t th3r00t created this issue from a note in pyShelf Ebook Server (In progress) Nov 17, 2019
@th3r00t
Copy link
Owner Author

th3r00t commented Nov 17, 2019

frontend/interface/templates/index.html

{% for book in Books %}
    <li>{{ book.title }} <img alt="Embedded Image" src="data:image;base64,{{ book.cover | bin_2_img }}" style="width:50px;height:50px;"/> </li>
{% endfor %}

I have also tried specifying the exact data type as both image/png, and image/jpeg

frontend/interface/templatetags/filters.py

from django import template
register = template.Library()
@register.filter
def bin_2_img(_bin):
    if _bin is not None: return b64encode(_bin)

@th3r00t
Copy link
Owner Author

th3r00t commented Nov 17, 2019

needed to decode the b64encode back to a string for the template to render it correctly. Stackoverflow

if _bin is not None: return b64encode(_bin).decode('utf-8)

@th3r00t th3r00t closed this as completed Nov 17, 2019
pyShelf Ebook Server automation moved this from In progress to Done Nov 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

1 participant