PHP code example of walrussoup / laravel-shortpixel

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

    

walrussoup / laravel-shortpixel example snippets


return [
    'api_key' => env('SHORTPIXEL_API_KEY'),
    'plugin_version' => env('SHORTPIXEL_PLUGIN_VERSION'),
    'log_channel' => env('SHORTPIXEL_LOG_CHANNEL', 'default')
];

$laravelShortpixel = new WalrusSoup\LaravelShortpixel();
// set this up yourself or just let the container do it
$laravelShortpixel->setApiKey('your api key');
$laravelShortpixel->setPluginVersion('your plugin version');
$laravelShortpixel->setLogChannel('your log channel');

// Create an image configuration that compresses the original format and also outputs a webp format
$configuration = (new CompressionConfig())
        ->resizeToCover(500, 400)
        ->addImage('https://images.unsplash.com/photo-1611457194403-d3aca4cf9d11')
        // or, add multiple images using addImages()
        ->useLosslessCompression()
        ->convertToWebp()
        ->retainOriginalFormat();

/** @var ShortpixelCompressionResult $results */
$results = $laravelShortpixel->callShortpixelAndWait($configuration);

foreach($results as $result) {
    ray($result->getOriginalUrl(), $result->getCompressedUrl());
}

// If you requested a conversion to another format, this will help you get the original name
$originalName = $result->getOriginalFilenameWithNewExtension();
// WEBP and AVIF are split off into a separate key, so you can use this to get it I suppose
$originalNameWebp = $result->getOriginalFilenameWebpLossy();
// Again, these are names only. You will need to still download the URL from the other key, for instance:
file_get_contents($result->getWebpLosslessURL());

namespace App\Jobs;

use WalrusSoup\LaravelShortpixel\CompressionConfig;
use WalrusSoup\LaravelShortpixel\LaravelShortpixel;

class CompressImage implements ShouldQueue
{
    public function __construct(public CompressionConfig $config) {}
    
    public function handle(LaravelShortpixel $laravelShortpixel)
    {
        // Queue the job and forget about it for 5 minutes
        $laravelShortpixel->callShortPixel($this->config);
        // Fetch the result later
        CheckCompressionResults::dispatch($this->config)->delay(now()->addMinutes(5));
    }
}

// another job
class CheckCompressionResults implement ShouldQueue
{
    public $tries = 1;
    
    public function __construct(public CompressionConfig $config) {}
    
    public function handle(LaravelShortpixel $laravelShortpixel)
    {
        // Again, we have to give them the original config. It won't compress again, it will just return the results
        $results = $laravelShortpixel->callShortPixelAndWait($this->config);
        
        foreach($results as $result) {
            // do something with the result
        }
    }
}
bash
php artisan vendor:publish --tag="laravel-shortpixel-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="laravel-shortpixel-config"