Node.js 使用gm處理圖像


現要對之前的文件服務器進行擴展,聽網上說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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM