简体   繁体   English

Node.js / AngularJS:将图像文件缩小到特定的文件大小

[英]Node.js/AngularJS: Shrink image files to specific file size

Using a MEAN environment, I would like to automatically shrink image files (.gif/.png/.jpg) which are uploaded by websites users, to a specific file size (eg 2 MB). 使用MEAN环境,我想将网站用户上传的图像文件(.gif / .png / .jpg)自动缩小到特定的文件大小(例如2 MB)。 Is there any node.js module available, enabling me to use something like this: 是否有可用的node.js模块,使我能够使用以下代码:

SomeModule.shrinkImage('MyHugeImage.jpg','2MB')
.then()
[...]

Apart from graphicsmagick minify function (where I have to calculate the minify factor on-the-fly based on the desired file size), I haven't found anything out-of-the box yet. 除了graphicsmagick的minify函数(我必须根据所需的文件大小即时计算出最小化因子)之外,我还没有发现任何现成的东西。 May happen on client (using AngularJS) or server-side (Node.js module), I don't care. 可能在客户端(使用AngularJS)或服务器端(Node.js模块)上发生,我不在乎。 Suggestions? 有什么建议吗?

I think ng-image-resize can help you. 我认为ng-image-resize可以为您提供帮助。

  • Add this module to your project with bower or npm. 使用bower或npm将此模块添加到您的项目中。

    eg: bower install angular-images-resizer. 例如:Bower安装angular-images-resizer。

  • Include it in your app. 将其包含在您的应用中。

    angular.module('app', ['images-resizer']); angular.module('app',['images-resizer']);

  • Then simply add the service to your code and start resizing your images : 然后只需将服务添加到您的代码中并开始调整图像大小即可:

    angular.module('app', function ($document, $log, $scope, resizeService) { resizeService .resizeImage('resources/imageToResize', { size: 100, sizeScale: 'ko' // Other options ... }) .then(function(image){
    // Add the resized image into the body var imageResized = document.createElement('img'); imageResized.src = image; $document[0].querySelector('body').appendChild(imageResized); }) .catch($log.error); // Always catch a promise :)

Please visit https://github.com/FBerthelot/angular-images-resizer for more details. 请访问https://github.com/FBerthelot/angular-images-resizer了解更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM