使用Nodejs和Imagemagick调整图像大小 [英] Resizing images with Nodejs and Imagemagick

查看:134
本文介绍了使用Nodejs和Imagemagick调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用nodejs和imagemagick能够调整图像的大小并将其发送到浏览器.

Using nodejs and imagemagick am able to re-size an image and send it to the browser with this.

    var http = require('http'),
        spawn = require('child_process').spawn;

    http.createServer(function(req, res) {

        var image = 'test.jpg';
        var convert = spawn('convert', [image, '-resize', '100x100', '-']);

        convert.stdout.pipe(res);
        convert.stderr.pipe(process.stderr);

    }).listen(8080);

从文件系统中读取测试图像,我想对其进行更改,以使测试图像为二进制字符串.

The test image is read from the file-system, I want to alter so that test image is a binary string.

var image = 'some long binray string representing an image.......';

我的计划是将二进制字符串存储在Mongodb中,并动态读取它们.

My plan is to store the binary strings in Mongodb and read them of dynamically.

推荐答案

由于您使用的是 spawn()来调用ImageMagick命令行 convert ,因此通常的方法是将中间文件写入临时目录,这些临时文件在使用后或作为计划的/cron作业将被立即清理.

Since you are using spawn() to invoke the ImageMagick command line convert, the normal approach is to write intermediate files to a temp directory where they will get cleaned up either immediately after use or as a scheduled/cron job.

如果要避免编写文件进行转换,可以尝试的一种方法是对图像进行base64编码,并使用内联格式.这类似于在某些HTML电子邮件或网页中对图像进行编码的方式.

If you want to avoid writing the file to convert, one option to try is base64 encoding your images and using the inline format. This is similar to how images are encoded in some HTML emails or web pages.

 inline:{base64_file|data:base64_data}
 Inline images let you read an image defined in a special base64 encoding.

注意::您可以传递的命令行选项的大小是有限制的.Imagemagick文档建议使用5000个字节.Base64编码的字符串比原始字符串大(Wikipedia建议粗略的指南大137%),除非您显示缩略图,否则这可能是非常有限的.

NOTE: There is a limit on the size of command-line options you can pass .. Imagemagick docs suggest 5000 bytes. Base64-encoded strings are larger than the original (Wikipedia suggests a rough guide of 137% larger) which could be very limiting unless you're showing thumbnails.

另一个ImageMagick格式选项是临时:

Another ImageMagick format option is ephemeral:

 ephemeral:{image_file}
 Read and then Delete this image file.

如果要完全避免I/O传递,则需要一个Node.js模块,该模块直接集成ImageMagick或GD等低级库,而不是包装命令行工具.

If you want to avoid the I/O passing altogether, you would need a Node.js module that directly integrates a low-level library like ImageMagick or GD rather than wrapping command line tools.

这篇关于使用Nodejs和Imagemagick调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆