PHP code example of bvtterfly / lio

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

    

bvtterfly / lio example snippets


use Bvtterfly\Lio\Facades\ImageOptimizer;
// The image from your configured filesystem disk will be downloaded, optimized, and uploaded to the output path in
ImageOptimizer::optimize($pathToImage, $pathToOptimizedImage);
// The local image will be replaced with an optimized version which should be smaller
ImageOptimizer::optimizeLocal($pathToImage);
// if you use a second parameter the package will not modify the original
ImageOptimizer::optimizeLocal($pathToImage, $pathToOptimizedImage);

use Bvtterfly\Lio\OptimizerChain;
app(OptimizerChain::class)->optimize($pathToImage, $pathToOptimizedImage);

use Bvtterfly\Lio\Optimizers\Cwebp;
use Bvtterfly\Lio\Optimizers\Gifsicle;
use Bvtterfly\Lio\Optimizers\Jpegoptim;
use Bvtterfly\Lio\Optimizers\Optipng;
use Bvtterfly\Lio\Optimizers\Pngquant;
use Bvtterfly\Lio\Optimizers\ReSmushOptimizer;
use Bvtterfly\Lio\Optimizers\Svgo;
use Bvtterfly\Lio\Optimizers\Svgo2;

return [
    /*
     * If set to `default` it uses your default filesystem disk.
     * You can set it to any filesystem disks configured in your application.
     */
    'disk' => 'default',

    /*
     * If set to `true` all output of the optimizer binaries will be appended to the default log channel.
     * You can also set this to a class that implements `Psr\Log\LoggerInterface`
     * or any log channels you configured in your application.
     */
    'log_optimizer_activity' => false,

    /*
     * Optimizers are responsible for optimizing your image
     */
    'optimizers' => [
        Jpegoptim::class => [
            '--max=85',
            '--strip-all',
            '--all-progressive',
        ],
        Pngquant::class => [
            '--quality=85',
            '--force',
            '--skip-if-larger',
        ],
        Optipng::class => [
            '-i0',
            '-o2',
            '-quiet',
        ],
        Svgo2::class => [],
        Gifsicle::class => [
            '-b',
            '-O3',
        ],
        Cwebp::class => [
            '-m 6',
            '-pass 10',
            '-mt',
            '-q 80',
        ],
//        Svgo::class => [
//            '--disable={cleanupIDs,removeViewBox}',
//        ],
//        ReSmushOptimizer::class => [
//            'quality' => 92,
//            'retry' => 3,
//            'mime' => [
//                'image/png',
//                'image/jpeg',
//                'image/gif',
//                'image/bmp',
//                'image/tiff',
//            ],
//
//            'exif' => false,
//
//        ],
    ],

    /*
    * The maximum time in seconds each optimizer is allowed to run separately.
    */
    'timeout' => 60,

    /*
    * The directories where your binaries are stored.
    * Only use this when your binaries are not accessible in the global environment.
    */
    'binaries_path' => [
        'jpegoptim' => '',
        'optipng' => '',
        'pngquant' => '',
        'svgo' => '',
        'gifsicle' => '',
        'cwebp' => '',
    ],


    /*
    * The directory where the temporary files will be stored.
    */
    'temporary_directory' => storage_path('app/temp'),

];

Svgo2::class => [
    'path' => '/path/to/your/svgo/config.js'
]

Svgo::class => [
    '--disable={cleanupIDs,removeViewBox}',
],
// Svgo2::class => [],

use Bvtterfly\Lio\OptimizerChain;
app(OptimizerChain::class)->optimize($pathToImage, $pathToOptimizedImage);

use Bvtterfly\Lio\Facades\ImageOptimizer;
// The image from your configured filesystem disk will be downloaded, optimized, and uploaded to the output path in
ImageOptimizer::optimize($pathToImage, $pathToOptimizedImage);

use Bvtterfly\Lio\Facades\ImageOptimizer;
// The local image will be replaced with an optimized version which should be smaller
ImageOptimizer::optimizeLocal($pathToImage);
// if you use a second parameter the package will not modify the original
ImageOptimizer::optimizeLocal($pathToImage, $pathToOptimizedImage);

Route::middleware(OptimizeUploadedImages::class)->group(function () {
    // all images will be optimized automatically
    Route::post('images', 'ImageController@store');
});

use Psr\Log\LoggerInterface;

interface Optimizer
{
    /**
     * Determines if the given image can be handled by the optimizer.
     *
     * @param Image $image
     *
     * @return bool
     */
    public function canHandle(Image $image): bool;

    /**
     * Sets the path to the image that should be optimized.
     *
     * @param string $imagePath
     *
     * @return Optimizer
     */
    public function setImagePath(string $imagePath): self;

    /**
     * Sets the logger for logging optimization process.
     *
     * @param  LoggerInterface  $logger
     *
     * @return Optimizer
     */
    public function setLogger(LoggerInterface $logger): self;

    /**
     * Sets the amount of seconds optimizer may use.
     *
     * @param  int  $timeout
     *
     * @return Optimizer
     */
    public function setTimeout(int $timeout): self;

    /**
     * Runs the optimizer.
     *
     * @return void
     */
    public function run(): void;
}
bash
php artisan vendor:publish --tag="lio-config"