官网: https://github.com/thumbor/thumbor
目前版本: 6.3.2
图像处理是系统开发中的必备组件,各种开发语言都内置图像处理API,也有大量的开源图像处理库做补充。
[1] 图像变换引擎( ImageMagick、 Graphicsmagick、 OpenCV)
[2] 图像优化工具( JPEGMini、 TinyPNG、 ImageOptim、 ImageAlpha、 pngquant、 MozJPEG)
[3] 图像处理系统( Pilbox、 Thumbor)
Thumbor通过特殊构成的URL来创建缩略图,比如:
引用
http://thumbor.example.com/320x240/http://images.example.com/llamas.jpg
含义是:Thumbor服务器thumbor.example.com通过HTTP从images.example.com获取图像llamas.jpg,将其缩放到320x240之后返回图像数据。
Thumbor内部支持以下三种图像变换引擎:
PIL (Python Image Library)、GraphicsMagick (pgmagick)、OpenCV
Thumbor存储图片支持的方式有File、Memcached、MongoDB、Redis。根据实际的使用情况选择存储方式。默认选用文件系统的存储方式。
具体详细的使用方法可以参考官方文档: http://thumbor.readthedocs.io/en/latest/index.html
需要注意的是:Thumbor目前还不支持Python3!
引用
File "/usr/local/tensorflow/lib/python3.5/site-packages/thumbor/context.py", line 293
print "Joining threads...."
SyntaxError: Missing parentheses in call to 'print'
print "Joining threads...."
SyntaxError: Missing parentheses in call to 'print'
print "Hello world" # python 2.x print("Hello world") # python 3.x
(1)安装
# python -V Python 2.7.5 # pip search thumbor thumbor (6.3.2) - thumbor is an open-source photo thumbnail service by globo.com # pip install thumbor # pip install -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com thumbor
(2)启动
# mkdir /usr/local/thumbor # thumbor-config > /usr/local/thumbor/thumbor.conf # cp /usr/local/thumbor/thumbor.conf /usr/local/thumbor/thumbor.conf.default # thumbor --port=8888 --conf=/usr/local/thumbor/thumbor.conf
以下警告提示是没有安装opencv。
引用
2017-08-15 15:34:02 thumbor:WARNING Module thumbor.filters.distributed_collage could not be imported: No module named cv2
(3)测试
- 原图
http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第1张图片](http1://img.it610.com/image/info5/24ff2de1dd2c4a75bcbb5f37be552a76.jpg)
http://thumbor-server:8888/unsafe/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第2张图片](http1://img.it610.com/image/info5/40a4c0fb6f764d64b17173e9a5a8c79b.jpg)
- 保持横纵比,宽度固定300px
http://thumbor-server:8888/unsafe/300x0/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第3张图片](http1://img.it610.com/image/info5/b17e4c76c6974d19bb8ebf2d2b79924b.jpg)
- 保持横纵比,高度固定300px
http://thumbor-server:8888/unsafe/0x300/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第4张图片](http1://img.it610.com/image/info5/5224cbff238d496c9a3746f75c7076fa.jpg)
- 指定大小300×300
http://thumbor-server:8888/unsafe/300x300/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第5张图片](http1://img.it610.com/image/info5/297ff0020ea441c586d12a15f1d1d199.jpg)
- 裁剪指定位置
http://thumbor-server:8888/unsafe/528x111:1101x657/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第6张图片](http1://img.it610.com/image/info5/715a0ea448e9402891a7a0d0b9ef849e.jpg)
- 图像滤镜
http://thumbor-server:8888/unsafe/400x200/filters:grayscale()/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第7张图片](http1://img.it610.com/image/info5/c33e72a715614259bfb14fb0b1efd04a.jpg)
http://thumbor-server:8888/unsafe/200x200/filters:brightness(-20)/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第8张图片](http1://img.it610.com/image/info5/d20b81220a47425cad2dc2bb15838315.jpg)
http://thumbor-server:8888/unsafe/200x200/filters:brightness(40)/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第9张图片](http1://img.it610.com/image/info5/95bd80aa15e24db9bddcc2c4c0fda920.jpg)
http://thumbor-server:8888/unsafe/filters:round_corner(40,255,255,255):grayscale()/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第10张图片](http1://img.it610.com/image/info5/c23f2c90e0324bb39a234011a9100778.jpg)
(4)安全模式
以上都是测试用URL,可以看到URL里包含 /unsafe/。
限定来源
# vi /usr/local/thumbor/thumbor.conf ALLOWED_SOURCES = [ 'desk.fd.zol-img.com.cn' ]
访问以下URL会返回 200 OK
http://thumbor-server:8888/unsafe/300x100/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第11张图片](http1://img.it610.com/image/info5/a323ab985ccd414d860a069789c50496.jpg)
访问以下URL会返回 400 Bad Request
http://thumbor-server:8888/unsafe/300x100/https://http.cat/405.jpg
![搭建独立图像处理服务(Thumbor)_第12张图片](http1://img.it610.com/image/info5/957b50ab11f74e1ebae6319ed38f1d53.jpg)
关闭unsafe配置密钥
# openssl rand -base64 48 JNj9+RxUKfsgrVN+8cH5DVzNdRURiFzBo6S2lL9Q0zcweHmdkU6q1Rf60XX7LnVc # vi /usr/local/thumbor/thumbor.conf ALLOW_UNSAFE_URL = False SECURITY_KEY = 'JNj9+RxUKfsgrVN+8cH5DVzNdRURiFzBo6S2lL9Q0zcweHmdkU6q1Rf60XX7LnVc'
再次访问以下URL会返回 400 Bad Request,设置的ALLOWED_SOURCES也无效了。
http://thumbor-server:8888/unsafe/300x100/http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第13张图片](http1://img.it610.com/image/info5/957b50ab11f74e1ebae6319ed38f1d53.jpg)
(5)URL做成
查看thumbor-url用法
# thumbor-url -h
通过KEY获取URL
# thumbor-url -k JNj9+RxUKfsgrVN+8cH5DVzNdRURiFzBo6S2lL9Q0zcweHmdkU6q1Rf60XX7LnVc http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg URL: /UJWGMA3XL6X29zF2qgQgBKqz_QM=/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg /UJWGMA3XL6X29zF2qgQgBKqz_QM=/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
通过KEY文件获取URL
# vi /etc/thumbor.key JNj9+RxUKfsgrVN+8cH5DVzNdRURiFzBo6S2lL9Q0zcweHmdkU6q1Rf60XX7LnVc # thumbor-url -l /etc/thumbor.key http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg URL: /UJWGMA3XL6X29zF2qgQgBKqz_QM=/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg /UJWGMA3XL6X29zF2qgQgBKqz_QM=/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
访问以下URL会返回 200 OK
http://thumbor-server:8888/UJWGMA3XL6X29zF2qgQgBKqz_QM=/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第14张图片](http1://img.it610.com/image/info5/6821e182283e4d5186e06da79461c2f5.jpg)
缩放
# thumbor-url -l /etc/thumbor.key -w 300 -e 200 http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg URL: /byeRaXKHyMqfrsWmRZb558J_8iI=/300x200/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg /byeRaXKHyMqfrsWmRZb558J_8iI=/300x200/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
访问以下URL会返回 200 OK
http://thumbor-server:8888/byeRaXKHyMqfrsWmRZb558J_8iI=/300x200/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第15张图片](http1://img.it610.com/image/info5/e43907e4799d40269b4360d7fa8fbe4b.jpg)
(6)智能裁剪
安装
# yum install -y opencv GraphicsMagick openssl-devel curl-devel GraphicsMagick-c++-devel boost boost-devel boost-python # pip install pgmagick opencv-python graphicsmagick-engine # pip install -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com pgmagick opencv-python graphicsmagick-engine
设置
# vi /usr/local/thumbor/thumbor.conf DETECTORS = [ 'thumbor.detectors.face_detector', 'thumbor.detectors.feature_detector' ]
有无/smart/前后效果对比:
# thumbor-url -l /etc/thumbor.key -w 300 -e 80 http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg URL: /twOvaCpM5nAUv3JCdDnNP76Iqz0=/300x80/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg /twOvaCpM5nAUv3JCdDnNP76Iqz0=/300x80/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
# thumbor-url -l /etc/thumbor.key -w 300 -e 80 -s http://desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg URL: /swDVsG0g_nYBE8zHDcjzfyH561w=/300x80/smart/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg /swDVsG0g_nYBE8zHDcjzfyH561w=/300x80/smart/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
http://thumbor-server:8888/twOvaCpM5nAUv3JCdDnNP76Iqz0=/300x80/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第16张图片](http1://img.it610.com/image/info5/d01b66f6014b4f9883230b2344b93d19.jpg)
http://thumbor-server:8888/swDVsG0g_nYBE8zHDcjzfyH561w=/300x80/smart/http%3A//desk.fd.zol-img.com.cn/t_s1920x1200c5/g5/M00/01/0E/ChMkJlbKwbyIFnBKAAhTmqigyPUAALGdAHEN-kACFOy827.jpg
![搭建独立图像处理服务(Thumbor)_第17张图片](http1://img.it610.com/image/info5/48c274bacf154ff8921d505cb18fc944.jpg)
(7)图像上传
可以通过REST的方式上传图像:POST http://thumbor-server:8888/image
默认未开启上传功能,会返回‘405: Method Not Allowed’。
# vi /usr/local/thumbor/thumbor.conf UPLOAD_ENABLED = True # curl -i -H "Content-Type: image/jpeg" -H "Slug: rensanning.jpg" \ -XPOST http://thumbor-server:8888/image --data-binary "@/tmp/1348814002177.jpg" HTTP/1.1 201 Created Date: Wed, 16 Aug 2017 02:19:59 GMT Content-Length: 0 Content-Type: text/html; charset=UTF-8 Location: /image/14ffd48c8baa4301b476c4b837568af8/rensanning.jpg Server: TornadoServer/4.5.1
![搭建独立图像处理服务(Thumbor)_第18张图片](http1://img.it610.com/image/info5/331846b7fa57463c98f67efe9c58a3f8.jpg)
访问以下URL会返回 200 OK。
http://thumbor-server:8888/image/14ffd48c8baa4301b476c4b837568af8/rensanning.jpg
![搭建独立图像处理服务(Thumbor)_第19张图片](http1://img.it610.com/image/info5/5b17b6548b5b40feaaa7e70973702fd1.jpg)
同理可以通过PUT、DELETE、GET来操作已有图像。当然默认是不允许的需要修改配置。
引用
#UPLOAD_DELETE_ALLOWED = False
#UPLOAD_PUT_ALLOWED = False
#UPLOAD_PUT_ALLOWED = False
(8)其他设置
默认采用的HTTP加载图像,也就是需要提供一个图像的绝对URL,也可以采用加载本地图像,把图像放入root文件夹即可访问。
LOADER = 'thumbor.loaders.file_loader'
引用
## The loader thumbor should use to load the original image. This must be the
## full name of a python module (python must be able to import it)
## Defaults to: 'thumbor.loaders.http_loader'
#LOADER = 'thumbor.loaders.http_loader'
## The root path where the File Loader will try to find images
## Defaults to: '/root'
#FILE_LOADER_ROOT_PATH = '/root'
## full name of a python module (python must be able to import it)
## Defaults to: 'thumbor.loaders.http_loader'
#LOADER = 'thumbor.loaders.http_loader'
## The root path where the File Loader will try to find images
## Defaults to: '/root'
#FILE_LOADER_ROOT_PATH = '/root'
默认是不存储转换后的图像的,需要的话需要设置。
RESULT_STORAGE = 'thumbor.result_storages.file_storage'
引用
## The result storage thumbor should use to store generated images. This must be
## the full name of a python module (python must be able to import it)
## Defaults to: None
#RESULT_STORAGE = None
## Path where the Result storage will store generated images
## Defaults to: '/tmp/thumbor/result_storage'
#RESULT_STORAGE_FILE_STORAGE_ROOT_PATH = '/tmp/thumbor/result_storage'
## the full name of a python module (python must be able to import it)
## Defaults to: None
#RESULT_STORAGE = None
## Path where the Result storage will store generated images
## Defaults to: '/tmp/thumbor/result_storage'
#RESULT_STORAGE_FILE_STORAGE_ROOT_PATH = '/tmp/thumbor/result_storage'
参考:
https://segmentfault.com/a/1190000008656825
https://ingramchen.io/blog/2015/12/thumbor-tutorial.html
https://blog.1q77.com/2015/05/using-thumbor-part1