码迷,mamicode.com
首页 > Web开发 > 详细

Node.js 使用gm处理图像

时间:2017-01-12 18:40:32      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:服务器   mic   代码   fit   12px   var   auto   ima   anti   

现要对之前的文件服务器进行扩展,听网上说gm处理图像来一套一套的。so决定使用该工具去实现文件服务器的图片处理这块。目标有下

现在通过参数去获得缩略图
http://xxx.xxx.com/image/2f696d6167652f75706c6f61642f323031362f31302f31302f313030/0/w/100/h/2100/12/format/jpg/q/75

后面几个参数顺序可以调换如:
http://xxx.xxx.com/image/2f696d6167652f75706c6f61642f323031362f31302f31302f313030/0/format/jpg/q/75/w/100/h/2100
http://xxx.xxx.com/image/2f696d6167652f75706c6f61642f323031362f31302f31302f313030/0/format/jpg/q/75/w/100/h/2100

接口规格
http://file.ttyouni.com/image/hex编码/<mode>/w/<width>/h/<height>/format/<Format>/q/<Quality>

mode参数说明
0:指定高宽缩放(可能变形)
1:指定宽,高按比例(但是如果原图的宽比给定的小的话,则不进行压缩)
2:指定高,宽按比例(但是如果原图的高比给定的小的话,则不进行压缩)
3:指定高宽裁减(不变形)
4:固定(可视为第一种的智能版),将图片按照原始图片的比例为了适应进行按比例缩小,若显示区域偏大则保持原样

其它参数说明
format:新图的输出格式 取值范围:jpg,gif,png,默认为原图格式
q:新图的图片质量 取值范围是[1, 100],默认75。
w: 目标图片的宽度
h:目标图片的高度

接下来是工具。
下载地址:https://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.25/
文档地址:http://aheckmann.github.io/gm/docs.html

这里的resize的%,@,<,> 符号都没看懂了。希望有缘人解释。 see the GraphicsMagick docs for details
很多方法也没看懂,应该是走了很多复杂的路,贴代码如下,希望路过的指点一二
var fs = require(‘fs‘)
    , gm = require(‘gm‘);

var imageParameter = require(‘../models/ImageParameter‘);

exports.createImage = function (oriUrl,descUrl,obj,successFn) {
    var outputImage = gm(oriUrl);
    var m = imageParameter.getMode();
    outputImage.size(function(err, ori) {
        //设置图片质量格式
        outputImage = outputImage.quality(obj.quantity).setFormat(obj.format);
        //调整尺寸
        switch(obj.mode){
            case m.HW://指定高宽缩放(可能变形)
                outputImage = outputImage.resize(obj.width, obj.height,"!");
                break;
            case m.W://指定宽,高按比例
                if(obj.width >= ori.width){
                    outputImage = outputImage.resize(ori.width, null);
                }else{
                    outputImage = outputImage.resize(obj.width, null);
                }
                break;
            case m.H://指定高,宽按比例
                if(obj.height >= ori.height){
                    outputImage = outputImage.resize(null, ori.height);
                }else{
                    outputImage = outputImage.resize(null, obj.height);
                }
                break;
            case m.CUT://裁剪(不变形)
                var toHeight = ori.width;
                var toWidth = ori.height;
                var x = 0;
                var y = 0;
                if(ori.width >= obj.width && ori.height >= obj.height){
                    //以高为基准
                    if(ori.width/ori.height >= obj.width/obj.height){
                       toHeight = ori.height;
                       toWidth  = ori.height * obj.width / obj.height;
                       x= (ori.width -toWidth)/2;
                       y=0;
                    }else{
                        toWidth  = ori.width;
                        toHeight = ori.width * obj.height / obj.width;
                        x= 0;
                        y= (ori.height -toHeight)/2;
                    }
                }else{
                    x = (ori.width - obj.width)/2;
                    y = (ori.height - obj.height)/2;
                    toWidth = obj.width;
                    toHeight = obj.height;
                }
                outputImage= outputImage.crop(toWidth,toHeight, x, y);
                break;
            case m.FIT://固定
                if(obj.width >= ori.width || obj.height >= ori.height){
                    outputImage = outputImage.resize(ori.width, ori.height);
                }else{
                    if(obj.width/ori.width > obj.height/ori.height){
                        outputImage = outputImage.resize(obj.width, null);
                    }else{
                        outputImage = outputImage.resize(null, obj.height);
                    }
                }
                break;
            default:
                break;
        }
        //写入图片
        outputImage.write(descUrl, function (err) {
            if (!err){
                successFn();
            }
        });
        }
    );

}

Nodejs文件服务器
http://www.cnblogs.com/chenjianxiang/p/5963011.html

Node.js 使用gm处理图像

标签:服务器   mic   代码   fit   12px   var   auto   ima   anti   

原文地址:http://www.cnblogs.com/chenjianxiang/p/6279099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!