现要对之前的文件服务器进行扩展,听网上说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处理图像的更多相关文章

  1. Node.js 引用 gm 包错误 Error: Could not execute GraphicsMagick/ImageMagick

    今天在学习前后台图像剪切时,下载了有图片剪切瑞士军刀之称的 GraphicsMagick. 给 gm.exe 配置了环境变量,在 npm 下好了 gm 的模块,但是运行却出现了错误. 错误如图: [E ...

  2. 玩node-images模块---Node.js轻量级跨平台图像编解码库

    Node.js轻量级跨平台图像编解码库 github:https://github.com/zhangyuanwei/node-images Features 功能特性 轻量级:无需安装任何图像处理库 ...

  3. Node.js:Buffer浅谈

    Javascript在客户端对于unicode编码的数据操作支持非常友好,但是对二进制数据的处理就不尽人意.Node.js为了能够处理二进制数据或非unicode编码的数据,便设计了Buffer类,该 ...

  4. 用node.js给图片加水印

    一.准备工作: 首先,确保你本地已经安装好了node环境.然后,我们进行图像编辑操作需要用到一个Node.js的库:images.这个库的地址是:https://github.com/zhangyua ...

  5. Node.js与Sails~自定义响应体responses

    回到目录 在Node.js里,你可以控制请求和响应,自己可以定义自己的响应方式,如对文本如何响应,对json如何响应,对图像流如何响应等等,而这些在Sails架构里,变得更加容易和清晰了,它位于项目的 ...

  6. [转]为什么我要用 Node.js? 案例逐一介绍

    原文地址:http://blog.jobbole.com/53736/ 介绍 JavaScript 高涨的人气带来了很多变化,以至于如今使用其进行网络开发的形式也变得截然不同了.就如同在浏览器中一样, ...

  7. 为什么要使用 Node.js

    这是一个移动端工程师涉足前端和后端开发的学习笔记,如有错误或理解不到位的地方,万望指正. Node.js 是什么 传统意义上的 JavaScript 运行在浏览器上,这是因为浏览器内核实际上分为两个部 ...

  8. 【转】为什么我要用 Node.js? 案例逐一介绍

    原文转自:http://blog.jobbole.com/53736/ 介绍 JavaScript 高涨的人气带来了很多变化,以至于如今使用其进行网络开发的形式也变得截然不同了.就如同在浏览器中一样, ...

  9. Node.js Express框架

    Express 介绍 Express是一个最小的,灵活的Node.js Web应用程序框架,它提供了一套强大的功能来开发Web和移动应用程序. 它有助于基于Node Web应用程序的快速开发.下面是一 ...

随机推荐

  1. Spring IOC 之个性化定制the nature of a bean

    1.生命周期回调 为了影响容器管理的bean的生命周期,你可以实现Spring的InitializingBean和DisposableBean接口.容器首先调用afterPropertiesSet() ...

  2. Add GUI to connect to SQL

    (*********************************************************************************) (* *) (* Below i ...

  3. Byte[]、Image、Bitmap 之间的相互转换

    原文:Byte[].Image.Bitmap 之间的相互转换 /// <summary>        /// 将图片Image转换成Byte[]        /// </summ ...

  4. Windows 下让 Python 多个版本共存(支持 pip)

    转载自 http://blog.kgzx.net/index.php/archives/40/ 因为类库兼容性的关系,写实际项目时都是用 Python 2,但自己试验性的写点小东西还是喜欢用 Pyth ...

  5. java数字字符串累加1的解决方案

    近期操作项目遇到这样的问题,研究了下搞出了一个解决方案. //num也可以是在数字字符串里面截取的,比如我有14位的数字字符串前六位是市级,7,8位代表县区,后两位代表乡镇,最后四位是累计+1的,这个 ...

  6. Java、C#双语版HttpHelper类

    Java.C#双语版HttpHelper类(解决网页抓取乱码问题)   在做一些需要抓取网页的项目时,经常性的遇到乱码问题.最省事的做法是去需要抓取的网站看看具体是什么编码,然后采用正确的编码进行解码 ...

  7. 7.29 DFS总结

    7.29   黄昏时刻 (一) 全排列 建模: 给了数字n 代表从1-n 个数全排列 思路: 1. 输入n,如果n值为‘0’,则退出程序 2. vis[i] 保存 是否对第i个数字进行访问 3. df ...

  8. c++ virtual function 虚函数面试题

    下面的代码输出什么? #include<iostream> using namespace std; class A { public: virtual void foo() { cout ...

  9. RSA加密解密与签名验证

    关于RSACryption帮助类定义见RSACryption 一.加密与解密 //定义明文和密文变量 string plaintext = "天道酬勤,厚德载物!"; string ...

  10. DedeCms密码解密[转]

    dede 的密码怎么破解,dede后台.32位的DEDE密码如何破解 dede 的密码是32位MD5减去头5位,减去尾七位,得到20 MD5密码,方法是,前减3后减1,得到16位MD5. 比如我的数据 ...