PHP code example of guillaumetissier / image-resizer

1. Go to this page and download the library: Download guillaumetissier/image-resizer 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/ */

    

guillaumetissier / image-resizer example snippets


use Guillaumetissier\ImageResizer\ImageResizer;
use Guillaumetissier\ImageResizer\Constants\Transformations;
use Guillaumetissier\ImageResizer\Constants\Options;

// Create resizer with safe configuration
$resizer = ImageResizer::create();

// Resize image
$resizer
    ->setTransformation(Transformations::SET_RATIO, 0.8)
    ->setOption(Options::QUALITY, 85)
    ->resize('source.jpg', 'output.jpg');

use Guillaumetissier\ImageResizer\Constants\ResizeType;
use Guillaumetissier\ImageResizer\ImageResizer;
use Guillaumetissier\ImageResizer\Constants\Transformations;

$resizer = ImageResizer::create();
// Resize by aspect ratio (default)
$resizer
    ->setTransformation(Transformations::SET_RATIO, 2)
    ->resize('image.jpg', 'resized.jpg');
    
// Resize by width (maintains aspect ratio)
$resizer
    ->setResizeType(ResizeType::FIXED_WIDTH)
    ->setTransformation(Transformations::SET_WIDTH, 800)
    ->resize('image.jpg', 'resized.jpg');

// Resize by height (maintains aspect ratio)
$resizer
    ->setResizeType(ResizeType::FIXED_HEIGHT)
    ->setTransformation(Transformations::SET_HEIGHT, 600)
    ->resize('image.jpg', 'resized.jpg');

// Resize with fixed height and width
$resizer
    ->setResizeType(ResizeType::FIXED)
    ->setTransformation(Transformations::SET_HEIGHT, 600)
    ->setTransformation(Transformations::SET_WIDTH, 300)
    ->resize('image.jpg', 'resized.jpg');

$resizer->setTransformations([
    Transformations::SET_WIDTH->value => 1920,
    Transformations::SET_HEIGHT->value => 1080,
]);

$resizer->resize('source.jpg', 'output.jpg');

use Guillaumetissier\ImageResizer\Constants\Options;

$resizer = ImageResizer::create()
    ->setTransformation(Transformations::SET_RATIO, 0.5)
    ->setOption(Options::QUALITY, 85)           // JPEG quality (0-100)
    ->setOption(Options::INTERLACE, true);      // Progressive JPEG

// Or set multiple options at once
$resizer->setOptions([
    Options::QUALITY->value => 90,
    Options::INTERLACE->value => true,
]);

$resizer->resize('image.jpg', 'output.jpg');

ImageResizer::create()
    ->setType(ResizeType::FIXED)
    ->setTransformation(Transformations::SET_WIDTH, 1920)
    ->setTransformation(Transformations::SET_HEIGHT, 1080)
    ->setOption(Options::QUALITY, 85)
    ->setOption(Options::INTERLACE, true)
    ->resize('input.jpg', 'output.jpg');

use Guillaumetissier\ImageResizer\ImageResizerConfig;

// Default configuration
$resizer = ImageResizer::create(ImageResizerConfig::default());

// Safe configuration - for public websites with user uploads
$resizer = ImageResizer::create(ImageResizerConfig::safe());

// Strict configuration - for high-security environments
$resizer = ImageResizer::create(ImageResizerConfig::strict());

// Thumbnail configuration - optimized for small images
$resizer = ImageResizer::create(ImageResizerConfig::thumbnail());

// Web configuration - optimized for web display
$resizer = ImageResizer::create(ImageResizerConfig::web());

// Print configuration - optimized for print quality
$resizer = ImageResizer::create(ImageResizerConfig::print());

use Guillaumetissier\ImageResizer\ImageResizerConfig;

$config = new ImageResizerConfig([
    'minWidth' => 100,              // Minimum width in pixels
    'maxWidth' => 3000,             // Maximum width in pixels
    'minHeight' => 100,             // Minimum height in pixels
    'maxHeight' => 3000,            // Maximum height in pixels
    'minRatio' => 0.5,              // Minimum aspect ratio (width/height)
    'maxRatio' => 2.0,              // Maximum aspect ratio
    'minQuality' => 60,             // Minimum JPEG quality (0-100)
    'maxQuality' => 95,             // Maximum quality
    'defaultQuality' => 85,         // Default quality if not specified
    'defaultInterlace' => true,     // Enable progressive JPEG by default
]);

$resizer = ImageResizer::create($config);

try {
    $resizer = ImageResizer::create(ImageResizerConfig::safe());
    
    // This will throw InvalidRangeException if width > 8000
    $resizer->setTransformation(Transformations::SET_WIDTH, 10000);
    
} catch (\Guillaumetissier\ImageResizer\Exceptions\InvalidRangeException $e) {
    echo "Validation error: " . $e->getMessage();
}

use Guillaumetissier\ImageResizer\ImageResizerConfig;

$config = ImageResizerConfig::strict();  // Max width: 4000px
$resizer = ImageResizer::create($config)->setType(ResizeType::FIXED_WIDTH);

// ✅ Valid - within limits
$resizer->setTransformation(Transformations::SET_WIDTH, 2000);

// ❌ Invalid - exceeds max width
$resizer->setTransformation(Transformations::SET_WIDTH, 5000);
// Throws: InvalidRangeException

// ❌ Invalid - wrong type
$resizer->setTransformation(Transformations::SET_WIDTH, "2000");
// Throws: InvalidTypeException

// ❌ Invalid - file doesn't exist
$resizer->resize('nonexistent.jpg', 'output.jpg');
// Throws: InvalidPathException

use Guillaumetissier\ImageResizer\Constants\ResizeType;
use Guillaumetissier\ImageResizer\ImageResizer;
use Guillaumetissier\ImageResizer\ImageResizerConfig;
use Guillaumetissier\ImageResizer\Constants\Transformations;
use Guillaumetissier\ImageResizer\Constants\Options;

// Strict validation for user uploads
$resizer = ImageResizer::create(ImageResizerConfig::strict());

try {
    $resizer
        ->setResizeType(ResizeType::FIXED)
        ->setTransformations([
            Transformations::SET_WIDTH => 200,
            Transformations::SET_HEIGHT => 200,
        ])
        ->setOptions([
            Options::QUALITY => 85,
            Options::INTERLACE => true,
        ])
        ->resize($_FILES['avatar']['tmp_name'], 'uploads/avatars/user-123.jpg');
        
    echo "Avatar uploaded successfully!";
    
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

$resizer = ImageResizer::create(ImageResizerConfig::web())
    ->setType(ResizeType::FIXED_WIDTH)
    ->setTransformation(Transformations::SET_WIDTH, 1920)
    ->setOption(Options::QUALITY, 85);

$images = glob('originals/*.jpg');

foreach ($images as $image) {
    $filename = basename($image);
    try {
        $resizer->resize($image, "processed/{$filename}");
        echo "Processed: {$filename}\n";
    } catch (\Exception $e) {
        echo "Error processing {$filename}: {$e->getMessage()}\n";
    }
}

$resizer = ImageResizer::create(ImageResizerConfig::thumbnail())
    ->setType(ResizeType::FIXED_HEIGHT)
    ->setOptions([
        Options::QUALITY => 75,
        Options::INTERLACE => false,
    ]);

// Create multiple thumbnail sizes
$sizes = [
    'small' => 150,
    'medium' => 300,
    'large' => 500,
];

foreach ($sizes as $name => $height) {
    $resizer->setTransformation(Transformations::SET_HEIGHT, $height)
        ->resize('original.jpg', "thumbnails/{$name}.jpg");
}

public static function create(?ImageResizerConfig $config = null): self

// Set a single transformation
public function setTransformation(Transformations $transformation, mixed $value): self

// Set multiple transformations at once
public function setTransformations(array $transformations): self

// Set a single option
public function setOption(Options $option, int|bool|string $value): self

// Set multiple options at once
public function setOptions(array $options): self

// Resize the image
public function resize(string $source, ?string $target = null): void

public static function default(): self
public static function safe(): self
public static function strict(): self
public static function thumbnail(): self
public static function web(): self
public static function print(): self

public function __construct(array $config = [])

Transformations::SET_WIDTH   // Resize by width
Transformations::SET_HEIGHT  // Resize by height
Transformations::SET_RATIO   // Resize by aspect ratio

Options::QUALITY      // Image quality (0-100)
Options::INTERLACE    // Progressive/interlaced (bool)
Options::SCALE_MODE   // Scaling mode (integer)

// ❌ v1.x - No longer works
$resizer = new ImageResizer(...);

// ✅ v2.0 - Use factory method
$resizer = ImageResizer::create();
$resizer = ImageResizer::create(ImageResizerConfig::safe());

// This works in both v1.x and v2.0
$resizer = ImageResizer::create();
$resizer->setTransformation(Transformations::SET_WIDTH, 800);
$resizer->resize('input.jpg', 'output.jpg');