跳转到主要内容

ImgProxy的Python支持

项目描述

ImgProxy

ImgProxy -- 用于构建 ImgProxy URL的Python库

Tests Status PYPI Version Python Versions


功能

  • 支持高级 URL
  • 支持签名 URL
  • 预定义参数的URL工厂

目录

要求

  • 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 查看哈希值)

上传时间: Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面