PHP code example of shykat199 / webpbolt

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

    

shykat199 / webpbolt example snippets


$result = (new BatchProcessor())
    ->source('public/uploads')
    ->destination('public/webp')
    ->run();

echo $result->summary();
// 412 converted, 88 skipped, 2 failed — 31.4MB saved (36.2%) in 47.9s

use Shykat\WebpBolt\WebpBolt;

WebpBolt::make('photo.jpg')->save();                    // -> photo.webp

WebpBolt::make('photo.jpg')
    ->autoOrient()
    ->resize(1600)
    ->watermarkText('(c) 2026 Studio')
    ->quality(85)
    ->save('out.webp');

use Shykat\WebpBolt\ImageProcessor;

$processor = new ImageProcessor();

$path = $processor->toWebp('photo.jpg');
$path = $processor->toWebp('photo.jpg', 'out.webp', quality: 90);

use Shykat\WebpBolt\BatchProcessor;

$result = (new BatchProcessor())
    ->source('public/uploads')
    ->destination('public/webp')
    ->run();

echo $result->summary();

$preview = (new BatchProcessor())
    ->source('public/uploads')
    ->dryRun()
    ->run();

echo "Would convert {$preview->converted} files\n";

$result = (new BatchProcessor())
    ->source('public/uploads')
    ->destination('public/webp')
    ->countFirst()
    ->onProgress(function ($file, $done, $total, $status) {
        printf("[%d/%d] %-18s %s\n", $done, $total, $status, basename($file));
    })
    ->run();

use Shykat\WebpBolt\ImageProcessor;
use Shykat\WebpBolt\Transforms\Resize;

$processor = (new ImageProcessor())
    ->addTransform(new Resize(1600, 1600));

$result = (new BatchProcessor($processor))
    ->source('public/uploads')
    ->destination('public/webp')
    ->run(new WebpEncoder(quality: 78));

(new BatchProcessor())->source('public/uploads')->run();

// public/uploads/2024/cat.jpg
// public/uploads/2024/cat.webp   <- new

$result = (new BatchProcessor())->source('public/uploads')->run();

foreach ($result->failed as $path => $error) {
    fwrite(STDERR, "FAILED {$path}: {$error}\n");
}

(new BatchProcessor())
    ->source('public/uploads')
    ->skipExisting(false)
    ->run(new WebpEncoder(quality: 65));

$result->converted;       // int   — files successfully written
$result->skipped;         // int   — up to date, or output was larger
$result->failed;          // array — [source path => error message]
$result->bytesIn;         // int   — total original size
$result->bytesOut;        // int   — total new size
$result->bytesSaved();    // int
$result->percentSaved();  // float
$result->seconds;         // float
$result->total();         // int   — every file looked at
$result->summary();       // string — the one-line report

use Shykat\WebpBolt\ImageProcessor;
use Shykat\WebpBolt\Encoders\WebpEncoder;
use Shykat\WebpBolt\Transforms\Resize;
use Shykat\WebpBolt\Transforms\Grayscale;

$path = (new ImageProcessor())
    ->addTransform(new Resize(800, 800))
    ->addTransform(new Grayscale())
    ->save('photo.jpg', 'output/photo.webp', new WebpEncoder(quality: 75));

use Shykat\WebpBolt\WebpBolt;

WebpBolt::make('photo.jpg')->watermarkText('(c) 2026 Studio')->save();

use Shykat\WebpBolt\Transforms\Watermark;
use Shykat\WebpBolt\Enums\Position;

Watermark::text('(c) 2026 Studio')
    ->font(__DIR__ . '/fonts/Inter.ttf', 28)
    ->color('#ffffff')
    ->position(Position::BottomLeft)
    ->opacity(70)
    ->margin(30);

Watermark::image('logo.png')
    ->position(Position::BottomRight)
    ->scale(0.2)
    ->opacity(45);

WebpBolt::make('photo.jpg')
    ->autoOrient()
    ->resize(1600)
    ->transform(Watermark::image('logo.png')->scale(0.2))
    ->save('out.webp');

$processor = (new ImageProcessor())
    ->addTransform(new Resize(1600, 1600))
    ->addTransform(Watermark::image('logo.png')->opacity(45));

(new BatchProcessor($processor))
    ->source('public/uploads')
    ->destination('public/webp')
    ->run(new WebpEncoder(80));

namespace Shykat\WebpBolt\Transforms;

use GdImage;
use Shykat\WebpBolt\Contracts\TransformInterface;

class Watermark implements TransformInterface
{
    public function __construct(private string $text) {}

    public function apply(GdImage $image): GdImage
    {
        $color = imagecolorallocatealpha($image, 255, 255, 255, 50);
        imagestring($image, 5, 10, 10, $this->text, $color);

        return $image;
    }
}

namespace Shykat\WebpBolt\Encoders;

use GdImage;
use Shykat\WebpBolt\Contracts\EncoderInterface;

class AvifEncoder implements EncoderInterface
{
    public function __construct(private int $quality = 60) {}

    public function encode(GdImage $image, string $destinationPath): bool
    {
        imagepalettetotruecolor($image);
        imagealphablending($image, false);
        imagesavealpha($image, true);

        return imageavif($image, $destinationPath, $this->quality);
    }

    public function extension(): string
    {
        return 'avif';
    }
}

(new BatchProcessor())->source('public/uploads')->run(new AvifEncoder(60));

'max_dimension' => 1600,
'quality'       => 80,

'watermark' => [
    'image'    => public_path('logo.png'),
    'position' => 'bottom-right',
    'opacity'  => 50,
    'scale'    => 0.18,
],

public function store(Request $request, ImageProcessor $processor)
{
    $processor->toWebp($request->file('photo')->getRealPath());
}

Schedule::command('images:webp')->hourly()->withoutOverlapping();

use Shykat\WebpBolt\Exceptions\ImageNotFoundException;
use Shykat\WebpBolt\Exceptions\ImageTooLargeException;
use Shykat\WebpBolt\Exceptions\UnsupportedFormatException;
use Shykat\WebpBolt\Exceptions\EncodingFailedException;

try {
    WebpBolt::make($path)->resize(1600)->save();
} catch (UnsupportedFormatException $e) {
    // not an image we can read
} catch (ImageTooLargeException $e) {
    // would blow the memory limit
}
bash
php -r "var_dump(gd_info()['WebP Support']);"
bash
php artisan vendor:publish --tag=webpbolt-config
bash
php artisan images:webp --dry            # preview, writes nothing
php artisan images:webp                  # convert
php artisan images:webp --force          # rebuild everything
php artisan images:webp --no-watermark   # skip the configured watermark