【问题标题】:Resize image with Angular-base-64使用 Angular-base-64 调整图像大小
【发布时间】:2018-02-12 15:11:59
【问题描述】:

我是使用 AngularJs 开发的新手,我有一个使用 Angular-Base64-Upload 将图像转换为 base 64 的功能,我想知道如何将图像调整为大约 500 像素的标准尺寸。

   $scope.upload = function (file) {
  Upload.base64DataUrl(file).then(
    function (url){
      $scope.data['usu_foto'] = url;
    });
};

【问题讨论】:

标签: javascript angularjs


【解决方案1】:
app.controller('ctrl', function ($scope, $q) {

  $scope.resizeImage = function ( file, base64_object ) {
    // file is an instance of File constructor.
    // base64_object is an object that contains compiled base64 image data from file.
    var deferred = $q.defer();
    var url = URL.createObjectURL(file);// creates url for file object.
    Jimp.read(url)
    .then(function (item) {
      item
      .resize(1280, Jimp.AUTO)// width of 1280px, auto-adjusted height
      .quality(50)//drops the image quality to 50%
      .getBase64(file.type, function (err, newBase64) {
        if (err) {throw err;}
        var bytes = Math.round((3/4)*newBase64.length);
        base64Object.filetype = file.type;
        base64Object.filesize = bytes;
        base64Object.base64 = newBase64;
        // Note that base64 in this package doesn't contain "data:image/jpeg;base64," part,
        // while base64 string from Jimp does. It should be taken care of in back-end side.
        deferred.resolve(base64Object);
      });
    })
    .catch(function (err) {
      return console.log(err);// error handling
    });
    return deferred.promise;
  };

});

基本上你可以只使用调整大小功能。 更多细节可以在这里找到:https://github.com/adonespitogo/angular-base64-upload

【讨论】:

    猜你喜欢
    • 2014-01-24
    • 2014-10-14
    • 2015-05-02
    • 2011-09-07
    • 2021-06-25
    • 2012-02-28
    • 2013-08-21
    • 2012-08-09
    • 2014-12-10
    相关资源
    最近更新 更多