【问题标题】:Fabric js load image from data uri coming from another urlFabric js从来自另一个url的数据uri加载图像
【发布时间】:2015-10-05 07:24:44
【问题描述】:

我知道如何将图像从数据 uri 加载到结构 js 中。但是如果数据 uri 来自另一个 url,我无法弄清楚如何加载图像。我有一个接受图像 URL 并返回 base64 图像数据的 PHP 脚本。

<?php
    $path = $_REQUEST['url'];
    $type = pathinfo($path, PATHINFO_EXTENSION);
    $data = file_get_contents($path);
    $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
    echo $base64;
?>

假设网址是http://example.com/encodeImage.php?url=http://example.com/image.png。我正在尝试将此 URL 用于此功能

fabric.Image.fromURL('http://example.com/encodeImage.php?url=http://example.com/image.png', function(img) {

但它无法加载图像。在控制台中显示unable to load the image

有没有可能用fabric js做点什么?

【问题讨论】:

    标签: javascript php canvas html5-canvas fabricjs


    【解决方案1】:

    这样添加图片:

    var img = new Image();
        img.onload = function () {
            var imgbase64 = new fabric.Image(img, {
                scaleX: 0.2,
                scaleY: 0.2
            })
            canvas.add(imgbase64);
            canvas.deactivateAll().renderAll();
        }
    
    $.ajax({url: "http://example.com/encodeImage.php?url=http://example.com/image.png", success: function(result){
            img.src = result;
        }});
    

    【讨论】:

      【解决方案2】:
      fabric.Image.fromURL(url, function (img, callback) {
          img.id = imageId;
          img.filters = [new fabric.Image.filters.HueRotation()];
          img.applyFilters()
          var cor = img.set(
              {
                  left: left,
                  top: top,
                  selectable: false,
              }
          )
      
          canvas.add(img);
      });
      

      【讨论】:

      • 你能解释一下这段代码在做什么吗?而且它看起来可能不完整,因为它缺少最后的右括号
      猜你喜欢
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 2017-06-11
      • 1970-01-01
      • 2014-03-13
      • 2018-05-10
      相关资源
      最近更新 更多