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();

->pipeline('optimized', fn(): Pipeline => Pipeline::make()->through([
  Resize::make(1900, 1500), Webp::make()->quality(92),
]))

use CloakWP\ImageTransformations\Core\Image;
use CloakWP\ImageTransformations\Core\Source;
use CloakWP\ImageTransformations\Transformations\Avif;
use CloakWP\ImageTransformations\Transformations\When;

$webp = Pipeline::make()->through([
  Resize::make(1600, 1600),
  Webp::make()->quality(85),
]);

$avif = Pipeline::make()
  ->onlyMimeTypes(['image/jpeg', 'image/png'])
  ->when(fn(Source $source): bool => $source->width > 800)
  ->through([
    Resize::make(1900, 1500),
    Avif::make()->quality(75)->skipIfUnsupported(),
    When::make(
      fn(Image $image): bool => !$image->supports('image/avif'),
      Webp::make()->quality(85),
    ),
  ]);

ImageTransformations::make()
  ->pipeline('webp', $webp)
  ->pipeline('avif', $avif)
  ->onUpload('avif')
  ->mediaActions() // Optional native Media Library list/bulk actions.
  ->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');

add_action('cloakwp/image-transformations/failed', function (
  Throwable $error,
  string $path,
  string $pipeline,
): void {
  error_log($error->getMessage());
}, 10, 3);

use CloakWP\ImageTransformations\Core\Config;

add_filter('cloakwp/image-transformations/config', function (Config $config) use ($webp): Config {
  return $config->withPipeline('optimized', $webp)->withUploadPipeline('optimized');
});

->onUpload(fn(): ?string => get_current_blog_id() === 2 ? 'optimized' : null)