PHP code example of sandstorm / laravel-imagor

1. Go to this page and download the library: Download sandstorm/laravel-imagor 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/ */

    

sandstorm / laravel-imagor example snippets


// Generate URL using helper function
$url = imagor()
    ->resize(width: 400, height: 300)
    ->uriFor('https://example.com/image.jpg');

$resizeOp = imagor()
    ->resize(width: 400, height: 300);
    

$url1 = $resizeOp->uriFor('https://example.com/image.jpg');
$url2 = $resizeOp->uriFor('https://example.com/foo.jpg');

$imagor = imagor();

// ❌ WILL NOT WORK ❌ because a new object is returned
$imagor->resize(width: 400, height: 300);
$imagor->uriFor('https://example.com/image.jpg');

// ✅ Instead, do the following:
$imagor = $imagor->resize(width: 400, height: 300);
$imagor->uriFor('https://example.com/image.jpg');

// Basic resizing
$url = imagor()
    ->resize(width: 400, height: 300)
    ->uriFor($imageUrl);

// Fit image within dimensions (preserves aspect ratio)
$url = imagor()
    ->resize(width: 400, height: 300)
    ->fitIn()
    ->uriFor($imageUrl);

// Force stretch to exact dimensions (does NOT preserve aspect ratio)
$url = imagor()
    ->resize(width: 400, height: 300)
    ->stretch()
    ->uriFor($imageUrl);

// Smart cropping with focal point detection
$url = imagor()
    ->resize(width: 400, height: 300)
    ->smart()
    ->uriFor($imageUrl);

// Manual cropping (left, top, right, bottom)
$url = imagor()
    ->crop(10, 10, 300, 200)
    ->uriFor($imageUrl);

// Set JPEG quality
$url = imagor()->resize(width: 400)->quality(85)->uriFor($imageUrl);

// Convert to different formats
$webpUrl = imagor()->resize(width: 400)->format('webp')->uriFor($imageUrl);
$avifUrl = imagor()->resize(width: 400)->format('avif')->uriFor($imageUrl);
$pngUrl = imagor()->resize(width: 400)->format('png')->uriFor($imageUrl);

$url = imagor()
    ->resize(width: 500, height: 300)
    ->blur(2.0)              // Blur effect
    ->sharpen(1.5)           // Sharpen details  
    ->brightness(20)         // Increase brightness
    ->contrast(110)          // Enhance contrast (percentage)
    ->saturation(120)        // Boost saturation (percentage)
    ->uriFor($imageUrl);

// Flip images
$url = imagor()
    ->resize(width: 400, height: 300)
    ->flipHorizontally()
    ->flipVertically() 
    ->uriFor($imageUrl);

// Add padding (left, top, right, bottom)
$url = imagor()
    ->resize(width: 400, height: 300)
    ->padding(10, 10, 10, 10)
    ->uriFor($imageUrl);

// Set alignment for cropping
$url = imagor()
    ->resize(width: 400, height: 300)
    ->hAlign('left')         // 'left', 'right', 'center'
    ->vAlign('top')          // 'top', 'bottom', 'middle'
    ->uriFor($imageUrl);

FileUpload::make('media_files')
    ->acceptedFileTypes(['image/*'])
    ->rules(['image', 'max:10240']) // max 10MB per file
    ->disk('public')
    ->visibility('public')

use Sandstorm\LaravelImagor\Filament\Components\ImagorFileUpload;

ImagorFileUpload::make('media_files')
    ->acceptedFileTypes(['image/*'])
    ->rules(['image', 'max:10240']) // max 10MB per file
    
    // not me logic, different syntax (without global function)
    ->imageProcessor(fn(ImagorPathBuilder $imagor) => $imagor->resize(100)),

// In AppServiceProvider::boot()
use Illuminate\Support\Facades\Blade;

Blade::directive('imagorWide', function ($expression) {
    return " echo imagor()->resize(width: 400)->uriFor($expression); 

// Portrait enhancement
$enhancedPortrait = imagor()
    ->resize(width: 600, height: 800)
    ->smart()
    ->brightness(8)       // Slightly brighter
    ->contrast(110)       // Enhanced contrast
    ->saturation(105)     // Subtle saturation boost
    ->sharpen(0.8)        // Gentle sharpening
    ->quality(92)
    ->uriFor($portrait);

// Vintage effect
$vintageEffect = imagor()
    ->resize(width: 600, height: 400)
    ->saturation(70)      // Reduced saturation
    ->contrast(90)        // Lower contrast
    ->brightness(-10)     // Slightly darker
    ->quality(85)
    ->uriFor($image);

// Clean product photos
$productClean = imagor()
    ->resize(width: 800, height: 800)
    ->fitIn()
    ->trim()              // Remove whitespace
    ->brightness(15)      // Bright and clean
    ->contrast(110)       // Good contrast
    ->sharpen(1.5)        // Sharp product details
    ->quality(95)         // High quality for products
    ->format('webp')
    ->uriFor($product);
bash
php artisan vendor:publish --tag="imagor-config"

$binaryData = imagor()
    ->resize(width: 400, height: 300)
    ->format('jpeg')
    ->imageBinaryDataFor($imageUrl);

echo '<img src="data:image/jpeg;base64,' . base64_encode($binaryData) . '" />';
blade
<img src="{{ imagor()->resize(300)->uriFor(Storage::disk('public')->path($post->media_files[0])) }}">