PHP code example of bonniss26 / lqip

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

    

bonniss26 / lqip example snippets


use Bonniss26\Lqip\PlaceholderGenerator;

$placeholder = PlaceholderGenerator::forFile('/path/to/photo.jpg');
// "linear-gradient(#b5b2a5,#b5b2a5)"

PlaceholderGenerator::resolveDriver()->supportsFormat('webp'); // true / false
// resolveDriver() is null when neither GD nor Imagick is installed

use Bonniss26\Lqip\PlaceholderGenerator;

// From a file:
PlaceholderGenerator::forFile('/path/to/photo.jpg');
// "linear-gradient(#b5b2a5,#b5b2a5)"

// From bytes you already have, e.g. downloaded from S3:
PlaceholderGenerator::forBytes($imageBytes, ['mode' => 'blur']);
// "url('data:image/webp;base64,UklGRkYAAABXRUJQVlA4IDoAAADwAQCd...')"

use Bonniss26\Lqip\PlaceholderGenerator;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;

function lqip(string $path): string
{
    $disk = Storage::disk('public');
    if (!$disk->exists($path)) {
        return '';
    }

    // The modification time in the key regenerates it when the file is replaced.
    $key = 'lqip:' . $path . ':' . $disk->lastModified($path);
    if (($cached = Cache::get($key)) !== null) {
        return $cached;
    }

    $placeholder = PlaceholderGenerator::forBytes((string) $disk->get($path));
    if ($placeholder !== null) {
        Cache::forever($key, $placeholder); // a failure is not cached, so it is retried
    }

    return $placeholder ?? '';
}

$result = PlaceholderGenerator::resultForFile('/path/to/photo.jpg');

$result->width();        // 800
$result->height();       // 533
$result->aspectRatio();  // 1.5009...
$result->toStyleAttribute(['aspect_ratio' => true]);
// "background-image:...;background-size:cover;aspect-ratio:800/533"

$result = PlaceholderGenerator::resultForFile('/path/to/photo.jpg', ['mode' => 'css_only']);

$result->kind();             // "css_var" ("css" for the CSS modes, "hash" for thumbhash)
$result->value();            // "87524"
$result->toCss();            // null: not usable as a background-image
$result->toStyleAttribute(); // "--lqip:87524"

use Bonniss26\Lqip\Drivers\GdDriver;

PlaceholderGenerator::forFile($path, ['driver' => 'gd']);   // per call
PlaceholderGenerator::useDriver(new GdDriver());            // process-wide
PlaceholderGenerator::availableDrivers();                   // ['imagick', 'gd']

use Bonniss26\Lqip\Drivers\Driver;
use Bonniss26\Lqip\Drivers\Image;
use Bonniss26\Lqip\LqipResult;
use Bonniss26\Lqip\PlaceholderGenerator;
use Bonniss26\Lqip\Strategies\LqipStrategy;

final class AverageColour implements LqipStrategy
{
    public function generate(Image $image, Driver $driver, array $options): ?LqipResult
    {
        $hex = sprintf('#%06x', $image->sample(1, 1)->rgbAt(0, 0));

        return LqipResult::css("linear-gradient({$hex},{$hex})");
    }
}

PlaceholderGenerator::extend('average', new AverageColour());
PlaceholderGenerator::forFile('/path/to/photo.jpg', ['mode' => 'average']);
bash
php bench/compare-drivers.php /path/to/photos [mode]