1.0.1 • Published 6 years ago
@houkai/imagehandler v1.0.1
支持 jpg, jpeg, png, webp, tiff, gif, svg 样式的图片裁剪、缩放等操作
内置处理引擎
图片处理支持类型
resize: 缩放width: 宽度height: 高度fit: 图片缩放规则default: covercover: 智能裁剪- 智能裁剪 (个人理解)
- width 对比 (old_width/new_width) 和 height 对比 (old_height/new_height) 比较
- 比例大的需要先裁剪一定长度(z)
- 比如 width 比例较大: (old_width - z)/new_width = old_height/new_height
- z = old_width - (old_height/new_height) * new_width
fill: 忽略比例, 强行缩放inside: 忽略给定短边, 按照长边比例进行缩放outside: 忽略给定长边, 按照短边比例进行缩放contain: 图片缩放和 inside 相同, 差别在于是内嵌在给定长宽内
limit: \<bool> 当超出原有大小时, 是否禁止缩放;default: falseposition: 智能裁剪时保留哪个部位;center,top,bottom,left,right
crop: 裁剪left: x 坐标top: y 坐标width: 宽度height: 高度
rotate: 翻转rotate: 度数
quality:quality: number
proportion:proportion: number (放大倍数)
format:format: 'png', 'jpg' ...
circle: 裁圆; gif 需先 format png- desc: 目前只做了圆处理, 后期会加入花式处理
x: x 坐标y: y 坐标r: 半径
暴露函数
imageHandler (input, rule, forceSharp)
- input: 仅持支 buffer, 后期优化
- rule:
[ { method: 'format', opts: { format: 'png' } }, { method: 'resize', opts: { width: 100, height: 100 } } ] - style: 内置的几种样式, 优先级高于 rule
- forceSharp: 如 format、circle 等功能对 gif 图片而言就需要强制使用 sharp 引擎处理; 因为另一个没写 😂
- return: buffer
toRule (ruleStr)
- param: ruleStr, 字符串形式处理规则
- returns: ruleArr, 数组形式规则
为方便使用, 提供了另一套字符串规则, 使用缩写代替
上文中提到, 支持 resize crop circle rotate format proportion quality 七种操作方式,
其中 resize crop circle 三种操作, 需要批量参数修正, 所以制定如下简易规则:
- resize:
- 示例:
resize,w_100,h_100,f_inside,l_1,p_center - width: w
- height: h
- limit: l
- fit: f
- position: p
- 示例:
- crop:
- 示例:
crop,x_100,y_100,w_100,h_100 - left: x
- top: y
- width: w
- heigth: h
- 示例:
- circle
- 示例:
circle,x_100,y_100,r_100
- 示例:
- rotate
- 示例:
rotate,10
- 示例:
- quality
- 示例:
quality,90
- 示例:
- proportion
- 示例:
proportion,2
- 示例:
- format
- 示例:
format,png
- 示例:
单样式字符串, 比如 'format,png' 交由 toRule 函数处理, 返回规则数组, 然后交由 imageHandler 函数处理即可
多样式字符串, 样式之间以 : 间隔, resize,w_100:format,png
1.0.1
6 years ago