How to display an image which saved as base64 encoded string?

21,921 views
Skip to first unread message

Zhang Huangbin

unread,
Jun 8, 2009, 10:35:26 PM6/8/09
to we...@googlegroups.com
Hi, list.

How can i display an image wich saved as base64 encoded string?
It's store in 'jpegPhoto' attribute in LDAP.

I can display it with <img> tag directly like below:

<img src="data:image/jpeg;base64,***BASE64 ENCODED STRING HERE***" />

But IE doesn't support this.

I try to use web.header(), but failed:

web.header("Content-Type","image/jpeg")
***BASE64 ENCODED STRING HERE***

--
Best regards.

Zhang Huangbin

- Open Source Mail Server Solution for RHEL, CentOS,
Debian, Ubuntu:
+ Homepage: http://code.google.com/p/iredmail/
+ Forum: http://www.iredmail.org/forum/

Frank Smit

unread,
Jun 9, 2009, 3:53:15 AM6/9/09
to we...@googlegroups.com
If you're going to display it with "Content-Type: image/jpeg" you'll have to decode the base64 encoded string first.

2009/6/9 Zhang Huangbin <michae...@gmail.com>

Zhang Huangbin

unread,
Jun 9, 2009, 4:51:11 AM6/9/09
to web.py


On Jun 9, 3:53 pm, Frank Smit <61924...@gmail.com> wrote:
> If you're going to display it with "Content-Type: image/jpeg" you'll have to
> decode the base64 encoded string first.

It displays a blank page while i use web.header('Content-Type', 'image/
jpeg').

Any example?

Frank Smit

unread,
Jun 9, 2009, 6:24:13 AM6/9/09
to we...@googlegroups.com
I don't know how it works with Python yet, but here's an example in PHP.

<?php
header('Content-Type: image/png');
echo base64_decode($base64_encoded_str);
?>

I think it would work the same with Python. Just decode the string and print it to the browser.

2009/6/9 Zhang Huangbin <michae...@gmail.com>

Jiahua Huang

unread,
Jun 9, 2009, 7:15:58 AM6/9/09
to we...@googlegroups.com
On Tue, Jun 9, 2009 at 4:51 PM, Zhang Huangbin<michae...@gmail.com> wrote:
> Any example?
>

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import web

urls = (
"/", "index",
"/img", "img",
)

app = web.application(urls, globals())

class index:
def GET(self):
return "<img src='/img' />"
pass

class img:
def GET(self):
web.header('Content-Type', 'image/png')
return "***BASE64 ENCODED STRING HERE***"
pass

if __name__ == "__main__":
app.run()

Tzury Bar Yochay

unread,
Jun 9, 2009, 8:44:40 AM6/9/09
to web.py
make sure to add "data:image/gif;base64," before the data it self
as in
<img
src="data:image/gif;base64,R0lGODlhUAAPAKIAAAsLav///
88PD9WqsYmApmZmZtZfYmdakyH5BAQUAP8ALAAAAABQAA8AAAPb
WLrc/jDKSVe4OOvNu/
9gqARDSRBHegyGMahqO4R0bQcjIQ8E4BMCQc930JluyGRmdAAcdiigMLVr
ApTYWy5FKM1IQe+Mp+L4rphz
+qIOBAUYeCY4p2tGrJZeH9y79mZsawFoaIRxF3JyiYxuHiMGb5KT
kpFvZj4ZbYeCiXaOiKBwnxh4fnt9e3ktgZyHhrChinONs3cFAShFF2JhvCZlG5uchYNun5eedRxM
AF15XEFRXgZWWdciuM8GCmdSQ84lLQfY5R14wDB5Lyon4ubwS7jx9NcV9/j5+g4JADs="
alt="British Blog Directory" width="80" height="15" />

On Jun 9, 2:15 pm, Jiahua Huang <jhuangjia...@gmail.com> wrote:

Zhang Huangbin

unread,
Jun 9, 2009, 8:52:14 AM6/9/09
to web.py


On Jun 9, 7:15 pm, Jiahua Huang <jhuangjia...@gmail.com> wrote:

> class img:
>     def GET(self):
>         web.header('Content-Type', 'image/png')
>         return "***BASE64 ENCODED STRING HERE***"

Solved. but returned should be docoded.
Thanks jiahua :)

buyi wen

unread,
Sep 24, 2015, 11:14:08 PM9/24/15
to web.py
you can try this free online service to convert base64 to image online(http://www.online-code.net/base64-image.html).

diepth...@gmail.com

unread,
Oct 4, 2015, 1:10:34 PM10/4/15
to web.py
thanks very much PHP Obfuscator Online
Reply all
Reply to author
Forward
0 new messages