使用Angular-base-64调整图像大小

时间:2017-09-04 11:28:40

标签: javascript angularjs

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

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

1 个答案:

答案 0 :(得分:0)

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