定制 fostercommerce/imgproxy 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱: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

统计信息

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

GitHub 信息

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

其他信息

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