PHP code example of maccesar / laravel-glide-enhanced
1. Go to this page and download the library: Download maccesar/laravel-glide-enhanced 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/ */
maccesar / laravel-glide-enhanced example snippets
return [
// Image cache configuration
'cache' => [
'disk' => 'local',
'lifetime' => 30, // days
'path' => 'cache/glide',
'headers' => ['max_age' => 86400, 'stale_while_revalidate' => 3600],
],
// Files are resolved below <disk>/images and watermarks below <disk>/watermarks
'source_root' => 'images',
'watermark_root' => 'watermarks',
'allowed_mime_types' => ['image/jpeg', 'image/png', 'image/webp'],
'limits' => ['max_width' => 4096, 'max_height' => 4096, 'max_megapixels' => 16, 'max_dpr' => 4],
// Default processing settings
'defaults' => [
'fit' => 'max', // Default fit mode (max, crop, fill, stretch)
'quality' => 85, // Default quality (0-100)
'format' => 'webp', // Default output format (webp, jpg, png, etc.)
],
// Routes configuration
'routes' => [
'enabled' => true, // Enable/disable package routes
'prefix' => 'glide', // URL prefix for the image routes
'middleware' => ['web', 'throttle:60,1'],
],
// Disk configuration. Change to 'local' if your images are in /storage/app
'disk' => 'public',
// Default fallback images by category
'fallback_images' => [
'default' => 'defaults/no-image.jpg',
'documents' => 'defaults/document.jpg',
'evidence' => 'defaults/evidence.jpg',
'products' => 'defaults/product.jpg',
'users' => 'defaults/user.jpg',
],
// Predefined presets for image conversion
'presets' => [
'large' => ['dimensions' => '800', 'format' => 'webp', 'fit' => 'max'],
'medium' => ['dimensions' => '400', 'format' => 'webp', 'fit' => 'max'],
'social' => ['dimensions' => '1200x630', 'format' => 'jpg', 'fit' => 'crop'],
'thumbnail' => ['dimensions' => '150x150', 'format' => 'webp', 'fit' => 'crop'],
],
];
'disk' => 'public', // Change to 'local' if your images are in /storage/app
'disk' => 'local',
'source_root' => 'public-images',
'routes' => [
'enabled' => true, // Set to false to disable all package routes
'prefix' => 'glide', // Change the URL prefix (e.g., 'images', 'media', etc.)
'middleware' => ['web', 'throttle:60,1'],
],
// Option 1: Full facade name
use MacCesar\LaravelGlideEnhanced\Facades\ImageProcessor;
// Option 2: Short alias (recommended for cleaner code)
use MacCesar\LaravelGlideEnhanced\Facades\Img;
use MacCesar\LaravelGlideEnhanced\Facades\Img;
// Basic URL with parameters
Img::url('path/to/image.jpg', ['w' => 300, 'h' => 200, 'fit' => 'crop']);
// WebP optimized URL
Img::webpUrl('path/to/image.jpg', ['w' => 300, 'h' => 200, 'fit' => 'crop', 'q' => 90]);
// Use a predefined preset
Img::preset('path/to/image.jpg', 'thumbnail');
use MacCesar\LaravelGlideEnhanced\Traits\HasImages;
class Product extends Model
{
use HasImages;
// ...
}
// Usage in code
$product->getImageWebpUrl('main');
$product->getImageUrl('main', '300x200', 'webp');
$product->getImagePreset('main', 'thumbnail');
use MacCesar\LaravelGlideEnhanced\Traits\HasImages;
class Product extends Model
{
use HasImages;
// Your model code...
}
$product = Product::find(1);
// Custom resize with crop
$imageUrl = $product->getImageUrl('main', '400x300', 'webp', null, [
'fit' => 'crop',
'q' => 90
]);
// With watermark and effects
$imageUrl = $product->getImageUrl('main', '600', 'jpg', 'watermarks/logo.png', [
'blur' => 5,
'bright' => 10,
'markpos' => 'bottom-right'
]);
// Just width, no processing
$imageUrl = $product->getImageUrl('main'); // Returns original URL
$product = Product::find(1);
// Simple WebP with default settings
$webpUrl = $product->getImageWebpUrl('main');
// WebP with custom parameters
$webpUrl = $product->getImageWebpUrl('main', [
'w' => 600,
'fit' => 'crop',
'q' => 95
]);
$product = Product::find(1);
$thumbnail = $product->getImagePreset('main', 'thumbnail'); // 150x150 WebP cropped
$medium = $product->getImagePreset('main', 'medium'); // 400px WebP
$large = $product->getImagePreset('main', 'large'); // 800px WebP
$social = $product->getImagePreset('main', 'social'); // 1200x630 JPG for social sharing
class Product extends Model
{
use HasImages;
// Method 1: Custom method for specific type
public function getMainImagePath()
{
return $this->main_photo;
}
// Method 2: Direct property access
// $product->gallery_1, $product->featured, etc.
}
// Usage
$product->getImageUrl('main'); // Uses getMainImagePath()
$product->getImageUrl('gallery_1'); // Uses $product->gallery_1 property
$product->getImageUrl('default'); // Uses $product->image property
'presets' => [
'thumbnail' => [
'dimensions' => '150x150',
'format' => 'webp',
'fit' => 'crop'
],
'medium' => [
'dimensions' => '400',
'format' => 'webp',
'fit' => 'max'
],
'large' => [
'dimensions' => '800',
'format' => 'webp',
'fit' => 'max'
],
'social' => [
'dimensions' => '1200x630',
'format' => 'jpg',
'fit' => 'crop'
],
],
'presets' => [
// Default presets...
// Custom presets
'product_card' => [
'dimensions' => '300x300',
'format' => 'webp',
'fit' => 'crop',
'quality' => 90
],
'banner' => [
'dimensions' => '1920x400',
'format' => 'jpg',
'fit' => 'crop',
'quality' => 95
],
'mobile_hero' => [
'dimensions' => '768x400',
'format' => 'webp',
'fit' => 'crop',
'quality' => 85
],
],
use MacCesar\LaravelGlideEnhanced\Facades\Img;
// Using Img facade
$cardImage = Img::preset('products/image.jpg', 'product_card');
$bannerImage = Img::preset('banners/hero.jpg', 'banner');
// Using HasImages trait
$product = Product::find(1);
$cardImage = $product->getImagePreset('main', 'product_card');
$bannerImage = $product->getImagePreset('main', 'banner');
use MacCesar\LaravelGlideEnhanced\Facades\Img;
// Generate srcset for 1x, 2x, and 3x pixel densities
Img::srcset('path/to/image.jpg', ['w' => 300, 'fm' => 'webp']);
// Output: "/glide/storage/path/to/image.jpg?w=300&fm=webp 1x, /glide/storage/path/to/image.jpg?w=600&fm=webp 2x, /glide/storage/path/to/image.jpg?w=900&fm=webp 3x"
// Control the maximum density factor (e.g., up to 2x)
Img::srcset('path/to/image.jpg', ['w' => 300, 'h' => 200, 'fm' => 'webp'], 2);
// Output: "/glide/storage/path/to/image.jpg?w=300&h=200&fm=webp 1x, /glide/storage/path/to/image.jpg?w=600&h=400&fm=webp 2x"
// Blog post featured image
$blog->getImagePreset('featured', 'large');
// User avatar
$user->getImagePreset('avatar', 'thumbnail');
// Product gallery
$product->getImageWebpUrl('gallery_1', ['w' => 600]);
// Social sharing image
$post->getImagePreset('cover', 'social');
// Responsive hero image
Img::srcset('hero/main.jpg', ['w' => 800, 'fm' => 'webp']);
use MacCesar\LaravelGlideEnhanced\Facades\Img;
// Apply watermark
Img::url('path/to/image.jpg', ['w' => 600, 'mark' => 'watermarks/logo.png', 'markw' => 200, 'markpos' => 'bottom-right', 'markalpha' => 60]);
// Using HasImages trait with watermark
$product->getImageUrl('main', '600', 'jpg', 'watermarks/logo.png', [
'markw' => 200,
'markpos' => 'bottom-right',
'markalpha' => 60
]);
'disk' => 'public', // or 'local' depending on your setup
'routes' => [
'enabled' => true,
'prefix' => 'glide',
],
'cache' => [
'lifetime' => 90, // Increase cache lifetime
'path' => 'cache/glide',
],
bash
php artisan vendor:publish --tag=images-config
blade
<!-- Responsive image with srcset -->
<img src="{{ $product->getImageWebpUrl('main', ['w' => 400]) }}"
srcset="{{ \MacCesar\LaravelGlideEnhanced\Facades\Img::srcset($product->main, ['w' => 400, 'fm' => 'webp']) }}"
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 400px"
alt="{{ $product->name }}"
class="w-full h-auto">
<!-- Different sizes for different breakpoints -->
<picture>
<source media="(max-width: 768px)"
srcset="{{ $product->getImageWebpUrl('main', ['w' => 400]) }}">
<source media="(max-width: 1200px)"
srcset="{{ $product->getImageWebpUrl('main', ['w' => 600]) }}">
<img src="{{ $product->getImageWebpUrl('main', ['w' => 800]) }}"
alt="{{ $product->name }}"
class="w-full h-auto">
</picture>
bash
php artisan images:clean-cache
bash
php artisan images:clean-cache --days=7
bash
php artisan vendor:publish --tag=images-config
bash
php artisan images:clean-cache
bash
php artisan route:clear
php artisan config:clear
bash
php artisan route:list | grep glide