PHP code example of fostercommerce / imgproxy
1. Go to this page and download the library: Download fostercommerce/imgproxy library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fostercommerce / imgproxy example snippets
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"
// 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,
],
]);
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
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
// 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
// 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
bash
composer