【问题标题】:AngularJS file upload from ngThumb directive (with angular-file-upload)从 ngThumb 指令上传 AngularJS 文件(使用 angular-file-upload)
【发布时间】:2015-09-29 09:46:14
【问题描述】:

我正在使用Angular-File-Upload 将文件上传到服务器。一切正常,文件可以保存在数据库中。

问题是,如何加载我在编辑模式下保存的图像?

这是上传图片时创建canvas的指令

'use strict';

myApp

    .directive('ngThumb', ['$window', function($window) {
        var helper = {
            support: !!($window.FileReader && $window.CanvasRenderingContext2D),
            isFile: function(item) {
                return angular.isObject(item) && item instanceof $window.File;
            },
            isImage: function(file) {
                var type =  '|' + file.type.slice(file.type.lastIndexOf('/') + 1) + '|';
                return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
            }
        };

        return {
            restrict: 'A',
            template: '<canvas/>',
            link: function(scope, element, attributes) {
                if (!helper.support) return;

                var params = scope.$eval(attributes.ngThumb);

                if (!helper.isFile(params.file)) return;
                if (!helper.isImage(params.file)) return;

                var canvas = element.find('canvas');
                var reader = new FileReader();

                reader.onload = onLoadFile;
                reader.readAsDataURL(params.file);

                function onLoadFile(event) {
                    var img = new Image();
                    img.onload = onLoadImage;
                    img.src = event.target.result;
                }

                function onLoadImage() {
                    var width = params.width || this.width / this.height * params.height;
                    var height = params.height || this.height / this.width * params.width;
                    canvas.attr({ width: width, height: height });
                    canvas[0].getContext('2d').drawImage(this, 0, 0, width, height);
                }
            }
        };
    }]);

这是一个 html sn-p 在有上传时加载画布:

<div class="table-responsive"  ng-hide="!uploaderImages.queue.length">
    <table class="table">
        <thead>
            <tr>
            <th width="50%">Name</th>
            <th ng-show="uploaderImages.isHTML5">Size</th>
            <th ng-show="uploaderImages.isHTML5">Progress</th>
            <th>Status</th>
            <th>Actions</th>
            </tr>
        </thead>
        <tbody>
          <tr ng-repeat="item in uploaderImages.queue">
          <td><strong>{{ item.file.name }}</strong>
            <div ng-show="uploaderImages.isHTML5" ng-thumb="{ file: item._file, height: 100 }"></div>
        </td>
        <td ng-show="uploaderImages.isHTML5" nowrap>{{ item.file.size/1024/1024|number:2 }} MB</td>
        <td ng-show="uploaderImages.isHTML5">
<div class="progress progress-xs margin-bottom-0">
<div class="progress-bar" role="progressbar" ng-style="{ 'width': item.progress + '%' }"></div>
</div></td>
<td class="text-center">
    <span ng-show="item.isSuccess"><i class="glyphicon glyphicon-ok"></i></span>
    <span ng-show="item.isCancel"><i class="glyphicon glyphicon-ban-circle"></i></span>
    <span ng-show="item.isError"><i class="glyphicon glyphicon-remove"></i></span>
</td>
<td nowrap>
<button type="button" class="btn btn-danger btn-xs" ng-click="item.remove()">
<span class="glyphicon glyphicon-trash"></span> Remove
</button></td>
</tr>
</tbody>
</table>
</div>

谢谢!!

【问题讨论】:

  • 你能更详细地解释你的问题吗? “加载图片”和“编辑模式”是什么意思?
  • 当您说“加载回”时,您的意思是如何从数据库中检索它们并使其可用于加载到画布中?您如何从数据库中检索图像?
  • 如果其中一个答案是正确的会很有趣吗?
  • 您好,您有如何将图像保存到数据库的源代码吗?我真的很困惑如何上传角度文件??

标签: php angularjs laravel angularjs-directive angular-file-upload


【解决方案1】:

由于上传器已经正常工作并且您可以将图像保存到数据库中,您需要做的就是将上传的图像显示为画布上的缩略图。

这可以使用一些像这样的 jQuery 来完成:

// source of a large image - replace this with the URL of the uploaded image (served from the database)
var IMAGE_SRC = "http://cdn-media-1.lifehack.org/wp-content/files/2014/09/activities-on-the-beach.jpg";

// set the height for the thumbnail - your uploader currently has 100
var height = 100;

function drawImage() {
  // create a new Image object
  var img = new Image();

  // set up the onLoad handler on the image object to draw the thumbnail into the canvas
  img.onload = function() {
    // calculate the thumbnail width for the fixed height above, respecting the image aspect ratio
    var width = this.width / this.height * height;

    // set the dimensions on the canvas
    $("canvas").attr({
      width: width,
      height: height
    });

    // draw the image from the loaded image object
    $("canvas")[0].getContext("2d").drawImage(img, 0, 0, width, height);
  };

  // set the source of the image object to the URL of the uploaded image (served from the database)
  img.src = IMAGE_SRC;
}

// Do all of this when the button is clicked
$("button").click(drawImage);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Load image into Canvas</button>
<br />
<br />
<canvas></canvas>

同样的代码也可以转换成另一个角度指令——比如&lt;uploaded-image&gt;&lt;/uploaded-image&gt;

【讨论】:

    【解决方案2】:

    编写一个简单的指令来预览返回的图像非常容易。或多或少是您提供的ngThumb 指令的简化版本。

    angular.module('components', [])
        .directive('imgPreview', [function () {
        return {
            restrict: 'E',
            template: '<canvas/>',
            replace: true,
            link: function (scope, element, attrs) {
                var myCanvas = element[0];
                var ctx = myCanvas.getContext('2d');
                var img = new Image;
                img.onerror = function () {
                    throw new Error("Image can't be loaded");
                }
                img.onload = function () {
                    myCanvas.width = img.width;
                    myCanvas.height = img.height;
                    ctx.drawImage(img, 0, 0); // Or at whatever offset you like
                };
                img.src = attrs.image;
            }
        }
    }]);
    

    Demo

    【讨论】:

      【解决方案3】:

      // source of a large image - replace this with the URL of the uploaded image (served from the database)
      var IMAGE_SRC = "http://cdn-media-1.lifehack.org/wp-content/files/2014/09/activities-on-the-beach.jpg";
      
      // set the height for the thumbnail - your uploader currently has 100
      var height = 100;
      
      function drawImage() {
        // create a new Image object
        var img = new Image();
      
        // set up the onLoad handler on the image object to draw the thumbnail into the canvas
        img.onload = function() {
          // calculate the thumbnail width for the fixed height above, respecting the image aspect ratio
          var width = this.width / this.height * height;
      
          // set the dimensions on the canvas
          $("canvas").attr({
            width: width,
            height: height
          });
      
          // draw the image from the loaded image object
          $("canvas")[0].getContext("2d").drawImage(img, 0, 0, width, height);
        };
      
        // set the source of the image object to the URL of the uploaded image (served from the database)
        img.src = IMAGE_SRC;
      }
      
      // Do all of this when the button is clicked
      $("button").click(drawImage);
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <button>Load image into Canvas</button>
      <br />
      <br />
      <canvas></canvas>

      【讨论】:

        【解决方案4】:

        如果我正确理解您的问题,那么我认为您需要上传 blob(因此在画布中编辑图像后,您将拥有 Data URI。而您需要将其转换为可以上传的 Blob!

        这是我使用 angular-file-upload 上传裁剪图像的解决方案:

        https://github.com/nervgh/angular-file-upload/issues/208#issuecomment-116344239

        你需要使用

        • uploader.onBeforeUploadItem

        覆盖实际文件 = item._file!

        PS:在给定的链接中还有一个将“DataURI”转换为“Blob”的功能!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-12-01
          • 2016-02-04
          • 1970-01-01
          • 2017-10-20
          • 2014-11-16
          • 2017-02-27
          • 2018-02-06
          相关资源
          最近更新 更多