Reputation: 35
I'm using CouchDB for storing images and I'd like to know how to get an image from that CouchDD document to display it in HTML page.
like:
<img src= ".....">
Are there any code examples I can follow?
Upvotes: 1
Views: 1357
Reputation: 1983
If you are storing the images as an attachment and you want to retrieve image for a certain key (let's say some user) do it this way:
view data-user-avatar: map:
function(doc) {
if (doc.doc_type === "config" && doc.config_type === "user") {
emit(doc.username, doc._attachments);
}
}
list list-user-avatar:
function(head, req) {
start({
"headers": {
"Content-Type": "text/html"
}
});
var row;
while(row = getRow()) {
for (var attachment_key in row.value) {
send('<img src="/' + req.query.database + '/' + row.id + '/' + attachment_key + '" style="border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; border-top-left-radius: 5px; border-top-right-radius: 5px;">');
}
}
}
HTTP call in javascript:
'http://localhost:5984/db/_design/db/_list/list-user-avatar/data-user-avatar?key="' + user + '"&database=' + database
Upvotes: 0
Reputation: 39604
If I got you right you just want to display the image right out of the document's attachements object.
Just use the following URI construct:
/database/document-id/image-name.type
Here is a live example.
http://localhost:5984/users/sam/avatar.png
Upvotes: 1
Reputation: 5207
You can store converted png-(jpg/gif) image in BLOB field
<img src="data:image/png;base64,<?php echo $base64PNGImage ?>">
Upvotes: 0