简体   繁体   English

base 64 og:将图片分享到Facebook

[英]base 64 og:image to facebook share

I am using this library to convert HTML to canvas. 我正在使用此将HTML转换为画布。 Then I am using the following code to get an image out of canvas: 然后,我使用以下代码从画布上获取图像:

canvas = document.getElementsByTagName('canvas')[0] context = canvas.getContext('2d') image = new Image() image.src = canvas.toDataURL("image/png")

As a result I get a base64 image. 结果,我得到了base64图像。 I need to use this image when sharing a post to Facebook. 在Facebook上分享帖子时,我需要使用此图像。 I know I need to add a meta tag og:image and insert the image there. 我知道我需要添加一个元标记og:image并将og:image插入那里。 But it doesn't want to accept a base64 image. 但是它不想接受base64图像。 Can anyone advise how I can do this? 谁能建议我该怎么做?

Please help and thanks. 请帮忙,谢谢。

I solved this problem in such a way: 我以这种方式解决了这个问题:

At first I convert HTML to canvas, then canvas to image and afterwards this image is drawn with canvas for cropping the initial image to get rid of extra space. 首先,我将HTML转换为画布,然后将画布转换为图像,然后使用画布绘制此图像,以裁剪初始图像以消除多余的空间。 After this is done, the image is sent to the server for storage and passed to FB in og:image meta tag. 完成此操作后,图像将发送到服务器进行存储,并通过og:image元标记传递到FB。

$scope.facebookShared = () ->

      $window.open "//www.facebook.com/sharer/sharer.php?u=" + encodeURI($location.absUrl()), "sharer", "toolbar=0,status=0,width=" + 500 + ",height=" + 500 /// this is window for share fb

      height=$('.sharing').height()
      html2canvas document.body,
        onrendered: (canvas) ->

          context = canvas.getContext('2d')
          image = new Image()
          image.src = canvas.toDataURL("image/png")
          image.onload = ->
            sharing=$('.sharing')
            canvas.height = Math.round(sharing.width()/1.91)
            canvas.width = sharing.width()

            context=canvas.getContext('2d')

            pos =  sharing.parent(0).parent(0).position()
            context.drawImage(this, pos.left, pos.top, sharing.width() + 20, sharing.height(),0,0,sharing.width()+20,sharing.height())

            $.ajax
              url: '../../save_img'
              type: 'post'
              beforeSend: (xhr) ->
                xhr.setRequestHeader "X-CSRF-Token", $("meta[name=\"csrf-token\"]").attr("content")
                return
              data:
                base64_image: canvas.toDataURL("image/png").replace(/^data:image\/(png|jpg);base64,/, "")
                claim_slug: $scope.claim.slug

            return false

        width: $('.claim-page ').width()
        height: height+115

      return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM