Skip to content

Repository files navigation

ImgProxy PHP

A comprehensive PHP library for building URLs for ImgProxy, supporting all features including pro options.

This library is based on and extends the work of the onliner/imgproxy-php project, adding support for all ImgProxy features including Pro functionality.

VersionTotal DownloadsPhpLicenseBuild Status

Installation

The preferred way to install this extension is through composer.

composer require mperonnet/imgproxy-php

Features

  • Full support for all ImgProxy options (including Pro features)
  • URL signing and encryption
  • Base64 and plain URL encoding
  • Support for info endpoint
  • Support for presets
  • Support for chained pipelines (Pro)
  • Type-safe option builders
  • Immutable, fluent interface

Basic Usage

useMperonnet\ImgProxy\UrlBuilder;
useMperonnet\ImgProxy\Options\Width;
useMperonnet\ImgProxy\Options\Height;
useMperonnet\ImgProxy\Options\Quality;
useMperonnet\ImgProxy\Options\Dpr;
$key = getenv('IMGPROXY_KEY');
$salt = getenv('IMGPROXY_SALT');
$src = 'http://example.com/image.jpg';
$builder = UrlBuilder::signed($key, $salt);
$builder = $builder->with(
newDpr(2),
newQuality(90),
newWidth(300),
newHeight(400)
);
$url = $builder->url($src); // Base64-encoded URL$url = $builder->usePlain()->url($src); // Plain URL$url = $builder->url($src, 'png'); // Change image format

URL Formats

Base64-encoded URL (default)

$url = $builder->useBase64()->url($src);
// Example: /9SaGqJILqstFsWthdP/dpr:2/q:90/w:300/h:400/aHR0cDovL2V4YW1wL2ltYWdlLmpwZw

Plain URL

$url = $builder->usePlain()->url($src);
// Example: /9SaGqJILqstFsWthdP/dpr:2/q:90/w:300/h:400/plain/http://example.com/image.jpg

Encrypted URL (Pro)

$encryptionKey = getenv('IMGPROXY_ENCRYPTION_KEY');
$url = $builder->useEncryption($encryptionKey)->url($src);
// Example: /9SaGqJILqstFsWthdP/dpr:2/q:90/w:300/h:400/enc/a1b2c3d4e5f6g7h8i9j0...

Advanced Features

Gravity

useMperonnet\ImgProxy\Options\Gravity;
useMperonnet\ImgProxy\Support\GravityType;
// Basic gravity$builder->with(newGravity(GravityType::CENTER));
// With offset$builder->with(newGravity(GravityType::NORTH_WEST, 10, 20));
// Smart gravity$builder->with(Gravity::smart());
// Focus point gravity$builder->with(Gravity::focusPoint(0.7, 0.3));
// Object detection gravity (Pro)$builder->with(Gravity::object(['face', 'cat']));
// Weighted object detection gravity (Pro)$builder->with(Gravity::objectWeighted(['face' => 2, 'cat' => 1]));

Watermark

useMperonnet\ImgProxy\Options\Watermark;
useMperonnet\ImgProxy\Options\WatermarkText;
useMperonnet\ImgProxy\Options\WatermarkUrl;
// Basic watermark$builder->with(Watermark::center(0.5, 10, 10, 0.3));
// Replicated watermark$builder->with(Watermark::replicate(0.5));
// Custom watermark URL (Pro)$builder->with(newWatermarkUrl('http://example.com/watermark.png'));
// Text watermark (Pro)$builder->with(newWatermarkText('© Example Corporation'));

Info Endpoint (Pro)

useMperonnet\ImgProxy\Options\InfoOptions;
// Basic info$infoUrl = $builder->info()->with(InfoOptions::basic())->url($src);
// Complete info$infoUrl = $builder->info()->with(InfoOptions::complete())->url($src);
// Custom info$infoUrl = $builder->info()->with(
newInfoOptions(
true, // sizetrue, // formattrue, // dimensionsfalse, // exiffalse, // iptcfalse, // xmptrue, // video_metatrue// detect_objects
)
)->url($src);

Chained Pipelines (Pro)

useMperonnet\ImgProxy\Options\Resize;
useMperonnet\ImgProxy\Options\Blur;
useMperonnet\ImgProxy\Options\Watermark;
// First resize, then blur and add watermark$url = $builder
->with(newResize('fit', 300, 300))
->pipeline(
newBlur(10),
Watermark::southEast(0.7)
)
->url($src);

Additional Processing Options

Resizing and Dimensions

useMperonnet\ImgProxy\Options\Resize;
useMperonnet\ImgProxy\Options\Size;
useMperonnet\ImgProxy\Options\Width;
useMperonnet\ImgProxy\Options\Height;
useMperonnet\ImgProxy\Options\MinWidth;
useMperonnet\ImgProxy\Options\MinHeight;
useMperonnet\ImgProxy\Options\Zoom;
useMperonnet\ImgProxy\Options\Dpr;
$builder->with(
newResize('fill', 300, 400, true, false),
newSize(300, 400, true, false),
newWidth(300),
newHeight(400),
newMinWidth(200),
newMinHeight(200),
newZoom(1.5),
newDpr(2)
);

Image Adjustments

useMperonnet\ImgProxy\Options\Adjust;
useMperonnet\ImgProxy\Options\Brightness;
useMperonnet\ImgProxy\Options\Contrast;
useMperonnet\ImgProxy\Options\Saturation;
$builder->with(
newAdjust(10, 1.1, 0.9),
newBrightness(10),
newContrast(1.1),
newSaturation(0.9)
);

Color Effects

useMperonnet\ImgProxy\Options\Background;
useMperonnet\ImgProxy\Options\Blur;
useMperonnet\ImgProxy\Options\Sharpen;
useMperonnet\ImgProxy\Options\Monochrome;
useMperonnet\ImgProxy\Options\Duotone;
useMperonnet\ImgProxy\Options\Colorize;
useMperonnet\ImgProxy\Options\Gradient;
useMperonnet\ImgProxy\Support\Color;
$builder->with(
newBackground(newColor(255, 255, 255)),
newBlur(5),
newSharpen(0.7),
newMonochrome(0.8, 'b3b3b3'),
newDuotone(0.8, '000000', 'ffffff'),
newColorize(0.5, '3498db', true),
newGradient(0.7, 'ff0000', 45, 0.2, 0.8)
);

Format and Quality

useMperonnet\ImgProxy\Options\Format;
useMperonnet\ImgProxy\Options\Quality;
useMperonnet\ImgProxy\Options\FormatQuality;
useMperonnet\ImgProxy\Options\Autoquality;
useMperonnet\ImgProxy\Options\MaxBytes;
$builder->with(
newFormat('webp'),
newQuality(85),
newFormatQuality([
'jpeg' => 85,
'webp' => 80,
'avif' => 60
]),
Autoquality::dssim(0.02, 70, 85, 0.001),
newMaxBytes(100000)
);

Object Detection (Pro)

useMperonnet\ImgProxy\Options\BlurDetections;
useMperonnet\ImgProxy\Options\DrawDetections;
$builder->with(
newBlurDetections(5, ['face']),
newDrawDetections(true, ['face', 'cat'])
);

Credits

This library is an extension of the onliner/imgproxy-php project with added support for ImgProxy Pro features and a more comprehensive API. We're grateful to the original authors for providing the foundation that made this extension possible.

License

Released under the MIT license.

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages