Maher Elaissi
Maher Elaissi

Reputation: 35

How can I display an image out of a document's attachments in CouchDB?

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

Answers (3)

Gjorgji Tashkovski
Gjorgji Tashkovski

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

Octavian Helm
Octavian Helm

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

Sergey
Sergey

Reputation: 5207

You can store converted png-(jpg/gif) image in BLOB field

<img src="data:image/png;base64,<?php echo $base64PNGImage ?>">

Upvotes: 0

Related Questions