【问题标题】:Facebook Share Data URI ImageFacebook 共享数据 URI 图像
【发布时间】:2014-11-10 12:14:42
【问题描述】:

我有一个应用程序,用户可以在其中以交互方式在 HTML5 画布上设计元素。我想通过 Facebook 分享画布图像。

我正计划创建一个动态页面并传入一个数据 URI,但 Facebook 不接受数据 URI 图像并且需要绝对图像路径。

我真的不想走在服务器上存储图像的道路上,即使是暂时的,但恐怕这是我唯一的选择?我应该研究其他途径吗?

【问题讨论】:

    标签: html facebook facebook-graph-api html5-canvas data-uri


    【解决方案1】:

    我找到了一些不错的代码,看起来不错。你也可以在"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');
            }
        });
    }
    

    【讨论】:

    • 感谢分享此代码! :) 我现在就试试!
    • Publish_actions 权限已于 2018 年 8 月 1 日被删除。:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多