PHP code example of pboivin / flou
1. Go to this page and download the library: Download pboivin/flou 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/ */
pboivin / flou example snippets
use Pboivin\Flou\ImageFactory;
$flou = new ImageFactory([
'sourcePath' => '/home/user/my-site.com/public/images/source',
'cachePath' => '/home/user/my-site.com/public/images/cache',
'sourceUrlBase' => '/images/source',
'cacheUrlBase' => '/images/cache',
]);
$image = $flou->image('01.jpg');
$image = $flou->image('01.jpg', [
'w' => 10,
'h' => 10,
'fit' => 'crop',
]);
$phone = $flou->image('01.jpg', ['w' => 500]);
$tablet = $flou->image('01.jpg', ['w' => 900]);
$desktop = $flou->image('01.jpg', ['w' => 1300]);
$flou = new ImageFactory([
// ...
'glideParams' => [
'h' => 10,
'fm' => 'gif',
],
]);
$image = $flou->image('01.jpg');
# Source image data:
echo $image->source()->url(); # /images/source/01.jpg
echo $image->source()->path(); # /home/user/my-site.com/public/images/source/01.jpg
echo $image->source()->width(); # 3840
echo $image->source()->height(); # 2160
echo $image->source()->ratio(); # 1.77777778
# Transformed image data:
echo $image->cached()->url(); # /images/cache/01.jpg/de828e8798017be816f79e131e41dcc9.jpg
...
$data = $image->toArray();
# [
# "source" => [
# "url" => "/images/source/01.jpg",
# "path" => "/home/user/my-site.com/public/images/source/01.jpg",
# "width" => 3840,
# "height" => 2160,
# "ratio" => 1.77777778,
# ],
# "cached" => [
# "url" => "/images/cache/01.jpg/de828e8798017be816f79e131e41dcc9.jpg",
# ...
# ],
# ]
$greyscale = $flou->resample('01.jpg', [
'filt' => 'greyscale',
'w' => 2000,
]);
$image = $flou->image($greyscale, ['w' => 50]);
# Source image:
echo $image->source()->url(); # /images/cache/01.jpg/a50df0a8c8a84cfc6a77cf74b414d020.jpg
echo $image->source()->width(); # 2000
...
# Transformed image:
echo $image->cached()->url(); # /images/cache/_r/01.jpg/a50df0a8c8a84cfc6a77cf74b414d020.jpg/9a5bdd58bbc27a556121925569af7b0c.jpg
echo $image->cached()->width(); # 50
...
$image = $flou->image('01.jpg');
echo $image
->render()
->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
echo $image
->render()
->img([
'class' => 'w-full',
'alt' => 'Lorem ipsum',
'!src' => false,
]);
echo $image
->render()
->useAspectRatio()
->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
# or use a custom aspect-ratio:
echo $image
->render()
->useAspectRatio(16 / 9)
->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
echo $image
->render()
->usePaddingTop()
->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
# or use a custom aspect-ratio:
echo $image
->render()
->usePaddingTop(16 / 9)
->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
echo $image
->render()
->useWrapper()
->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
echo $image
->render()
->useBase64Lqip()
->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
$flou = new ImageFactory([
// ...
'renderOptions' => [
'aspectRatio' => true,
'wrapper' => true,
'base64Lqip' => true,
// ...
],
]);
echo $image
->render()
->noScript(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
$imageSet = $flou->imageSet([
'image' => '01.jpg',
'sizes' => '(max-width: 500px) 100vw, 50vw',
'widths' => [500, 900, 1300, 1700],
]);
echo $imageSet
->render()
->useAspectRatio()
->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
$imageSet = $flou->imageSet([
[
'image' => 'portrait.jpg',
'media' => '(max-width: 1023px)',
'sizes' => '100vw',
'widths' => [400, 800, 1200],
],
[
'image' => 'landscape.jpg',
'media' => '(min-width: 1024px)',
'sizes' => '66vw',
'widths' => [800, 1200, 1600],
],
]);
echo $imageSet
->render()
->picture(['class' => 'my-image', 'alt' => 'Lorem ipsum']);
$imageSet = $flou->imageSet([
'image' => '01.jpg',
'sizes' => '100vw',
'widths' => [400, 800, 1200, 1600],
'formats' => ['webp', 'jpg'],
]);
echo $imageSet
->render()
->picture(['class' => 'my-image', 'alt' => 'Lorem ipsum']);
$imageSet = $flou->imageSet([
'image' => '01.jpg',
'sizes' => '100vw',
'widths' => [400, 800, 1200, 1600],
'formats' => ['webp', 'jpg'],
], [
'q' => 80,
]);
$flou = new Pboivin\Flou\RemoteImageFactory([
'glideUrlBase' => '/glide', // or use a full URL: https://cdn.my-site.com/glide
'glideUrlSignKey' => 'secret',
]);
$image = $flou->image('test.jpg');
//...
// @see https://flysystem.thephpleague.com/docs/adapter/aws-s3-v3/
$sourceFilesystem = new League\Flysystem\Filesystem(
new League\Flysystem\AwsS3V3\AwsS3V3Adapter(/* S3 adapter configuration */);
);
$server = League\Glide\ServerFactory::create([
'source' => $sourceFilesystem,
'cache' => '/home/my-site.com/storage/glide-cache',
'base_url' => '/glide',
]);
$flou = new Pboivin\Flou\RemoteImageFactory([
'glideServer' => $server,
'glideUrlSignKey' => 'secret',
]);
$image = $flou->image('test.jpg');
//...
// @see https://laravel.com/docs/filesystem
$sourceFilesystem = Illuminate\Support\Facades\Storage::disk('s3')->getDriver(),
//...
<?= $flou
->image('01.jpg')
->render()
->useAspectRatio()
->useWrapper()
->img(['class' => 'w-full', 'alt' => 'Lorem ipsum']);
<?= $flou->imageSet([
[
'image' => 'portrait.jpg',
'media' => '(max-width: 1023px)',
'sizes' => '100vw',
'widths' => [400, 800, 1200],
],
[
'image' => 'landscape.jpg',
'media' => '(min-width: 1024px)',
'sizes' => '66vw',
'widths' => [800, 1200, 1600],
],
])
->render()
->picture(['class' => 'my-image', 'alt' => 'Lorem ipsum']);
<div>
<?= ($image = $flou->image('01.jpg'))
->render()
->img(['class' => 'w-full', 'alt' => 'Lorem ipsum'])
<?= ($image = $flou->image('01.jpg'))
->render()
->useAspectRatio()
->noScript([
'class' => 'w-full',
'alt' => 'Lorem ipsum',
'loading' => 'lazy',
'decoding' => 'async',
'style' => "background-image: url({$image->cached()->url()});
background-size: cover;"
]);
$image = $flou->image('01.jpg');
= new Pboivin\Flou\ImageFactory([
'sourcePath' => './public/images/source',
'cachePath' => './public/images/cache',
'sourceUrlBase' => '/images/source',
'cacheUrlBase' => '/images/cache',
]);
$data = [];
foreach (glob('./public/images/source/*.jpg') as $path) {
$file = basename($path);
echo "Processing image: $file\n";
$data[$file] = [
'source' => $flou->image($file)->source()->toArray(),
'lqip' => $flou->image($file)->cached()->toArray(),
'responsive' => array_map(
fn ($width) => $flou->image($file, ['w' => $width])->cached()->toArray(),
[500, 900, 1300, 1700]
),
];
}
file_put_contents('./data/images.json', json_encode($data, JSON_PRETTY_PRINT));
echo "Done!\n";