PHP code example of isapp / laravel-imagetools

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

    

isapp / laravel-imagetools example snippets


use Isapp\\ImageTools\\Facades\\ImageTools;

// A string URL, or null when the source is narrower than 640px (see "Larger than the source").
$url = ImageTools::asset('resource/images/placeholder.jpg?w=640&q=75&format=webp');

ImageTools::disk('s3')->asset('assets/hero.jpg?w=1200&format=webp');

  ImageTools::asset('public/images/hero.jpg?w=1400&format=avif&q=75&optimize=0');
  

  'optimizers' => [
      Spatie\ImageOptimizer\Optimizers\Jpegoptim::class => ['-m85', '--strip-all', '--all-progressive'],
      Spatie\ImageOptimizer\Optimizers\Pngquant::class => ['--force'],
  ],
  

use Isapp\ImageTools\Facades\ImageTools;

// Reads s3://<bucket>/assets/hero.jpg, processes it, writes the result to the
// configured output disk (image-tools.disk).
ImageTools::disk('s3')->asset('assets/hero.jpg?w=1200&format=webp');

  return [
      'resource/images/hero.jpg?h=630&w=1200&fit=contain&format=webp&q=82' => [
          'path' => 'image-tools/hero--a1b2c3d4e5.webp',
          'disk' => 'public',
          // The source this file was built from, used by imagetools:regenerate.
          'source' => 'resource/images/hero.jpg?w=1200&h=630&fit=contain&format=webp&q=82',
          'source_disk' => null,
      ],
      // A request larger than the source: no file, and asset() returns null.
      'resource/images/icon.png?w=3840' => [
          'path' => null,
          'disk' => null,
          'source' => 'resource/images/icon.png?w=3840',
          'source_disk' => null,
      ],
      // A media() call: the entry also records the Media key.
      's3:42/slide.jpg?format=avif&w=1400' => [
          'path' => 'image-tools/slide--f6e5d4c3b2.avif',
          'disk' => 'public',
          'source' => '42/slide.jpg?w=1400&format=avif',
          'source_disk' => 's3',
          'media_id' => 42,
      ],
  ];
  
bash
php artisan vendor:publish --provider="Isapp\\ImageTools\\ServiceProvider"
blade
<img
  src="{{ ImageTools::asset('public/images/placeholder.jpg?w=640&q=75&format=webp') }}"
  width="640"
  height="360"
  alt="Placeholder"
/>
blade
{{ app(Isapp\ImageTools\ImageTools::class)->asset('resource/images/pic.jpg?w=800') }}
{{ app('image-tools')->asset('resource/images/pic.jpg?w=800') }}
{{ \Illuminate\Support\Facades\App::make('image-tools')->asset('resource/images/pic.jpg?w=800') }}
{{ App::make(Isapp\ImageTools\ImageTools::class)->asset('resource/images/pic.jpg?w=800') }}
dotenv
IMAGE_TOOLS_DISK=public
IMAGE_TOOLS_DRIVER=gd                # gd | imagick | vips; defaults to the spatie/image choice
IMAGE_TOOLS_ALLOW_UPSCALE=false
IMAGE_TOOLS_MANIFEST_PATH=bootstrap/cache/image-tools.php
IMAGE_TOOLS_BLADE_PATHS=resources/views,modules/*/resources/views
IMAGE_TOOLS_PHP_PATHS=app,modules

# Deferred generation (optional)
IMAGE_TOOLS_QUEUE=true               # queue every miss; defaults to false
IMAGE_TOOLS_QUEUE_FALLBACK=original  # original | none | derivative
IMAGE_TOOLS_QUEUE_CONNECTION=redis   # defaults to QUEUE_CONNECTION
IMAGE_TOOLS_QUEUE_NAME=images        # defaults to "default"
IMAGE_TOOLS_QUEUE_UNIQUE_FOR=3600
blade
@php
    $srcset = collect([
        768 => ImageTools::asset('public/images/hero.jpg?w=768&format=webp'),
        1536 => ImageTools::asset('public/images/hero.jpg?w=1536&format=webp'),
        3840 => ImageTools::asset('public/images/hero.jpg?w=3840&format=webp'),
    ])->filter()->map(fn ($url, $width) => "{$url} {$width}w")->implode(', ');
@endphp

<img
  src="{{ ImageTools::asset('public/images/hero.jpg?format=webp') }}"
  srcset="{{ $srcset }}"
  sizes="100vw"
  alt="Hero"
/>
blade
<img src="{{ ImageTools::asset('public/images/hero.jpg?w=1200&format=webp&queue=1') }}">
{{-- Generated in the request, even when the config queues: --}}
<img src="{{ ImageTools::asset('public/images/logo.png?w=200&queue=0') }}">
blade
<img src="{{ ImageTools::disk('s3')->asset('assets/hero.jpg?w=800') }}">
bash
php artisan imagetools:generate
bash
php artisan imagetools:clear
bash
php artisan imagetools:regenerate
bash
php artisan imagetools:prune --dry-run   # list the orphans, delete nothing
php artisan imagetools:prune