PHP code example of cloakwp / image-transformations
1. Go to this page and download the library: Download cloakwp/image-transformations 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/ */
cloakwp / image-transformations example snippets
use CloakWP\ImageTransformations\ImageTransformations;
use CloakWP\ImageTransformations\Core\Pipeline;
use CloakWP\ImageTransformations\Transformations\Resize;
use CloakWP\ImageTransformations\Transformations\Webp;
ImageTransformations::make()
->pipeline('optimized', Pipeline::make()->through([
Resize::make(width: 1900, height: 1500),
Webp::make()->quality(92),
]))
->onUpload('optimized')
->register();
use CloakWP\ImageTransformations\Core\Contracts\Transformation;
use CloakWP\ImageTransformations\Core\Image;
final class RotatePortrait implements Transformation
{
public function apply(Image $image): void
{
$size = $image->dimensions();
if ($size['height'] > $size['width']) {
$image->rotate(90);
}
}
}
$recipe = $webp->pipe(new RotatePortrait());
$image->edit(function (\CloakWP\ImageTransformations\Core\Contracts\Editor $editor): void {
$native = $editor->native();
// A method implemented by your custom WP_Image_Editor subclass:
$native->watermark($this->watermarkPath);
});
$images = ImageTransformations::instance();
$result = $images->transformAttachment(42, 'webp');
// A separate output file, without attachment/database changes:
$result = $images->transformFile('/absolute/path/photo.png', 'avif');
use CloakWP\ImageTransformations\Core\Config;
add_filter('cloakwp/image-transformations/config', function (Config $config) use ($webp): Config {
return $config->withPipeline('optimized', $webp)->withUploadPipeline('optimized');
});