fostercommerce/imgproxy 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fostercommerce/imgproxy

最新稳定版本:1.0.3

Composer 安装命令:

composer require fostercommerce/imgproxy

包简介

A PHP library for generating imgproxy URLs with processing options.

README 文档

README

Important

This package is no longer actively maintained. The Foster Commerce team has transitioned our projects over to Small Pics using their official PHP client.

imgproxy-php

A PHP library for working with imgproxy. This package provides a simple way to build URLs with imgproxy processing options.

Installation

composer require fostercommerce/imgproxy-php

Usage

Creating Processing Options

use fostercommerce\imgproxy\Options;

// Create a new Options object
$options = new Options();

// Set options with individual setters
$options->setWidth(300)
    ->setHeight(400)
    ->setResizingType('fill')
    ->setGravity('sm');

// Get string representation for URL
echo $options->toString();
// Output: "width:300/height:400/resizing_type:fill/gravity:sm"

// Options object can also be used directly in a string context
echo $options;
// Output: "width:300/height:400/resizing_type:fill/gravity:sm"

Alternative Initialization

It is possible to initialize the Options object with an associative array. The keys of the array are the option names and the values are the option values.

The values can either be a single value, an array of values, or an associative array of values.

  • When a single value is used, it is passed as the first argument to the setter method.
  • When an array of values is used, they are destructured as passed as arguments in order to the setter method.
  • When an associative array is used, the key/value pairs are destructured and passed in as named arguments to the setter method.
// Create Options object with array of values
$options = new Options([
    'width' => 300,
    'height' => 400,
    'resize' => [
        'width' => 300,
        'height' => 400,
        'enlarge' => false,
        'resize_type' => 'fill',
    ],
    'gravity' => 'sm',
    'png_options' => [
        'interlaced' => true,
        'quantize' => false,
    ],
]);

Building URLs

use fostercommerce\imgproxy\Options;
use fostercommerce\imgproxy\UrlBuilder;

// Create options object
$options = new Options();
$options->setPreset('sharp')
    ->setResize('fill', 300, 400, false)
    // or
    ->setResize([
        'width' => 300,
        'height' => 400,
        'enlarge' => false,
        'resize_type' => 'fill',
    ])
    ->setGravity('sm')
    ->setQuality(80)
    ->setFormat('png');

// Create URL builder (without signing, with plain URLs)
$builder = new UrlBuilder('https://imgproxy.example.com', null, null, false);

// Build URL
$imageUrl = 'https://example.com/images/image.jpg';
$url = $builder->buildUrl($imageUrl, $options);

echo $url;
// Output: https://imgproxy.example.com/unsafe/preset:sharp/resize:fill:300:400:0/gravity:sm/quality:80/format:png/plain/https://example.com/images/image.jpg

Encoded URLs

use fostercommerce\imgproxy\Options;
use fostercommerce\imgproxy\UrlBuilder;

// Create options object
$options = new Options();
$options->setWidth(300)
    ->setHeight(400)
    ->setResizingType('fill');

// Create URL builder with base64 encoding (default behavior)
$builder = new UrlBuilder('https://imgproxy.example.com', null, null, true);

// Build URL with encoded source
$imageUrl = 'https://example.com/images/image.jpg';
$url = $builder->buildUrl($imageUrl, $options);

echo $url;
// Output: https://imgproxy.example.com/unsafe/width:300/height:400/resizing_type:fill/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZXMvaW1hZ2UuanBn

// With extension
$url = $builder->buildUrl($imageUrl, $options, 'png');
echo $url;
// Output: https://imgproxy.example.com/unsafe/width:300/height:400/resizing_type:fill/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZXMvaW1hZ2UuanBn.png

Signed URLs

// Create URL builder with signing keys (using plain URLs)
$key = '0123456789abcdef0123456789abcdef';
$salt = 'fedcba9876543210fedcba9876543210';
$builder = new UrlBuilder('https://imgproxy.example.com', $key, $salt, false);

// Build signed URL
$imageUrl = 'https://example.com/images/image.jpg';
$url = $builder->buildUrl($imageUrl, $options, 'png');

echo $url;
// Output will include a signature: https://imgproxy.example.com/[signature]/preset:sharp/resize:fill:300:400:0/gravity:sm/quality:80/format:png/plain/https://example.com/images/image.jpg@png

Signed and Encoded URLs

// Create URL builder with signing keys and base64 encoding
$key = '0123456789abcdef0123456789abcdef';
$salt = 'fedcba9876543210fedcba9876543210';
$builder = new UrlBuilder('https://imgproxy.example.com', $key, $salt, true);

// Build signed URL with encoded source
$imageUrl = 'https://example.com/images/image.jpg';
$url = $builder->buildUrl($imageUrl, $options);

echo $url;
// Output will include a signature: https://imgproxy.example.com/[signature]/preset:sharp/resize:fill:300:400:0/gravity:sm/quality:80/format:png/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZXMvaW1hZ2UuanBn

Development

Testing

This package uses Pest PHP for testing. To run the tests:

composer test

or

vendor/bin/pest

fostercommerce/imgproxy 适用场景与选型建议

fostercommerce/imgproxy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.97k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「image」 「library」 「processing」 「imgproxy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 fostercommerce/imgproxy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fostercommerce/imgproxy 我们能提供哪些服务?
定制开发 / 二次开发

基于 fostercommerce/imgproxy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 22
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-21