PHP code example of joby / smol-image

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

    

joby / smol-image example snippets


use Joby\Smol\Image\SmolImage;

// Resize and save
SmolImage::load('/path/to/source.jpg')
    ->cover(800, 600)
    ->save('/path/to/output.webp');

// Or get the result as a string
$data = SmolImage::load('/path/to/source.jpg')
    ->fit(400, 300)
    ->jpeg()
    ->string();

// Scale and crop to exactly fill the given dimensions (upscales if necessary)
$resized = $image->cover($width, $height);

// Scale to fit within the given bounding box (upscales if necessary)
$resized = $image->fit($width, $height);

// Scale only on one dimension (upscales if necessary)
$resized = $image->fit($width, null);
$resized = $image->fit(null, $height);

// No transformation — output at original size
$original = $image->original();

// Global defaults
SmolImage::setFormat(Format::jpeg);
SmolImage::setQuality(90);

// Per image — returns a new immutable instance
$image->jpeg();
$image->webp();
$image->png();
$image->quality(90);

// Blur by a factor of 80/10
$image->blur();

// Blur by a factor of 50/100
$image->blur(50);

// Restore to zero blurring
$image->blur(0);

use Joby\Smol\Image\Drivers\GdDriver;
use Joby\Smol\Image\Drivers\ImagickDriver;
use Joby\Smol\Image\Drivers\CliConvertDriver;

SmolImage::setDriver(new ImagickDriver());        // 

SmolImage::setDriver(new CliConvertDriver('/usr/local/bin/convert'));

$source = SmolImage::load('/path/to/photo.jpg')->webp();

$source->cover(1200, 630)->save('/path/to/cover.webp');
$source->cover(400, 400)->save('/path/to/thumb.webp');
$source->fit(1600, 1200)->save('/path/to/full.webp');