PHP code example of laraextend / media-toolkit

1. Go to this page and download the library: Download laraextend/media-toolkit 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/ */

    

laraextend / media-toolkit example snippets


use Laraextend\MediaToolkit\Facades\Media;

// Simple optimized URL
$url = Media::image('resources/images/hero.jpg')
    ->resize(width: 800)
    ->format('webp')
    ->url();

// Full responsive <picture> with filters
echo Media::image('resources/images/hero.jpg')
    ->resize(width: 1200)
    ->grayscale()
    ->picture(formats: ['avif', 'webp'], fallback: 'jpg')
    ->fetchpriority('high')
    ->html(alt: 'Hero', class: 'w-full');

Media::image('photo.jpg')->resize(width: 800)              // → 800px wide, height proportional
Media::image('photo.jpg')->resize(height: 600)             // → 600px tall, width proportional
Media::image('photo.jpg')->resize(width: 800, height: 600) // → fit inside 800×600, no crop

Media::image('small.jpg')->resize(width: 1200)->upscale()->url();

Media::image('photo.jpg')->fit(400, 400)->url();  // Always exactly 400×400

Media::image('photo.jpg')->stretch(200, 200)->url();

Media::image('photo.jpg')->crop(400, 200, 'center', 'center')->url();
Media::image('photo.jpg')->crop(400, 200, 100, 50)->url();  // pixel offsets

Media::image('resources/images/photo.jpg')->original()->url();

Media::image('photo.jpg')
    ->resize(width: 800)
    ->grayscale()
    ->blur(3)
    ->brightness(20)
    ->html(alt: 'Photo');

Media::image('photo.jpg')
    ->resize(width: 1200)
    ->watermark(
        source:   'resources/images/watermark.png',
        position: 'bottom-right',
        padding:  20,
        opacity:  80,
    )
    ->html(alt: 'Photo');

// Relative path (resolved via base_path())
->watermark('resources/images/logo.png', 'bottom-right')

// Web path — as returned by ->url() (resolved via public_path())
$logoUrl = Media::image('resources/images/logo.png')->resize(width: 200)->url();
->watermark($logoUrl, 'bottom-right', 10, 80)

// Absolute filesystem path
->watermark('/var/www/html/storage/watermarks/logo.png', 'center')

$url = Media::image('resources/images/og.jpg')->resize(width: 1200)->format('jpg')->url();

// Simple <img>
echo Media::image('logo.png')->resize(width: 200)->html(alt: 'Logo', class: 'h-8');

// <img> with srcset
echo Media::image('hero.jpg')->resize(width: 800)->responsive('(max-width: 768px) 100vw, 800px')->html(alt: 'Hero');

// <picture> with <source> elements
echo Media::image('hero.jpg')->resize(width: 800)->picture()->html(alt: 'Hero');

Media::image('hero.jpg')
    ->resize(width: 800)
    ->responsive('(max-width: 768px) 100vw, 800px')
    ->fetchpriority('high')
    ->html(alt: 'Hero Banner', class: 'w-full');

Media::image('hero.jpg')
    ->resize(width: 1200)
    ->picture(formats: ['avif', 'webp'], fallback: 'jpg', imgClass: 'w-full')
    ->fetchpriority('high')
    ->html(alt: 'Hero', class: 'hero-picture');

return [
    // Image processing driver: 'auto' (recommended), 'gd', or 'imagick'
    'driver'     => env('MEDIA_TOOLKIT_DRIVER', 'auto'),

    // Output directory relative to public/
    'output_dir' => env('MEDIA_TOOLKIT_OUTPUT_DIR', 'media/optimized'),

    'image' => [

        // Image quality per format (1–100)
        'quality' => [
            'webp'  => 80,
            'avif'  => 65,
            'jpg'   => 82,
            'jpeg'  => 82,
            'png'   => 85,
        ],

        // Responsive breakpoints
        'responsive' => [
            'size_factors' => [0.5, 0.75, 1.0, 1.5, 2.0], // multipliers of the requested width
            'min_width'    => 100,                           // skip variants narrower than this
        ],

        // Default HTML attribute values and format choices
        'defaults' => [
            'format'          => 'webp',
            'picture_formats' => ['avif', 'webp'],
            'fallback_format' => 'jpg',
            'loading'         => 'lazy',
            'fetchpriority'   => 'auto',
            'sizes'           => '100vw',
        ],

    ],
];

'image' => [
    'errors' => [
        'on_not_found'    => env('MEDIA_ON_NOT_FOUND',    'placeholder'),  // file does not exist
        'on_error'        => env('MEDIA_ON_ERROR',        'placeholder'),  // processing failed
        'on_memory_limit' => env('MEDIA_ON_MEMORY_LIMIT', 'placeholder'),  // GD memory bypass

        // Placeholder label text per error type
        'not_found_text'     => 'Media could not be found.',
        'error_text'         => 'Media could not be displayed!',
        'memory_limit_text'  => 'Media will be displayed shortly.',

        // Placeholder background colour per error type
        'not_found_color'    => '#f87171',   // red-400
        'error_color'        => '#f87171',   // red-400
        'memory_limit_color' => '#9ca3af',   // gray-400
    ],
],

'image' => [
    'logging' => [
        'enabled' => env('MEDIA_LOGGING_ENABLED', true),

        // null = Laravel's default log channel (LOG_CHANNEL in .env)
        // Set to 'single', 'daily', 'stack', etc. to use a dedicated channel
        'channel' => env('MEDIA_LOG_CHANNEL', null),

        'level' => [
            'not_found'    => 'warning',
            'error'        => 'error',
            'memory_limit' => 'notice',
        ],
    ],
],

// Minimal usage
echo Media::video('resources/videos/intro.mp4')->html(class: 'w-full');

// With chain options
echo Media::video('resources/videos/hero.mp4')
    ->controls()
    ->muted()
    ->autoplay()
    ->loop()
    ->width(1280)
    ->height(720)
    ->poster('resources/images/poster.jpg')
    ->html(class: 'w-full rounded-lg', id: 'hero-video');

// URL only (e.g. for custom players)
$url = Media::video('resources/videos/clip.mp4')->url();

->html(string $class = '', ?string $id = null, array $attributes = []): string

// Minimal usage
echo Media::audio('resources/audio/podcast.mp3')->html(class: 'w-full');

// With chain options
echo Media::audio('resources/audio/track.ogg')
    ->controls()
    ->preload('none')
    ->loop()
    ->html(class: 'w-full', id: 'audio-player');

// URL only
$url = Media::audio('resources/audio/intro.mp3')->url();

->html(string $class = '', ?string $id = null, array $attributes = []): string

// Default: <img> tag pointing to the served SVG
echo Media::svg('resources/images/icon.svg')->html(alt: 'Icon', class: 'w-6 h-6');

// Inline SVG embedded directly in the HTML (scripts and on* handlers filtered)
echo Media::svg('resources/images/logo.svg')
    ->inline()
    ->width(120)
    ->html(alt: 'Company Logo', class: 'fill-current');

// Skip sanitization (developer responsibility)
echo Media::svg('resources/images/animation.svg')
    ->inline(sanitize: false)
    ->html(class: 'w-full');

// URL only
$url = Media::svg('resources/images/sprite.svg')->url();

->html(string $alt = '', string $class = '', ?string $id = null, array $attributes = []): string

// All of these throw MediaBuilderException:
Media::image('../../etc/passwd')->html();            // traversal
Media::image("logo.jpg\nINJECTED")->html();          // CRLF
Media::image('config/database.php')->html();          // disallowed extension
Media::image('resources/img.svg')->html();            // SVG excluded (scripting risk)

Media::image($src)->watermark('/../../etc/shadow')->html();   // traversal in watermark
Media::image($src)->watermark('http://x.com/../../etc')->html(); // traversal in URL
bash
php artisan vendor:publish --tag=media-toolkit-config
bash
php artisan vendor:publish --tag=media-toolkit-config
bash
php artisan media:cache-clear
bash
php artisan media:cache-warm
blade
@foreach ($products as $product)
    {!! Media::image($product->image)
        ->fit(300, 300)
        ->grayscale()
        ->html(alt: $product->name, class: 'product-thumb') !!}
@endforeach
json
{
  "resources/images/hero.jpg": {
    "reason": "memory_limit",
    "count": 3,
    "first_occurred": "2026-02-22T12:00:00+00:00",
    "last_occurred":  "2026-02-22T12:05:00+00:00",
    "params": {
      "display_width": 800,
      "format": "webp",
      "quality": 80,
      "operations_fingerprint": "d41d8cd9...",
      "single_only": true
    }
  }
}
bash
   php artisan vendor:publish --tag=media-toolkit-config --force
   
bash
   php artisan media:cache-clear
   rm -rf public/img/optimized   # remove old directory manually if needed