在 ImageMagick 中通过 Node.JS 使用远程图像

发布于 2024-11-02 07:34:54 字数 194 浏览 7 评论 0原文

如何在 Node.JS 中使用 ImageMagick 中的远程图像?

我想实现类似的目标:

im.identify('http://www.website.com/image.jpg', function(error, features) {
    console.log(features);
});

How can I use remote image in ImageMagick with Node.JS?

I want to achieve something like:

im.identify('http://www.website.com/image.jpg', function(error, features) {
    console.log(features);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

驱逐舰岛风号 2024-11-09 07:34:54

图像大小调整的快速代码片段

http://nodejs.org/api/http .html
https://github.com/rsms/node-imagemagick

var thumb = '';

...

var request = http.get(options, function(response) {

    var data = '';

    response.setEncoding('binary');

    response.on('data', function(chunk) {
        data += chunk;
    });

    response.on('end', function () {

        im.resize({
            srcData: data,
            width: 100,
            height: 75,
            format: 'jpg'
        }, function(err, stdout, stderr) {
            if (err) throw err;
            thumb = stdout;
        });
    }

});

A quick code snippet of image resizing

http://nodejs.org/api/http.html
https://github.com/rsms/node-imagemagick

var thumb = '';

...

var request = http.get(options, function(response) {

    var data = '';

    response.setEncoding('binary');

    response.on('data', function(chunk) {
        data += chunk;
    });

    response.on('end', function () {

        im.resize({
            srcData: data,
            width: 100,
            height: 75,
            format: 'jpg'
        }, function(err, stdout, stderr) {
            if (err) throw err;
            thumb = stdout;
        });
    }

});
樱花落人离去 2024-11-09 07:34:54

这就是我使用远程图像的方式:

var srcUrl = 'http://domain.com/path/to/image.jpg';
var http = srcUrl.charAt(4) == 's' ? require("https") : require("http");
var url = require("url");

http.get(url.parse(srcUrl), function(res) {

    if(res.statusCode !== 200) {
        throw 'statusCode returned: ' + res.statusCode;
    }
    else {
        var data = new Array;
        var dataLen = 0;

        res.on("data", function (chunk) {
            data.push(chunk);
            dataLen += chunk.length;
        });

        res.on("end", function () {
            var buf = new Buffer(dataLen);
            for(var i=0,len=data.length,pos=0; i<len; i++) {
                data[i].copy(buf, pos);
                pos += data[i].length;
            }

            im(buf).imFunctionYouWantToUse();
        });
    }
});

请访问 https://stuk.github。 io/jszip/documentation/howto/read_zip.html

This is how I use remote images:

var srcUrl = 'http://domain.com/path/to/image.jpg';
var http = srcUrl.charAt(4) == 's' ? require("https") : require("http");
var url = require("url");

http.get(url.parse(srcUrl), function(res) {

    if(res.statusCode !== 200) {
        throw 'statusCode returned: ' + res.statusCode;
    }
    else {
        var data = new Array;
        var dataLen = 0;

        res.on("data", function (chunk) {
            data.push(chunk);
            dataLen += chunk.length;
        });

        res.on("end", function () {
            var buf = new Buffer(dataLen);
            for(var i=0,len=data.length,pos=0; i<len; i++) {
                data[i].copy(buf, pos);
                pos += data[i].length;
            }

            im(buf).imFunctionYouWantToUse();
        });
    }
});

Credit go to https://stuk.github.io/jszip/documentation/howto/read_zip.html

北凤男飞 2024-11-09 07:34:54

很难说我是否正确理解了你(考虑到你在这里发布的信息量)。

使用 imagemagick 对远程图像执行操作的唯一方法是先将其下载到本地服务器。这可以使用 http.ClientRequest 类来完成node.js,之后您应该能够像往常一样使用 Imagemagick 对图像进行操作。

It's hard to say if i understood you correctly (considering the amount of information you posted here).

The only way you can perform operations on a remote image using imagemagick is to download it to the local server first. This can be done using the http.ClientRequest class of node.js, afterwards you should be able to operate on the image as usual using Imagemagick.

简单 2024-11-09 07:34:54

这应该有效:

var request = require('request');
var fs = require('fs');

request({
        'url': 'http://www.website.com/image.jpg',
        'encoding':'binary'
    }, 
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            fs.writeFileSync('/mylocalpath/image.jpg', body, 'binary');
            im.identify('/mylocalpath/image.jpg',
                function(error, features) {
                    console.log(features);
                 }
            );
        }else{
            console.error(error, response);
        }
    }
)

This should work:

var request = require('request');
var fs = require('fs');

request({
        'url': 'http://www.website.com/image.jpg',
        'encoding':'binary'
    }, 
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            fs.writeFileSync('/mylocalpath/image.jpg', body, 'binary');
            im.identify('/mylocalpath/image.jpg',
                function(error, features) {
                    console.log(features);
                 }
            );
        }else{
            console.error(error, response);
        }
    }
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文