标签:服务器 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
see the GraphicsMagick docs for detailsvar 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
标签:服务器 mic 代码 fit 12px var auto ima anti
原文地址:http://www.cnblogs.com/chenjianxiang/p/6279099.html