├── font.ttf ├── sample ├── node.png ├── test.jpg ├── watermark.js ├── captcha.js └── thumb.js ├── package.json ├── .gitignore ├── LICENSE ├── README-CN.md ├── README.md └── index.js /font.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skipify/webimg/HEAD/font.ttf -------------------------------------------------------------------------------- /sample/node.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skipify/webimg/HEAD/sample/node.png -------------------------------------------------------------------------------- /sample/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skipify/webimg/HEAD/sample/test.jpg -------------------------------------------------------------------------------- /sample/watermark.js: -------------------------------------------------------------------------------- 1 | var Webimg = require('../'); 2 | 3 | Webimg('./test.jpg') 4 | .markText('test')//设置水印文字 5 | .markPos(5)//水印位置 123456789 6 | .fontSize(50)//文字水印字体大小 7 | //.font('foot.ttf')//文字水印字体 8 | .fontColor('#ffffff')//字体颜色 9 | .watermark('./mark.jpg'); 10 | 11 | Webimg('./test.jpg').params({ 12 | img:'./node.png', 13 | pos:5, 14 | fontcolor:'#ff00ff', 15 | fontsize:100 16 | }).watermark('./batmark.jpg') 17 | 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webimg", 3 | "version": "0.3.3", 4 | "description": "Creating thumbnails, verification code, add a watermark.用于生成缩略图,添加水印,生成验证码 ", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/skipify/webimg" 12 | }, 13 | "dependencies":{ 14 | "gm" : "~1.17.0", 15 | "underscore" : "~1.7.0" 16 | }, 17 | "author": "skipify", 18 | "license": "MIT" 19 | } 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | -------------------------------------------------------------------------------- /sample/captcha.js: -------------------------------------------------------------------------------- 1 | var webimg = require('../') 2 | 3 | //最简单的验证码 4 | var captcha = webimg().captcha('./captcha.jpg'); 5 | console.log(captcha.getStr()); //输出验证码的文字 6 | 7 | 8 | //背景颜色为 #ff00ff的验证码,输出文件到./aa.png 9 | var captcha = webimg(100,50,'#ff00ff').captcha('./aa.png'); 10 | 11 | 12 | //设置属性 13 | var captcha = webimg(100,50,'#ff00ff'); 14 | captcha.fontSize(40)//字号 15 | .font('../font.ttf')//字体文件 16 | .fontColor('#ffffff')//字的颜色 17 | .width(200) 18 | .height(100) 19 | .captcha('newcaptcha.gif'); 20 | 21 | //不直接输出到图片而是返回buffer 22 | var fs = require('fs'); 23 | var captcha2 = webimg().captcha(function(err,buf){ 24 | if(err){ 25 | throw err; 26 | } 27 | fs.writeFile('buffer.png',buf,function(err,data){ 28 | if(err){ 29 | throw err; 30 | } 31 | }) 32 | }) 33 | console.log(captcha2.getStr()); 34 | 35 | //通过改变验证文字的产生来自定义 36 | 37 | webimg.captcha.prototype.random = function(){ 38 | return '5 + 3 =?'; 39 | } 40 | 41 | var captcha = webimg().captcha('out.jpg'); 42 | 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /sample/thumb.js: -------------------------------------------------------------------------------- 1 | var Webimg = require('../'); 2 | 3 | //缩略图默认的名字是原图生成的 4 | //如 pic.jpg => pic_t0.jpg 5 | 6 | //缩略图,最简单的缩小到宽度100 高度不指定则自动按比例缩小 7 | Webimg('./test.jpg').width(100).thumb(); 8 | 9 | //按照指定宽高自动缩小,比例不合适时自动裁剪 10 | Webimg('./test.jpg').height(50).width(100).thumb(); 11 | //=》等同于 12 | Webimg('./test.jpg').resize(100,50).thumb(); 13 | 14 | //指定输出的名字 15 | //指定名字如果包含 . 则不会增加扩展名,如果不包含.则会自动添加源文件的扩展名 16 | //不包含 / 则会生成到与原图相同的目录 包含 / 则会当做一个最终的路径输出 17 | //Webimg('./test.jpg').resize(100,50).outFile('thumb.jpg').thumb(); 18 | //等同于 19 | //Webimg('./test.jpg').resize(100,50).thumb('thumb.jpg'); 20 | //等同于 21 | Webimg('./test.jpg').resize(100,50).thumb('thumb'); 22 | 23 | 24 | //高级应用, 25 | //配置对象 26 | 27 | Webimg('./test.jpg').params({ 28 | width:185, 29 | outFile:'xx.jpg', 30 | }).thumb(); 31 | 32 | //配置项: 33 | 34 | /* 35 | width 36 | height 37 | outFile 38 | 39 | */ 40 | 41 | //同时生成多张缩略图 42 | 43 | Webimg('./test.jpg').params([{ 44 | width:200, 45 | }, 46 | { 47 | width:100, 48 | text:'hahahasdsd', ////生成图片的同时添加水印 如果 text img同时存在 那么先添加图片水印后添加文字水印 49 | fontcolor:'#ffffff', 50 | fontsize:12 51 | }, 52 | { 53 | width:290 54 | } 55 | 56 | ]).thumb(); 57 | 58 | //out put new file name 59 | webimg('./node.png').width(300).thumb(function(name){ 60 | console.log(name); 61 | }); -------------------------------------------------------------------------------- /README-CN.md: -------------------------------------------------------------------------------- 1 | webimg 2 | ====== 3 | 4 | [English](https://github.com/skipify/webimg/blob/master/README.md) 5 | 6 | 此项目使用了著名的[gm](https://github.com/aheckmann/gm),在使用之前请安装[graphicsmagick](http://www.graphicsmagick.org/),如果你用到了验证码或者文字水印你还应该安装[Ghostscript](http://www.ghostscript.com/). 7 | 8 | 9 | 此项目用于生成缩略图、添加水印、生成验证码,批量缩略图水印 10 | # 安装 11 | 12 | npm install webimg 13 | 14 | ## 缩略图示例 15 | 16 | Webimg('./test.jpg').resize(100,50).thumb('thumb.jpg'); 17 | 18 | ## 水印示例 19 | Webimg('./test.jpg') 20 | .markText('test')//设置水印文字 21 | .markPos(5)//水印位置 123456789 22 | .fontSize(50)//文字水印字体大小 23 | //.font('foot.ttf')//文字水印字体 24 | .fontColor('#ffffff')//字体颜色 25 | .watermark('./mark.jpg'); 26 | 27 | ## 验证码示例 28 | 29 | var captcha = webimg().captcha(); 30 | console.log(captcha.getStr()); //输出验证码的文字 31 | 32 | ## 缩略图和水印联合 33 | 34 | Webimg('./test.jpg').params({ 35 | width:100, 36 | text:'hahahasdsd', ////生成图片的同时添加水印 如果 text img同时存在 那么先添加图片水印后添加文字水印 37 | fontcolor:'#ffffff', 38 | fontsize:12 39 | }).thumb(); 40 | 41 | # API 42 | 43 | ### Webimg 构造函数 44 | 45 | 缩略图和水印 46 | @param string 图片文件名 47 | 48 | 或 49 | 用于验证码: 50 | @param int 宽 51 | @param int 高 52 | @param string 背景颜色 53 | 54 | ### size 55 | @param function 回调 56 | 返回图片的尺寸 57 | 58 | ### filesize 59 | @param function 60 | 返回文件的大小 61 | 62 | ### outFile 63 | @param string 设置输出的文件名 64 | 65 | ### font 66 | @param string 字体路径 (文字水印,验证码) 67 | 68 | ### fontColor 69 | @param string 文字颜色 (#ffffff) 70 | 71 | ### fontSize 72 | @param int 文字大小 73 | 74 | ### width 75 | @param int 宽度 76 | 77 | ### height 78 | @param int 高度 79 | 80 | **注意** 81 | 缩略图生成的时候如果只指定宽或者高则未指定的按照比例自动计算, 82 | 如果同时指定了宽高那么会按照指定缩小,比例不合适时会自动裁剪 83 | 84 | ### resize 85 | @param int 宽度 86 | @param int 高度 87 | 88 | 相当于 width height俩方法同时调用 89 | 90 | ### quality 91 | @param int 图片质量 1-100 92 | 93 | ### backgroundColor 94 | @param string 背景颜色 #000000 95 | 96 | ### markImg 97 | @param string 设置水印的图片地址 98 | 99 | ### markText 100 | @param string 设置水印的文字 101 | 102 | ### markPos 103 | @param int 设置水印的位置 104 | 对应位置如下: 105 | 1,2,3 106 | 4,5,6 107 | 7,8,9 108 | 109 | ### params 110 | @param object 111 | 此方法用于一次性设置参数 112 | 参数如下: 113 | `width` 宽度 @param int 114 | `text` 生成图片的同时添加水印 如果 text img同时存在 那么图片和文字水印会随机意外添加 @param string 115 | `fontcolor` 字体颜色 @param string #ffffff 116 | `fontsize` 字体大小 @param int 117 | `img` 水印图片地址 @param string 118 | `pos` 水印位置 @param int 1,2,3,4,5,6,7,8,9 119 | `quality` 图片质量 @param int 1-100 120 | `outFile` 输出的文件地址 缩略图、水印、验证码通用 121 | 输出文件地址的格式 可以使 thumb.jpg thumb 这样的话最终的路径与原图一致 122 | 如果是/aa.jpg 带有/的地址 则会直接医用指定的地址 123 | 124 | ## thumb 125 | @param string/null/function 126 | @param function 127 | 生成缩略图最终调用的方法 128 | 对于非批量的缩略图生成可以指定唯一的输出文件参数 129 | 第二个参数是回调函数,可以接受一个参数是生成的缩略图名称 130 | 131 | ## watermark 132 | @param string/null 133 | 水印生成后输出的地址,不指定则会自动生成自动原文件名后添加 _t0(_t1,_t2) 134 | 135 | ## captcha 136 | @param string/function 137 | 验证码的输出地址,如果不指定输出地址则需要指定一个回调函数,回调函数会返回 相应 png图片的 Buffer 138 | function(err,buf){ 139 | 140 | } 141 | 142 | 143 | ## captcha 验证码生成类 144 | 145 | ### getStr 146 | 返回生成的验证码字符串 147 | 148 | ### random 149 | 为验证码提供随机的字符串,此方法可以覆盖覆盖后可以自定义字符 150 | 151 | 152 | # 原生的gm 153 | 可以直接使用 Webimg.gm 来调用原生的gm -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | webimg 2 | ====== 3 | 4 | [中文](https://github.com/skipify/webimg/blob/master/README-CN.md) 5 | 6 | This Project base on [gm](https://github.com/aheckmann/gm),you shoud install [graphicsmagick](http://www.graphicsmagick.org/),If you need to use captcha or text watermark, You should install [Ghostscript](http://www.ghostscript.com/). 7 | 8 | 9 | This project is used to generate a thumbnail, add watermarks, generate captchas, batch thumbnail watermark. 10 | # INSTALL 11 | 12 | npm install webimg 13 | 14 | ## thumbnail example 15 | 16 | Webimg('./test.jpg').resize(100,50).thumb('thumb.jpg'); 17 | 18 | ## Watermark example 19 | Webimg('./test.jpg') 20 | .markText('test')//text 21 | .markPos(5)//mark position 123456789 22 | .fontSize(50)//fongsize 23 | //.font('foot.ttf')//fontfamily 24 | .fontColor('#ffffff')//font color 25 | .watermark('./mark.jpg'); 26 | 27 | ## Verification code sample 28 | 29 | var captcha = webimg().captcha(); 30 | console.log(captcha.getStr()); //get the captcha str 31 | 32 | ## The thumbnail and watermark 33 | 34 | Webimg('./test.jpg').params({ 35 | width:100, 36 | text:'hahahasdsd', ////watermark text 37 | fontcolor:'#ffffff', 38 | fontsize:12 39 | }).thumb(); 40 | 41 | # API 42 | 43 | ### Webimg contructor 44 | 45 | thumbnail and watermark 46 | @param string image name 47 | 48 | OR 49 | Captcha: 50 | @param int width 51 | @param int height 52 | @param string background color 53 | 54 | ### size 55 | @param function callback 56 | return image size 57 | 58 | ### filesize 59 | @param function 60 | image's size 61 | 62 | ### outFile 63 | @param string Set the output file name 64 | 65 | ### font 66 | @param string font path 67 | 68 | ### fontColor 69 | @param string fontcolor (#ffffff) 70 | 71 | ### fontSize 72 | @param int fontsize 73 | 74 | ### width 75 | @param int width 76 | 77 | ### height 78 | @param int height 79 | 80 | **Notice** 81 | Thumbnails generated if only specify the width or height is not specified in accordance with the proportion of automatic calculation,If at the same time high then will be in accordance with the specified contract specifies the width, the proportion is improper cutting automatically 82 | 83 | ### resize 84 | @param int width 85 | @param int height 86 | 87 | 88 | 89 | ### quality 90 | @param int image quality 1-100 91 | 92 | ### backgroundColor 93 | @param string background color #000000 94 | 95 | ### markImg 96 | @param string watermark image path 97 | 98 | ### markText 99 | @param string Set text watermark 100 | 101 | ### markPos 102 | @param int Sets the position of the watermark 103 | As Follow: 104 | 1,2,3 105 | 4,5,6 106 | 7,8,9 107 | 108 | ### params 109 | @param object 110 | This method is used for one-time setup parameters 111 | params: 112 | `width` width @param int 113 | `text` The watermark generated images at the same time If the test img is at the same time Then add images and text watermark random accident @param string 114 | `fontcolor` fontcolor @param string #ffffff 115 | `fontsize` fontsize @param int 116 | `img` file path @param string 117 | `pos` mark position @param int 1,2,3,4,5,6,7,8,9 118 | `quality` quality @param int 1-100 119 | `outFile` The output file address Thumbnails, watermark, a verification code 120 | The format of the output file address Can make the thumb. JPG thumb so the final path consistent with the original image,If it is /aa. JPG with / Will direct medical specified address 121 | 122 | ## thumb 123 | @param string/null/function 124 | @param function 125 | Generated thumbnail in the end the method called 126 | For the batch thumbnail generation can be specified only output file parameters,the callback function can accept a param for the thumb name; 127 | 128 | ## watermark 129 | @param string/null 130 | Watermark generated output after the address, does not specify, it will automatically generated automatically added after the original file name _t0(_t1,_t2) 131 | 132 | ## captcha 133 | @param string/function 134 | Verification code output address, if you do not specify output address you will need to specify a callback function, the callback function returns the corresponding PNG image Buffer 135 | function(err,buf){ 136 | 137 | } 138 | 139 | 140 | ## captcha Verification code generated 141 | 142 | ### getStr 143 | Returns the generated captcha string 144 | 145 | ### random 146 | Provide authentication code with random string, this method can cover can customize the characters 147 | 148 | 149 | # gm 150 | Webimg.gm => gm -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var gm = require('gm'), 2 | fs = require('fs'), 3 | _ = require('underscore'), 4 | ph = require('path'), 5 | opts = { 6 | quality : 100//默认参数 7 | }, 8 | posTypes = [ 9 | "NorthWest", 10 | "North", 11 | "NorthEast", 12 | "West", 13 | "Center", 14 | "East", 15 | "SouthWest", 16 | "South", 17 | "SouthEast" 18 | ]; 19 | 20 | 21 | var Webimg = function(img,height,background){ 22 | 23 | if(!(this instanceof Webimg)){ 24 | return new Webimg(img,height,background); 25 | } 26 | 27 | //验证码配置 28 | if(_.isNumber(img)) 29 | { 30 | opts.width = img; 31 | img = null; 32 | opts.height = height; 33 | opts.background = background; 34 | this.dst = null; 35 | }else{ 36 | this.dst = img; 37 | } 38 | if(this.dst) 39 | { 40 | this.gm = gm(this.dst); 41 | } 42 | } 43 | 44 | Webimg.fn = {}; 45 | 46 | /* 47 | 根据原来的文件路径,宽高 48 | 创建一个新文件名 49 | */ 50 | Webimg.fn.fileName = function(i,opts) 51 | { 52 | if(!this.dst){ 53 | return ''; 54 | } 55 | if(_.isObject(i)){ 56 | opts = i; 57 | i = 0; 58 | } 59 | var path = this.dst, 60 | paths = path.split(ph.sep), 61 | n = paths.pop(), 62 | ns = n.split('.'), 63 | ext = "." + ns.pop(), 64 | xname = ns.join('.'); 65 | if(!ext){ 66 | ext = '.jpg'; 67 | } 68 | if(opts.outFile){ 69 | if(opts.outFile.indexOf(ph.sep) > -1) { 70 | return opts.outFile; 71 | } 72 | xname = opts.outFile.indexOf('.') > -1 ? opts.outFile : (opts.outFile + ext); 73 | }else{ 74 | xname += "_t" + i + ext; 75 | } 76 | return paths.join(ph.sep) + ph.sep + xname; 77 | } 78 | 79 | //指定一个输出文件的名字 80 | Webimg.fn.outFile = function(name){ 81 | if(_.isArray(opts)) { 82 | throw new Error('You have already set up the batch configuration'); 83 | } 84 | opts.outFile = name; 85 | return this; 86 | } 87 | //文件尺寸 88 | Webimg.fn.size = function(callback){ 89 | this.gm.size(function(e,v){ 90 | callback.apply(null,arguments); 91 | }) 92 | } 93 | //文件大小 94 | Webimg.fn.filesize = function(callback){ 95 | this.gm.filesize(function(e,v){ 96 | callback.apply(null,arguments); 97 | }) 98 | }; 99 | 100 | //设置字体 101 | Webimg.fn.font = function(font){ 102 | opts.font = font; 103 | return this; 104 | } 105 | 106 | //设置文字颜色 107 | Webimg.fn.fontColor = function(color){ 108 | opts.fontcolor = color; 109 | return this; 110 | } 111 | Webimg.fn.fontSize = function(size){ 112 | opts.fontsize = size; 113 | return this; 114 | } 115 | /* 116 | 设置各种参数 117 | */ 118 | 119 | //批量设置 120 | Webimg.fn.params = function(setting){ 121 | opts = setting; 122 | return this; 123 | } 124 | //重置所有设置 125 | Webimg.fn.resetParams = function(){ 126 | opts = {}; 127 | return this; 128 | } 129 | 130 | //设置要生成的图片宽高质量,通用于缩略图 验证码 131 | Webimg.fn.width = function(width){ 132 | if(_.isArray(opts)){ 133 | throw new Error('You have already set up the batch configuration'); 134 | } 135 | opts.width = width; 136 | return this; 137 | } 138 | Webimg.fn.height = function(height) 139 | { 140 | if(_.isArray(opts)){ 141 | throw new Error('You have already set up the batch configuration'); 142 | } 143 | opts.height = height; 144 | return this; 145 | } 146 | Webimg.fn.resize = function(width,height){ 147 | this.width(width); 148 | this.height(height); 149 | return this; 150 | } 151 | Webimg.fn.quality = function(q){ 152 | if(_.isArray(opts)) { 153 | throw new Error('You have already set up the batch configuration'); 154 | } 155 | opts.quality = q; 156 | return this; 157 | } 158 | //设置图片背景色 159 | Webimg.fn.backgroundColor = function(color){ 160 | opts.background = color; 161 | } 162 | 163 | 164 | //设置水印物料 165 | Webimg.fn.markImg = function(mark) 166 | { 167 | if(_.isArray(opts)) 168 | { 169 | throw new Error('You have already set up the batch configuration'); 170 | } 171 | opts.img = mark; 172 | opts.text = null; 173 | return this; 174 | } 175 | //水印文字 176 | Webimg.fn.markText = function(text){ 177 | if(_.isArray(opts)) 178 | { 179 | throw new Error('You have already set up the batch configuration'); 180 | } 181 | opts.text = text; 182 | opts.img = null; 183 | return this; 184 | } 185 | //水印位置 186 | Webimg.fn.markPos = function(pos){ 187 | opts.pos = formatPos(pos); 188 | return this; 189 | } 190 | 191 | /* 192 | 缩略图 193 | */ 194 | 195 | //生成缩略图 196 | Webimg.fn.thumb = function(outfile,callback){ 197 | var that = this, 198 | dst = this.dst, 199 | oopts = opts, 200 | tnames = []; 201 | if(typeof outfile == 'function'){ 202 | callback = outfile; 203 | outfile = null; 204 | } 205 | this.resetParams(); 206 | if(!dst){ 207 | throw new Error('No file specified'); 208 | } 209 | this.formatParmas(oopts,function(sopts){ 210 | if(_.isArray(sopts)){ 211 | for(var i=0;i -1) 269 | { 270 | return pos; 271 | } 272 | if(!_.isNumber(pos)) 273 | { 274 | return 'SouthEast'; 275 | } 276 | pos = parseInt(pos); 277 | pos --; 278 | return posTypes[pos]; 279 | } 280 | //生成水印 281 | 282 | Webimg.fn.watermark = function(outfile){ 283 | if(!this.dst){ 284 | throw new Error('No file specified'); 285 | } 286 | opts.outFile = outfile || this.dst;//不指定输出名字则直接给原图输出 287 | //组织水印配置 288 | var markopts = formatOpts(opts); 289 | this.resetParams();//重置参数 290 | watermark(this.dst,markopts); 291 | } 292 | 293 | //格式化非宽高配置 294 | var formatOpts = function(opts){ 295 | opts.img = opts.img || null; 296 | opts.text = opts.text || null; 297 | opts.pos = formatPos(opts.pos) || 'SouthEast'; 298 | opts.fontsize = opts.fontsize || 28; 299 | opts.font = opts.font || __dirname + '/font.ttf'; 300 | opts.fontcolor = opts.fontcolor || '#000000'; 301 | opts.background = opts.background || '#ffffff'; 302 | opts.quality = opts.quality || 100; 303 | return opts; 304 | } 305 | 306 | var watermark = function(file,opts){ 307 | //图片水印 308 | if(opts.img) 309 | { 310 | watermarkImg(file,opts); 311 | } 312 | //文字水印 313 | if(opts.text) 314 | { 315 | watermarkText(file,opts); 316 | } 317 | } 318 | //生成图片水印 319 | var watermarkImg = function(file,opts){ 320 | gm(file).composite(opts.img).gravity(opts.pos).write(opts.outFile,function(err){ 321 | if(err) 322 | { 323 | throw err 324 | } 325 | }) 326 | } 327 | //生成文字水印 328 | 329 | var watermarkText = function(file,opts) 330 | { 331 | var pos = opts.pos.toLowerCase(); 332 | gm(file) 333 | .stroke(opts.fontcolor) 334 | .fill(opts.fontcolor)//边框和填充颜色一致 335 | .font(opts.font, opts.fontsize) 336 | .drawText(20, 20, opts.text,pos) 337 | .write(opts.outFile, function (err) { 338 | if (err){ 339 | throw err; 340 | } 341 | }); 342 | } 343 | 344 | /* 345 | 验证码 346 | */ 347 | Webimg.fn.captcha = function(outfile){ 348 | if(_.isFunction(outfile)) 349 | { 350 | opts.outFile = null; 351 | }else{ 352 | opts.outFile = outfile || opts.outFile; 353 | } 354 | sopts = formatOpts(opts); 355 | this.resetParams(); 356 | var c = new captcha(sopts,outfile); 357 | return c; 358 | } 359 | 360 | //验证码 361 | var captcha = function(opts,callback){ 362 | opts.width = opts.width || 80; 363 | opts.height = opts.height || 35; 364 | opts.background = opts.background || '#ffffff'; 365 | opts.outFile = opts.outFile; 366 | 367 | this.str = this.random() || '8888'; 368 | 369 | this.img = gm(opts.width,opts.height,opts.background); 370 | this.img.stroke(opts.fontcolor) 371 | .fill(opts.fontcolor) 372 | .font(opts.font, opts.fontsize) 373 | .drawText(0, 0, this.str ,'center'); 374 | this.drawLine(opts); 375 | if(_.isFunction(callback)) 376 | { 377 | this.img.swirl(45).toBuffer('png',function(err,buffer){ 378 | callback && callback.apply(null,arguments); 379 | }); 380 | }else{ 381 | this.img.swirl(45).write(opts.outFile, function (err) { 382 | if(err){ 383 | throw err; 384 | } 385 | }); 386 | } 387 | 388 | } 389 | captcha.prototype.getStr = function(){ 390 | return this.str; 391 | } 392 | 393 | //生成验证码字符串 394 | captcha.prototype.random = function(len){ 395 | len = parseInt(len) || 4; 396 | var chars = ['a','b','c','d','e','f','g','i','h','k','m','n','p','q','r','s','t','u','v','w','x','y','3','4','5','6','7','8','9'], 397 | str = "", 398 | clen = chars.length - 1; 399 | for(var i =0;i value.width){ 456 | opt.width = value.width; 457 | } 458 | if(opt.height > value.height){ 459 | opt.height = value.height; 460 | } 461 | opt.owidth = value.width; 462 | opt.oheight = value.height; 463 | return opt; 464 | } 465 | callback && callback.call(null,opt); 466 | }); 467 | } 468 | 469 | 470 | Webimg.prototype = Webimg.fn; 471 | Webimg.gm = gm; 472 | Webimg.captcha = captcha; 473 | module.exports = Webimg; --------------------------------------------------------------------------------