Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Share Data URI Image

I have an application where users can interactively design elements on a HTML5 canvas. I would like to share the canvas image via Facebook.

I was planning on creating a dynamic page and pass in a Data URI but Facebook does not accept a Data URI image and requires an absolute image path.

I really don't want to head down the path of storing the image on server, even temporarily, but I am afraid that this is my only option? Is there another avenue I should be researching?

like image 502
Gouvi Avatar asked Nov 10 '14 12:11

Gouvi


People also ask

How do you get the url of a picture on Facebook?

Right-click the image and click the option to copy its URL. Depending on the browser you use, the option reads "Copy Image Location," "Copy Image Address," "Copy Link" or "Copy Image URL." In Internet Explorer, click the "Properties" option and copy the URL from the Properties window.

How do I share a Facebook link with a thumbnail?

Right-click inside the "Update Status" box at the top of your Facebook wall and select "Paste" from the context menu. Your link is entered into the status box and the Facebook system automatically scans for a thumbnail image.


1 Answers

I found some nice code for that , looks fine . You can also see who it looks for twiter at "How to post image (canvas) to the facebook , twiter"

// Canvas Object
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

// load image from data url
var imageObj = new Image();
imageObj.onload = function() {
ctx.drawImage(this, 0, 0);
};

imageObj.src = 'panda_dark.png';

function dataURItoBlob(dataURI) {
    var byteString = atob(dataURI.split(',')[1]);
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }
    return new Blob([ab], {type: 'image/png'});
}

$('#shareFB').click(function () {
    var data = $('#canvas')[0].toDataURL("image/png");
    try {
        blob = dataURItoBlob(data);
    } catch (e) {
        console.log(e);
    }
    FB.getLoginStatus(function (response) {
        console.log(response);
        if (response.status === "connected") {
            postImageToFacebook(response.authResponse.accessToken, "Canvas to Facebook/Twitter", "image/png", blob, window.location.href);
        } else if (response.status === "not_authorized") {
            FB.login(function (response) {
                postImageToFacebook(response.authResponse.accessToken, "Canvas to Facebook/Twitter", "image/png", blob, window.location.href);
            }, {scope: "publish_actions"});
        } else {
            FB.login(function (response) {
                postImageToFacebook(response.authResponse.accessToken, "Canvas to Facebook/Twitter", "image/png", blob, window.location.href);
            }, {scope: "publish_actions"});
        }
    });
});


function postImageToFacebook(token, filename, mimeType, imageData, message) {
    var fd = new FormData();
    fd.append("access_token", token);
    fd.append("source", imageData);
    fd.append("no_story", true);

    // Upload image to facebook without story(post to feed)
    $.ajax({
        url: "https://graph.facebook.com/me/photos?access_token=" + token,
        type: "POST",
        data: fd,
        processData: false,
        contentType: false,
        cache: false,
        success: function (data) {
            console.log("success: ", data);

            // Get image source url
            FB.api(
                "/" + data.id + "?fields=images",
                function (response) {
                    if (response && !response.error) {
                        //console.log(response.images[0].source);

                        // Create facebook post using image
                        FB.api(
                            "/me/feed",
                            "POST",
                            {
                                "message": "",
                                "picture": response.images[0].source,
                                "link": window.location.href,
                                "name": 'Look at the cute panda!',
                                "description": message,
                                "privacy": {
                                    value: 'SELF'
                                }
                            },
                            function (response) {
                                if (response && !response.error) {
                                    /* handle the result */
                                    console.log("Posted story to facebook");
                                    console.log(response);
                                }
                            }
                        );
                    }
                }
            );
        },
        error: function (shr, status, data) {
            console.log("error " + data + " Status " + shr.status);
        },
        complete: function (data) {
            //console.log('Post to facebook Complete');
        }
    });
}
like image 182
Nikola Lukic Avatar answered Oct 01 '22 15:10

Nikola Lukic