docker 布置 thumbor 服务器
下载镜象
docker pull apsl/thumbor
下载完后先临时启动
docker run -it --rm --name thumbor
开多一个ssh窗口,将配置文件和启动文件COPY出来
mkdir /opt/thumbor
docker cp thumbor:/docker-entrypoint.sh /opt/thumbor/
docker cp thumbor:/usr/src/app /opt/thumbor/app
修改启动文件
#!/bin/sh
# To disable warning libdc1394 error: Failed to initialize libdc1394
ln -s /dev/null /dev/raw1394
#envtpl /usr/src/app/thumbor.conf.tpl --allow-missing --keep-template
# If log level is defined we configure it, else use default log_level = info
if [ -n "$LOG_LEVEL" ]; then
LOG_PARAMETER="-l $LOG_LEVEL"
fi
# Check if thumbor port is defined -> (default port 80)
if [ -z ${THUMBOR_PORT+x} ]; then
THUMBOR_PORT=8000
fi
if [ "$1" = 'thumbor' ]; then
exec thumbor --port=$THUMBOR_PORT --conf=/usr/src/app/thumbor.conf $LOG_PARAMETER
fi
exec "$@"
修改配置文件,一般需要修改到的,已标记中文
################################### 日志配置 ####################################
## Logging configuration as json
## Defaults to: None
THUMBOR_LOG_CONFIG = None
## Log Format to be used by thumbor when writing log messages.
## Defaults to: %(asctime)s %(name)s:%(levelname)s %(message)s
THUMBOR_LOG_FORMAT = '%(asctime)s %(name)s:%(levelname)s %(message)s'
## Date Format to be used by thumbor when writing log messages.
## Defaults to: %Y-%m-%d %H:%M:%S
THUMBOR_LOG_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
################################################################################
################################### Imaging ####################################
## 读取或者创建图片的最大宽度(PX),0为不限制
## Defaults to: 0
MAX_WIDTH = 1024
## 读取或者创建图片的最大高度(PX),0为不限制
## Defaults to: 0
MAX_HEIGHT = 0
## 读取的最大像素
## Defaults to: 75000000 pixels
MAX_PIXELS = 75000000
## 读取或者创建图片的最小宽度(PX)
## Defaults to: 1
MIN_WIDTH = 1
## 读取或者创建图片的最小高度(PX)
## Defaults to: 1
MIN_HEIGHT = 1
## 域名白名单,添加后别人不能引用
## expressions.
## Defaults to: []
ALLOWED_SOURCES = []
## 生成JPEG的质量
## Defaults to: 80
QUALITY = 80
## 是否使用渐进式加载图片.
## Defaults to: True
PROGRESSIVE_JPEG = True
## Specify subsampling behavior for Pillow (see `subsampling` in
## http://pillow.readthedocs.org/en/latest/handbook/image-file-
## formats.html#jpeg).Be careful to use int for 0,1,2 and string for "4:4:4"
## notation. Will ignore `quality`. Using `keep` will copy the original file's
## subsampling.
## Defaults to: None
PILLOW_JPEG_SUBSAMPLING = None
## Specify quantization tables for Pillow (see `qtables` in
## http://pillow.readthedocs.org/en/latest/handbook/image-file-
## formats.html#jpeg). Will ignore `quality`. Using `keep` will copy the
## original file's qtables.
## Defaults to: None
PILLOW_JPEG_QTABLES = None
## Specify resampling filter for Pillow resize method.One of LANCZOS, NEAREST,
## BILINEAR, BICUBIC, HAMMING (Pillow>=3.4.0).
## Defaults to: 'LANCZOS'
PILLOW_RESAMPLING_FILTER = 'LANCZOS'
## Quality index used for generated WebP images. If not set (None) the same level
## of JPEG quality will be used.
## Defaults to: None
WEBP_QUALITY = None
## PNG 的压缩级别.
## Defaults to: 6
PNG_COMPRESSION_LEVEL = 6
## Specifies whether WebP format should be used automatically if the request
## accepts it (via Accept header)
## Defaults to: False
AUTO_WEBP = False
## Specify the ratio between 1in and 1px for SVG images. This is only used
## whenrasterizing SVG images having their size units in cm or inches.
## Defaults to: 150
SVG_DPI = 150
## Max AGE sent as a header for the image served by thumbor in seconds
## Defaults to: 86400
MAX_AGE = 86400
## Indicates the Max AGE header in seconds for temporary images (images with
## failed smart detection)
## Defaults to: 0
MAX_AGE_TEMP_IMAGE = 0
## Indicates whether thumbor should rotate images that have an Orientation EXIF
## header
## Defaults to: False
RESPECT_ORIENTATION = False
## 忽略错误并返回原图 (not
## saved in result storage and with MAX_AGE_TEMP_IMAGE age)
## Defaults to: False
IGNORE_SMART_ERRORS = False
## Sends If-Modified-Since & Last-Modified headers; requires support from result
## storage
## Defaults to: False
SEND_IF_MODIFIED_LAST_MODIFIED_HEADERS = False
## 保留 exif 信息(图片会变大).
## Defaults to: False
PRESERVE_EXIF_INFO = False
## 启用动画支持.
## Defaults to: True
ALLOW_ANIMATED_GIFS = False
## Indicates whether thumbor should use gifsicle engine. Please note that smart
## cropping and filters are not supported for gifs using gifsicle (but won't
## give an error).
## Defaults to: False
USE_GIFSICLE_ENGINE = False
## 启用黑名单.
## Defaults to: False
USE_BLACKLIST = False
## 图像转换的线程池大小. The default value is
## 0 (don't use a threadpoool. Increase this if you are seeing your IOLoop
## getting blocked (often indicated by your upstream HTTP requests timing out)
## Defaults to: 0
ENGINE_THREADPOOL_SIZE = 0
################################################################################
################################ Extensibility #################################
## The metrics backend thumbor should use to measure internal actions. This must
## be the full name of a python module (python must be able to import it)
## Defaults to: 'thumbor.metrics.logger_metrics'
METRICS = 'thumbor.metrics.logger_metrics'
## 图片加载方式
## thumbor.loaders.http_loader 网络加载
## thumbor.loaders.file_loader 本地加载
## full name of a python module (python must be able to import it)
## Defaults to: thumbor.loaders.http_loader
LOADER = 'thumbor.loaders.file_loader'
## The file storage thumbor should use to store original images. This must be the
## full name of a python module (python must be able to import it)
## Defaults to: thumbor.storages.file_storage
STORAGE = 'thumbor.storages.file_storage'
STORAGE_BUCKET = ''
RESULT_STORAGE_BUCKET = ''
## 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 = 'thumbor.result_storages.file_storage'
## The imaging engine thumbor should use to perform image operations. This must
## be the full name of a python module (python must be able to import it)
## Defaults to: thumbor.engines.pil
ENGINE = 'thumbor.engines.pil'
## The gif engine thumbor should use to perform image operations. This must be
## the full name of a python module (python must be able to import it)
## Defaults to: 'thumbor.engines.gif'
GIF_ENGINE = 'thumbor.engines.gif'
## The url signer thumbor should use to verify url signatures.This must be the
## full name of a python module (python must be able to import it)
## Defaults to: 'libthumbor.url_signers.base64_hmac_sha1'
URL_SIGNER = 'libthumbor.url_signers.base64_hmac_sha1'
################################################################################
################################### Security ###################################
## 使用密钥加密URL
## Defaults to: MY_SECURE_KEY
SECURITY_KEY = 'MY_SECURE_KEY'
## 设为 True 可支持非密钥(unsafe)访问
## Indicates if the /unsafe URL should be available
## Defaults to: True
ALLOW_UNSAFE_URL = True
## Indicates if encrypted (old style) URLs should be allowed
## Defaults to: True
ALLOW_OLD_URLS = True
## AWS 设置
## AWS access keys - used in thumbor_aws storage
AWS_ACCESS_KEY = ''
AWS_SECRET_KEY = ''
################################################################################
##################################### HTTP #####################################
## Enables automatically generated etags
## Defaults to: True
ENABLE_ETAGS = True
################################################################################
################################### Storage ####################################
## 图片ID最大长度
## Defaults to: 32
MAX_ID_LENGTH = 32
################################################################################
################################### Metrics ####################################
## 主机监控
## Defaults to: None
STATSD_HOST = None
## 监控端口
## Defaults to: 8125
STATSD_PORT = 8125
## 前缀
## Defaults to: None
STATSD_PREFIX = None
################################################################################
################################# File Loader ##################################
## 加载图片路径
## Defaults to: /tmp
FILE_LOADER_ROOT_PATH = '/data/upload'
################################################################################
################################# HTTP Loader ##################################
## 超时(秒)
## loaded
## Defaults to: 5
HTTP_LOADER_CONNECT_TIMEOUT = 5
## 图片载超时(秒)
## Defaults to: 20
HTTP_LOADER_REQUEST_TIMEOUT = 20
## 开启重试
## Defaults to: True
HTTP_LOADER_FOLLOW_REDIRECTS = True
## 重试次数
## Defaults to: 5
HTTP_LOADER_MAX_REDIRECTS = 5
## 最大并发
## queuing
## Defaults to: 10
HTTP_LOADER_MAX_CLIENTS = 10
## 下面是代理设置
## Indicates whether thumbor should forward the user agent of the requesting user
## Defaults to: False
HTTP_LOADER_FORWARD_USER_AGENT = False
## Default user agent for thumbor http loader requests
## Defaults to: Thumbor/6.3.0
HTTP_LOADER_DEFAULT_USER_AGENT = 'Thumbor/6.3.0'
## The proxy host needed to load images through
## Defaults to: None
HTTP_LOADER_PROXY_HOST = None
## The proxy port for the proxy host
## Defaults to: None
HTTP_LOADER_PROXY_PORT = None
## The proxy username for the proxy host
## Defaults to: None
HTTP_LOADER_PROXY_USERNAME = None
## The proxy password for the proxy host
## Defaults to: None
HTTP_LOADER_PROXY_PASSWORD = None
## 下面是 HTTPS 设置
## The filename of CA certificates in PEM format
## Defaults to: None
HTTP_LOADER_CA_CERTS = None
## Validate the servers certificate for HTTPS requests
## Defaults to: True
HTTP_LOADER_VALIDATE_CERTS = True
## The filename for client SSL key
## Defaults to: None
HTTP_LOADER_CLIENT_KEY = None
## The filename for client SSL certificate
## Defaults to: None
HTTP_LOADER_CLIENT_CERT = None
## If the CurlAsyncHTTPClient should be used
## Defaults to: False
HTTP_LOADER_CURL_ASYNC_HTTP_CLIENT = False
################################################################################
################################# File Storage #################################
## 图像缓存到期时间,默认一个月,单位(秒)
## Defaults to: 2592000
STORAGE_EXPIRATION_SECONDS = 259200
## Indicates whether thumbor should store the signing key for each image in the
## file storage. This allows the key to be changed and old images to still be
## properly found
## Defaults to: False
STORES_CRYPTO_KEY_FOR_EACH_IMAGE = False
## 缓存路径
## Defaults to: /tmp/thumbor/storage
FILE_STORAGE_ROOT_PATH = '/data/storage'
################################################################################
#################################### Upload ####################################
## 是传的最大尺寸
## Aliases: MAX_SIZE
## Defaults to: 0
UPLOAD_MAX_SIZE = 0
## 是否启用文件上传
## Aliases: ENABLE_ORIGINAL_PHOTO_UPLOAD
## Defaults to: False
UPLOAD_ENABLED = False
## The type of storage to store uploaded images with
## Aliases: ORIGINAL_PHOTO_STORAGE
## Defaults to: thumbor.storages.file_storage
UPLOAD_PHOTO_STORAGE = 'thumbor.storages.file_storage'
## 是否可以删除图片
## Aliases: ALLOW_ORIGINAL_PHOTO_DELETION
## Defaults to: False
UPLOAD_DELETE_ALLOWED = False
## 是否可以覆盖图片
## Aliases: ALLOW_ORIGINAL_PHOTO_PUTTING
## Defaults to: False
UPLOAD_PUT_ALLOWED = False
## 默认文件名
## Defaults to: image
UPLOAD_DEFAULT_FILENAME = 'image'
################################################################################
## 下面是各种其它库的存储配置
############################### MongoDB Storage ################################
## MongoDB storage server host
## Defaults to: localhost
MONGO_STORAGE_SERVER_HOST = 'mongo'
## MongoDB storage server port
## Defaults to: 27017
MONGO_STORAGE_SERVER_PORT = 27017
## MongoDB storage server database name
## Defaults to: thumbor
MONGO_STORAGE_SERVER_DB = 'thumbor'
## MongoDB storage image collection
## Defaults to: images
MONGO_STORAGE_SERVER_COLLECTION = 'images'
################################################################################
################################ Redis Storage #################################
## Redis storage server host
## Defaults to: localhost
REDIS_STORAGE_SERVER_HOST = 'redis'
## Redis storage server port
## Defaults to: 6379
REDIS_STORAGE_SERVER_PORT = 6379
## Redis storage database index
## Defaults to: 0
REDIS_STORAGE_SERVER_DB = 0
## Redis storage server password
## Defaults to: None
REDIS_STORAGE_SERVER_PASSWORD = None
################################################################################
################################ Redis Result Storage #################################
## Redis storage server host
## Defaults to: localhost
REDIS_RESULT_STORAGE_SERVER_HOST = 'redis'
## Redis storage server port
## Defaults to: 6379
REDIS_RESULT_STORAGE_SERVER_PORT = 6379
## Redis storage database index
## Defaults to: 0
REDIS_RESULT_STORAGE_SERVER_DB = 0
## Redis storage server password
## Defaults to: None
REDIS_RESULT_STORAGE_SERVER_PASSWORD = None
################################################################################
############################### Memcache Storage ###############################
## List of Memcache storage server hosts
## Defaults to: ['localhost:11211']
MEMCACHE_STORAGE_SERVERS = ['localhost:11211']
################################################################################
################################ Mixed Storage #################################
## Mixed Storage file storage. This must be the full name of a python module
## (python must be able to import it)
## Defaults to: thumbor.storages.no_storage
MIXED_STORAGE_FILE_STORAGE = 'thumbor.storages.no_storage'
## Mixed Storage signing key storage. This must be the full name of a python
## module (python must be able to import it)
## Defaults to: thumbor.storages.no_storage
MIXED_STORAGE_CRYPTO_STORAGE = 'thumbor.storages.no_storage'
## Mixed Storage detector information storage. This must be the full name of a
## python module (python must be able to import it)
## Defaults to: thumbor.storages.no_storage
MIXED_STORAGE_DETECTOR_STORAGE = 'thumbor.storages.no_storage'
################################################################################
##################################### Meta #####################################
## The callback function name that should be used by the META route for JSONP
## access
## Defaults to: None
META_CALLBACK_NAME = None
################################################################################
################################## Detection ###################################
## 人脸识别
## List of detectors that thumbor should use to find faces and/or features. All
## of them must be full names of python modules (python must be able to import
## it)
## Defaults to: []
#DETECTORS = [
#'thumbor.detectors.queued_detector.queued_complete_detector',
#'thumbor.detectors.queued_detector.queued_face_detector',
#'thumbor.detectors.queued_detector.queued_feature_detector',
#'thumbor.detectors.feature_detector',
#'thumbor.detectors.face_detector',
#]
DETECTORS = []
## The cascade file that opencv will use to detect faces
## Defaults to: haarcascade_frontalface_alt.xml
FACE_DETECTOR_CASCADE_FILE = 'haarcascade_frontalface_alt.xml'
## The cascade file that opencv will use to detect glasses.
## Defaults to: 'haarcascade_eye_tree_eyeglasses.xml'
GLASSES_DETECTOR_CASCADE_FILE = 'haarcascade_eye_tree_eyeglasses.xml'
## The cascade file that opencv will use to detect profile faces.
## Defaults to: 'haarcascade_profileface.xml'
PROFILE_DETECTOR_CASCADE_FILE = 'haarcascade_profileface.xml'
################################################################################
################################## Optimizers ##################################
## List of optimizers that thumbor will use to optimize images
## Defaults to: [] --> ['thumbor.optimizers.jpegtran',]
OPTIMIZERS = []
## Path for the jpegtran binary
## Defaults to: /usr/bin/jpegtran
JPEGTRAN_PATH = '/usr/bin/jpegtran'
PROGRESSIVE_JPEG = True
FFMPEG_PATH = '/usr/bin/ffmpeg' # Default path for the docker installation in debian
################################################################################
################################### Filters ####################################
## 图片过滤器
## List of filters that thumbor will allow to be used in generated images. All of
## them must be full names of python modules (python must be able to import
## it)
## Defaults to: ['thumbor.filters.brightness', 'thumbor.filters.contrast', 'thumbor.filters.rgb', 'thumbor.filters.round_corner', 'thumbor.filters.quality', 'thumbor.filters.noise', 'thumbor.filters.watermark', 'thumbor.filters.equalize', 'thumbor.filters.fill', 'thumbor.filters.sharpen', 'thumbor.filters.strip_icc', 'thumbor.filters.frame', 'thumbor.filters.grayscale', 'thumbor.filters.rotate', 'thumbor.filters.format', 'thumbor.filters.max_bytes', 'thumbor.filters.convolution', 'thumbor.filters.blur', 'thumbor.filters.extract_focal', 'thumbor.filters.no_upscale']
FILTERS = ['thumbor.filters.brightness', 'thumbor.filters.contrast', 'thumbor.filters.rgb', 'thumbor.filters.round_corner', 'thumbor.filters.quality', 'thumbor.filters.noise', 'thumbor.filters.watermark', 'thumbor.filters.equalize', 'thumbor.filters.fill', 'thumbor.filters.sharpen', 'thumbor.filters.strip_icc', 'thumbor.filters.frame', 'thumbor.filters.grayscale', 'thumbor.filters.rotate', 'thumbor.filters.format', 'thumbor.filters.max_bytes', 'thumbor.filters.convolution', 'thumbor.filters.blur', 'thumbor.filters.extract_focal', 'thumbor.filters.no_upscale']
################################################################################
################################ Result Storage ################################
## Expiration in seconds of generated images in the result storage
## Defaults to: 0
RESULT_STORAGE_EXPIRATION_SECONDS = 0
## Path where the Result storage will store generated images
## Defaults to: /tmp/thumbor/result_storage
RESULT_STORAGE_FILE_STORAGE_ROOT_PATH = '/data/result_storage'
## Indicates whether unsafe requests should also be stored in the Result Storage
## Defaults to: False
RESULT_STORAGE_STORES_UNSAFE = False
################################################################################
############################ Queued Redis Detector #############################
## Server host for the queued redis detector
## Defaults to: localhost
REDIS_QUEUE_SERVER_HOST = 'redis'
## Server port for the queued redis detector
## Defaults to: 6379
REDIS_QUEUE_SERVER_PORT = 6379
## Server database index for the queued redis detector
## Defaults to: 0
REDIS_QUEUE_SERVER_DB = 0
## Server password for the queued redis detector
## Defaults to: None
REDIS_QUEUE_SERVER_PASSWORD = None
################################################################################
############################# Queued SQS Detector ##############################
## AWS key id
## Defaults to: None
SQS_QUEUE_KEY_ID = None
## AWS key secret
## Defaults to: None
SQS_QUEUE_KEY_SECRET = None
## AWS SQS region
## Defaults to: us-east-1
SQS_QUEUE_REGION = 'us-east-1'
################################################################################
#################################### Errors ####################################
## This configuration indicates whether thumbor should use a custom error
## handler.
## Defaults to: False
USE_CUSTOM_ERROR_HANDLING = False
## Error reporting module. Needs to contain a class called ErrorHandler with a
## handle_error(context, handler, exception) method.
## Defaults to: thumbor.error_handlers.sentry
ERROR_HANDLER_MODULE = 'thumbor.error_handlers.sentry'
## File of error log as json
## Defaults to: None
ERROR_FILE_LOGGER = None
## File of error log name is parametrized with context attribute
## Defaults to: False
ERROR_FILE_NAME_USE_CONTEXT = False
################################################################################
############################### Errors - Sentry ################################
## Sentry thumbor project dsn. i.e.: http://5a63d58ae7b94f1dab3dee740b301d6a:73ee
## a45d3e8649239a973087e8f21f98@localhost:9000/2
## Defaults to:
SENTRY_DSN_URL = ''
################################################################################
################################### General ####################################
## Custom app class to override ThumborServiceApp. This config value is
## overridden by the -a command-line parameter.
## Defaults to: 'thumbor.app.ThumborServiceApp'
APP_CLASS = 'thumbor.app.ThumborServiceApp'
################################################################################
############################## TC_AWS ##########################################
TC_AWS_REGION = 'eu-west-1' # AWS Region
TC_AWS_ENDPOINT = None # Custom S3 endpoint URL (for GCP, Minio, etc.)
TC_AWS_STORAGE_BUCKET = '' # S3 bucket for Storage
TC_AWS_STORAGE_ROOT_PATH = '' # S3 path prefix for Storage bucket
TC_AWS_LOADER_BUCKET = '' #S3 bucket for loader
TC_AWS_LOADER_ROOT_PATH = '' # S3 path prefix for Loader bucket
TC_AWS_RESULT_STORAGE_BUCKET = '' # S3 bucket for result Storage
TC_AWS_RESULT_STORAGE_ROOT_PATH = '' # S3 path prefix for Result storage bucket
# put data into S3 using the Server Side Encryption functionality to
# encrypt data at rest in S3
# https://aws.amazon.com/about-aws/whats-new/2011/10/04/amazon-s3-announces-server-side-encryption-support/
TC_AWS_STORAGE_SSE = False
# put data into S3 with Reduced Redundancy
# https://aws.amazon.com/about-aws/whats-new/2010/05/19/announcing-amazon-s3-reduced-redundancy-storage/
TC_AWS_STORAGE_RRS = False
# Enable HTTP Loader as well?
# This would allow you to load watermarks in over your images dynamically through a URI
# E.g.
# http://your-thumbor.com/unsafe/filters:watermark(http://example.com/watermark.png,0,0,50)/s3_bucket/photo.jpg
TC_AWS_ENABLE_HTTP_LOADER = False
TC_AWS_ALLOWED_BUCKETS = False # List of allowed bucket to be requested
TC_AWS_STORE_METADATA = False # Store result with metadata (for instance content-type)
################################################################################
回到刚才的临时启动窗口,ctrl+c 关闭服务器
正式启动服务器
# /home/upload 是我的图片目录
docker run -d --name thumbor -v /opt/thumbor/app:/usr/src/app -v /opt/thumbor/docker-entrypoint.sh:/docker-entrypoint.sh -v /home/upload:/data/upload -p 8888:8000 --restart=always docker.io/apsl/thumbor
启动成功后配置nginx反向代理
server {
listen 80;
#域名记得要修改
server_name img2.test.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 150;
proxy_send_timeout 100;
proxy_read_timeout 100;
proxy_pass http://127.0.0.1:8888$request_uri;
}
}
使用方法
http://localhost/hmac/trim/AxB:CxD/(adaptive-)(full-)fit-in/-Ex-F/HALIGN/VALIGN/smart/filters:FILTERNAME(ARGUMENT):FILTERNAME(ARGUMENT)/image-uri
localhost:服务器的地址
hmac:安全认证(unsafe:不安全认证)
trim:使用图片左上角像素 AxB:CxD:对图片的剪裁,AxB 是左上角坐标,CxD 是右下角坐标
-Ex-F:意味着调整图像 ExF 宽度/高度的大小。负号表示水平方向和垂直方向翻转
HALIGN:水平 VALIGN:垂直
smart:智能检测要点
filter:过滤要使用的图片
image-uri:图片的存储名称 加入‘meta’可查看图片的具体信息,以 json 形式返回: http://localhost/unsafe/meta/300x200/https://www.baidu.com/img/superlogo_c4d7df0a003d3db9b65e9ef0fe6da1ec.png
返回 json 文件:
# 以下是示例数据
{
"thumbor":
{
"operations": [
{"top": 0, "right": 1345, "type": "crop","bottom": 859, "left": 56},
{"width": 300.0, "type": "resize", "height": 200.0},
{"type": "flip_horizontally"},
{"type": "flip_vertically"}
],
"source":
{
"url": "https://www.baidu.com/img/superlogo_c4d7df0a003d3db9b65e9ef0fe6da1ec.png",
"width": 540, "frameCount": 1, "height":258
},
"focal_points": [
{"origin": "alignment", "height": 1, "width": 1, "y": 429.5, "x": 700.0, "z": 1.0 }
],
"target": {"width": 300.0, "height": 200.0}
}
}
上传图片
上传图片有两种方式,一种是新的 API 形式,一种是旧的 API 形式,这里介绍新的 API 形式上传图片。
在配置文件里(thumbor.conf)
配置参数 | 默认 | 描述 |
---|---|---|
UPLOAD_ENABLED | False | 是否支持上传 |
UPLOAD_PUT_ALLOWED | False | 是否允许修改 |
UPLOAD_DELETE_ALLOWED | False | 是否允许删除 |
UPLOAD_PHOTO_STORAGE | thumbor.storages.file_stora ge | 图片存储形式 |
UPLOAD_DEFAULT_FILENAME | image | 图片默认名 |
UPLOAD_MAX_SIZE | 0 | 图片最大大小 |
MIN_WIDTH | 0 | 最小宽度 |
MIN_HEIGHT | 1 | 最小高度 |
新的 API 是请求方式如下:
A.上传图片:POST 示例: curl -i -H "Content-Type: image/jpeg" -H "Slug: photo.jpg" -XPOST http://localhost/image --data-binary "@vows/crocodile.jpg"
B.修改图片:PUT 示例: curl -i -H "Content-Type: image/jpeg" -H "Slug: modified_image.jpg" -XPUT http://localhost-server/image/05b2eda857314e559630c6f3334d818d/photo.jpg --data-binary "@vows/crocodile.jpg"
C.删除图片:DELELT 示例: curl -i -XDELETE http://localhost-server/image/05b2eda857314e559630c6f3334d818d/modified_image.jpg
D.查看图片:GET 示例: curl -i -XGET http://localhost-server/unsafe/500x400/1.jpg
当然也可以使用表单提交的形式来操作。
图片的自动检测功能(人脸识别)
# 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
在配置文件中加入以下内容:
DETECTORS = [ 'thumbor.detectors.face_detector', 'thumbor.detectors.feature_detector', ]
那么就有了对图片中的人脸和一些特性的自动检测。 还有一些其他的检测器可用,也可以自定义检测器。
过滤功能
可以根据某个过滤器,对原图片进行一些处理,比如亮度不够,色彩不鲜明等处理后返 回给页面显示,以达到想要的效果。当然,这个过滤器也是可以自定义的。
图片亮度调整 格式:filters:brightness(amount)
解释:amount 的范围是-100~100 的整数,对应效果暗~亮
示例:http://localhost:8888/unsafe/400x500/filters:brightness(10)/1.jpg
图片对比度 格式:filters:contrast(amount)
解释:amount 的范围是-100~100 的整数
示例:http://localhost:8888/unsafe/400x500/filters:contrast(20)/1.jpg
图片着色 格式:filters:colorize(redPercentage, greenPercentage, bluePercentage, fillColor)
解释:redPercentage 填充红色混合像素的百分比,取值范围是-100~100 的整数 greenPercentage 填充绿色混合像素的百分比,取值范围是-100~100 的整数 bluePercentage 填充蓝色混合像素的百分比,取值范围是-100~100 的整数 fillColor 填充色,取值范围是十六进制数值
示例:http://localhost:8888/unsafe/400x500/filters:colorize(20)/1.jpg
图片颜色均衡 格式:filters:equalize()
解释:均衡颜色
示例:http://localhost:8888/unsafe/400x500/filters:equalize()/1.jpg
图片填充色 格式:filters:fill(color[,fill_transparent])
解释:color 填充色,取值范围是十六进制数值或颜色的单词 fill_transparent 是否填充,取值 true,false,1,0 默认是 false 按照尺寸显示照片,图片缺少的尺寸部分填充颜色
示例:http://localhost:8888/unsafe/400x500/filters:fill(blue,1)/1.jpg
图片格式 格式:filters:format(webp)
解释:webp 可选的图片格式,取值范围是 jpeg,gif,png 按照格式显示图片
示例:http://localhost:8888/unsafe/400x500/filters:format(png)/1.jpg
提取动态图片(速度很慢) 格式:filters:extract_cover()
解释:提取动态图片的第一帧图样
示例:http://localhost:8888/unsafe/400x500/filters:extract_cover()/2.gif
图片灰度 格式:filters:grayscale()
解释:将图片变成灰色
示例:http://localhost:8888/unsafe/400x500/filters:grayscale()/1.jpg
图片字节 格式:filters:max_bytes ()
解释:根据图片的字节大小调节图片质量
示例:http://localhost:8888/unsafe/400x500/filters:max_bytes(1000)/1.jpg
图片噪点 格式:filters:noise (amount)
解释:amount 取值范围 0~100,值越大,噪点越多
示例:http://localhost:8888/unsafe/400x500/filters:noise(20)/1.jpg
图片低品质 格式:filters:no_upscale()
解释:当要显示的图片尺寸大于图片原来的尺寸,要显示图片的原尺寸 示例:http://localhost:8888/unsafe/800x1000/filters:no_upscale()/1.jpg
图片质量 格式:filters:quality(amount)
解释:amount 取值范围 0~100,质量越高越清晰,只使用于 jpeg 图片
示例:http://localhost:8888/unsafe/800x1000/filters:quality(90)/1.jpg
图片色彩 格式:filters:rgb(r,g,b)
解释:r,g,b 取值范围-100~100,分别对应红,绿,蓝颜色的百分比
示例:http://localhost:8888/unsafe/800x1000/filters:rgb(90,-20,30)/1.jpg
图片四边圆角处理 格式:filters:round_corner(a|b,r,g,b)
解释:a|b 是半径大小 r,g,b 取值范围 0~255,分别对应红,绿,蓝颜色的百分比
示例:http://localhost:8888/unsafe/800x1000/filters:round_corner(20,0,0,0)/1.jpg
图片旋转 格式:filters:rotate(angle)
解释:angle 是旋转的度数,取值为 90 的倍数,按大小逆时针旋转
示例:http://localhost:8888/unsafe/800x1000/filters:rotate(90)/1.jpg
图片饱和度 格式:filters:saturation(angle)
解释:angle 饱和度度数,取值为 0.0~2.0,由低到高
示例:http://localhost:8888/unsafe/800x1000/filters:saturation(0.5)/1.jpg
图片锐化 格式:filters:sharpen(sharpen_amount,sharpen_radius,luminance_only)
解释:sharpen_amount ,取值为 0.0~10.0, 锐化量 sharpen_radius 取值 0.0~2.0, 锐化半径 luminance_only 取值 true,false,是否锐化亮度
示例:http://localhost:8888/unsafe/800x1000/filters:sharpen(2,1.0,true)/1.jpg
图片删除 icc 信息 格式:filters:strip_icc()
解释:图片删除 icc 信息,图片有可能变小或失真
示例:http://localhost:8888/unsafe/800x1000/filters:strip_icc()/1.jpg
图片水印 格式:filters:watermakr(imageUrl,x,y,alpha)
解释:imageUrl 要水印图片的 url 地址 x 水平方向的坐标 y 垂直方向的坐标 alpha 水印图片的透明度,取值范围 0~100 图片添加水印
示例:http://localhost:8888/unsafe/filters:watermark(3.jpg,-10,-10,0)/1.jpg
图片模糊 格式:filters:blur(rate[,sigma])
解释:rate 模糊率,取值范围 0~150 sigma 使用高斯函数 使图片模糊
示例:http://localhost:8888/unsafe/800x1000/filters:blur(3)/1.jpg
图片提取要点 格式:filters:extract_focal()
图片转换成 mp4 Html 格式:filters:gifv()
解释:将 gif 图片转换成 mp4 格式和 webm 格式
配置:
OPTIMIZERS = [ 'thumbor.optimizers.gifv', ]
示例:http://localhost:8888/unsafe/800x1000/filters:gifv()/1.jpg
图片 RGB 扭曲 格式:filters:curve(curve_all, curve_r, curve_g, curve_b)
解释:curve_all 扭曲所有色调 curve_r 扭曲红色 curve_g 扭曲绿色 curve_b 扭曲蓝色
示例: http://localhost:8888/unsafe/800x1000/filters:curve([(0,0),(40,59),(255,255)],[(32,59),(64,80),(92,111),(128,153),(140,169),(164,201),(192,214),(224,215),(240,214),(255,212)],[(34,41),(64,51),(92,76),(128,112),(140,124),(164,147),(192,180),(224,216),(240,236),(255,255)],[(40,46),(64,55),(92,83),(128,127),(140,144),(164,174),(192,197),(224,199),(240,197),(255,198)])/1.jpg
矩阵卷积 格式:filters:convolution(matrix_items, number_of_columns, should_normalize)
解释:matrix_items 用分号分隔矩阵的数字 number_of_columns 矩阵的列数字 should_normalize 是否计算矩阵总和
示例: http://localhost:8888/unsafe/filters:convolution(-1;-1;-1;-1;8;-1;-1;-1;-1,3,false)/htt p://upload.wikimedia.org/wikipedia/commons/5/50/1.jpg
更多参考资料
https://github.com/thumbor/thumbor/wiki/Configuration 配置文件
https://github.com/thumbor/thumbor/wiki/Requirements 安装必看
https://github.com/thumbor/thumbor/wiki/Usage 使用介绍
https://github.com/thumbor/thumbor/wiki/Optimizers
总结
优点:
1.thumbor 可以实现动态,灵活的对图片处理
2.thumbor 可以实现对图片的复杂操作处理
3.thumbor 可以节省网络流量
缺点:
1.thumbor 的服务器使用内存较大
PHP 的签名生成方法
url, signature),'/+', '_-');
PHP 示例
signature),'/+', '-').'/200x100/2018/05/22/20180522164126.jpeg';