簡體   English   中英

base 64 og:將圖片分享到Facebook

[英]base 64 og:image to facebook share

我正在使用此將HTML轉換為畫布。 然后,我使用以下代碼從畫布上獲取圖像:

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

結果,我得到了base64圖像。 在Facebook上分享帖子時,我需要使用此圖像。 我知道我需要添加一個元標記og:image並將og:image插入那里。 但是它不想接受base64圖像。 誰能建議我該怎么做?

請幫忙,謝謝。

我以這種方式解決了這個問題:

首先,我將HTML轉換為畫布,然后將畫布轉換為圖像,然后使用畫布繪制此圖像,以裁剪初始圖像以消除多余的空間。 完成此操作后,圖像將發送到服務器進行存儲,並通過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