ImgProxy的Python支持
项目描述
ImgProxy
ImgProxy -- 用于构建 ImgProxy URL的Python库
功能
目录
要求
- python >= 3.7
安装
imgproxy 应使用pip安装
$ pip install imgproxy
使用
from imgproxy import ImgProxy
# Create ImgProxy object with required params
img_url = ImgProxy('https://picsum.photos/1000', proxy_host='https://imgproxy.com', width=800, height=400)
# Convert the obj to string to get imgproxy URL
cover: str = str(img_url)
# or just call it to get imgproxy URL
cover: str = img_url()
assert cover == 'https://imgproxy.com/insecure/g:ce/rs:auto:800:400:0/aHR0cHM6Ly9waWNzdW0ucGhvdG9zLzEwMDA'
# Call the object with different params to customize the url
cover_small: str = img_url(width=400, height=200, resizing_type='fill')
assert cover_small == 'https://imgproxy.com/insecure/g:ce/rs:fill:400:200:0/aHR0cHM6Ly9waWNzdW0ucGhvdG9zLzEwMDA'
# Call it with advanced params to get an URL
cover_with_border = img_url('pd:10:10:10:10', 'bg:F00')
assert cover_with_border == 'https://imgproxy.com/insecure/pd:10:10:10:10/bg:F00/g:ce/rs:auto:0:0:0/aHR0cHM6Ly9waWNzdW0ucGhvdG9zLzEwMDA'
选项
基本选项(默认值)
width: int = 0
- 图像宽度height: int = 0
- 图像高度gravity: str = 'ce'
- 图像重力enlarge: bool = False
- 放大图像extension: str = ''
- 图像扩展名resizing_type: str = 'auto'
- 调整大小类型
from imgproxy import ImgProxy
img_url = ImgProxy('https://picsum.photos/1000', proxy_host='https://imgproxy.com')
thumbmail = img_url(width=100, height=100, gravity='no', extension='jpg', enlarge=True, resizing_type='fit')
调用图像proxy实例时也支持其他任何选项
from imgproxy import ImgProxy
img_url = ImgProxy('https://picsum.photos/1000', proxy_host='https://imgproxy.com')
# Get rotated and blured image
blured_rotated = img_url('blur:0.5', 'rotate:30')
签名URL
Imgproxy允许您使用密钥和盐对URL进行签名,因此攻击者无法通过请求多个不同的图像调整大小来造成拒绝服务攻击。
from imgproxy import ImgProxy
url = ImgProxy('https://picsum.photos/1000', proxy_host='https://imgproxy.com', key="aa396160c50ea766910eab53", salt="b3fb8f215827bda5d0e7313d")
assert str(url) == 'https://imgproxy.com/FrH21u_5bXmv-OJ0APMayxZ0F3982xx437gCpqcQ0BM/g:ce/rs:auto:600:0:0/aHR0cHM6Ly9waWNzdW0ucGhvdG9zLzEwMDA'
如果您需要快速生成一个随机的密钥/盐对,您可以使用以下示例片段快速生成,例如:
echo $(xxd -g 2 -l 64 -p /dev/random | tr -d '\n')
图像工厂
通常情况下,imgproxy的主机参数和签名参数在一个项目中是通用的。该库支持一种方法来生成具有预定义参数的工厂。
from imgproxy import ImgProxy
img_factory = ImgProxy.factory(proxy_host='https://imgproxy.com', key="aa396160c50ea766910eab53", salt="b3fb8f215827bda5d0e7313d")
# ...
# Generate image URL
url = img_factory('https://picsum.photos/1000', width=600)
assert str(url) == 'https://imgproxy.com/qcKAFfBJwpiKZ6xt-NT6GXGOGizkeq4sgyfoQ4h-080/g:ce/rs:auto:600:0:0/aHR0cHM6Ly9waWNzdW0ucGhvdG9zLzEwMDA'
用户可以预定义任何基本参数。
thumbnail_factory = ImgProxy.factory(proxy_host='https://imgproxy.com', width=300, height=200)
preview_factory = ImgProxy.factory(proxy_host='https://imgproxy.com', width=500, resizing_type='fit')
# and etc
也支持高级参数。
thumbnail_factory = ImgProxy.factory('bg:F00', 'pd:10:10:10:10', proxy_host='https://imgproxy.com', width=300, height=200)
变更日志
-
2021-11-08: [1.0.0]
- 支持Python 3.10。
- 支持工厂中的高级选项。
-
2021-09-14: [0.4.0]
- 支持Python 3.7。
- 基本格式已被移除(在ImgProxy中已弃用)。
-
2021-04-02: [0.2.3] 稳定发布
错误追踪器
如果您有任何建议、错误报告或不满,请向问题跟踪器报告:https://github.com/klen/imgproxy/issues
贡献
项目开发发生在:https://github.com/klen/imgproxy
许可证
在MIT许可证下授权
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分布
imgproxy-1.0.0.tar.gz (4.6 kB 查看哈希值)
构建分布
imgproxy-1.0.0-py3-none-any.whl (4.5 kB 查看哈希值)
关闭
imgproxy-1.0.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | f2e34b0da688284ad3a1e7ccd907b1038285001832362fb64cd2bd39c6c952e3 |
|
MD5 | 2bc0b17e379ca20647e8e981f6ed92ad |
|
BLAKE2b-256 | cd5b1a12a2c5af1f1c42133b126811cf2f382ac21ed46cd86ca9c5fca338840e |
关闭
imgproxy-1.0.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 18d3686c1b8f976906192e7bbdcd44dca305600e3e338fab68c30ee9a4102ded |
|
MD5 | fa5d7597d432e756828f7a5da68d1d0e |
|
BLAKE2b-256 | e3177188d1a8030966ab01c817e7dffe8dd745098e9ce2998cc966616479179b |