PHP code example of citomni / image

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

    

citomni / image example snippets



declare(strict_types=1);

return [
	\CitOmni\Image\Boot\Registry::class,
];

$this->app->image

$files = $this->app->image->save($sourcePath, [
	'large' => [
		'path' => $targetDir . '/large.webp',
		'format' => 'webp',
		'width' => 1600,
	],
	'thumb' => [
		'path' => $targetDir . '/thumb.jpg',
		'format' => 'jpeg',
		'width' => 320,
		'height' => 320,
		'fit' => 'cover',
	],
]);

[
	'large' => [
		'path' => '/absolute/path/large.webp',
		'format' => 'webp',
		'mime' => 'image/webp',
		'width' => 1600,
		'height' => 1067,
		'bytes' => 184321,
		'backend' => 'gd',
	],
	'thumb' => [
		'path' => '/absolute/path/thumb.jpg',
		'format' => 'jpeg',
		'mime' => 'image/jpeg',
		'width' => 320,
		'height' => 320,
		'bytes' => 28412,
		'backend' => 'gd',
	],
]

$this->app->image->inspect($path);
$this->app->image->inspectString($data);

$this->app->image->save($sourcePath, $outputs, $options);
$this->app->image->saveString($sourceData, $outputs, $options);

$this->app->image->encode($sourcePath, $outputs, $options);
$this->app->image->encodeString($sourceData, $outputs, $options);

$this->app->image->capabilities();

$info = $this->app->image->inspect('/path/to/photo.jpg');

$info = $this->app->image->inspectString($imageBytes);

[
	'format' => 'jpeg',
	'mime' => 'image/jpeg',
	'width' => 4032,
	'height' => 3024,
	'orientation' => 6,
	'display_width' => 3024,
	'display_height' => 4032,
	'multi_frame' => false,
	'alpha' => false,
	'icc_profile' => true,
	'color_space' => 'display-p3',
	'bytes' => 2841932,
	'decoder' => 'gd',
]

$result = $this->app->image->save('/path/to/source.jpg', [
	'preview' => [
		'path' => '/path/to/output/preview.webp',
		'format' => 'webp',
		'width' => 1200,
		'height' => 1200,
		'fit' => 'contain',
	],
]);

$result = $this->app->image->saveString($sourceBytes, [
	'preview' => [
		'path' => '/path/to/output/preview.webp',
		'format' => 'webp',
		'width' => 1200,
	],
]);

$result = $this->app->image->encode('/path/to/source.jpg', [
	'thumb' => [
		'format' => 'webp',
		'width' => 320,
		'height' => 320,
		'fit' => 'cover',
	],
]);

$bytes = $result['thumb']['data'];

$result = $this->app->image->encodeString($sourceBytes, [
	'thumb' => [
		'format' => 'jpeg',
		'width' => 320,
		'height' => 320,
		'fit' => 'cover',
		'quality' => 80,
	],
]);

[
	'data' => $encodedBytes,
	'format' => 'jpeg',
	'mime' => 'image/jpeg',
	'width' => 320,
	'height' => 320,
	'bytes' => strlen($encodedBytes),
	'backend' => 'gd',
]

'path' => '/var/app/images/thumb.webp'

'overwrite' => true

'format' => 'webp'

'width' => 1200,
'height' => 800,

'upscale' => true

'crop' => [$x, $y, $width, $height]

'rotate' => 90

'overlays' => [
	[
		'file' => '/path/to/logo.png',
		'anchor' => 'bottom-right',
		'offset' => [-24, -24],
		'width_percent' => 20,
		'opacity' => 70,
	],
]

'quality' => 82

'compression' => 6

'background' => '#ffffff'

[
	'auto_orient' => true,
]

[
	'multi_frame' => 'reject',
]

[
	'multi_frame' => 'first',
]

[
	'color' => 'srgb',
]

$outputs = [
	'hero' => [
		'format' => 'webp',
		'width' => 1600,
	],
	'card' => [
		'format' => 'webp',
		'width' => 640,
		'height' => 400,
		'fit' => 'cover',
	],
	'card_jpeg' => [
		'format' => 'jpeg',
		'width' => 640,
		'height' => 400,
		'fit' => 'cover',
	],
	'thumb' => [
		'format' => 'webp',
		'width' => 240,
		'height' => 240,
		'fit' => 'cover',
	],
];

$result = $this->app->image->encode($sourcePath, $outputs);

$capabilities = $this->app->image->capabilities();

[
	'backends' => [
		'gd' => [
			'available' => true,
			'version' => '...',
		],
		'imagick' => [
			'available' => true,
			'version' => 'ImageMagick 6.9.11-60 Q16 ...',
		],
	],
	'decode' => [
		'jpeg' => 'gd',
		'png' => 'gd',
		'gif' => 'gd',
		'webp' => 'gd',
		'avif' => 'imagick',
		'bmp' => 'gd',
		'tiff' => 'imagick',
		'heic' => 'imagick',
	],
	'encode' => [
		// Same format keys, with backend name or null.
	],
	'first_frame' => [
		// Same format keys, with backend name or null.
	],
	'color_management' => 'imagick',
]

'image' => [
	'max_pixels' => 25_000_000,
]

$result = $this->app->image->encode($sourcePath, [
	'portrait' => [
		'format' => 'webp',
		'width' => 800,
		'height' => 1200,
		'fit' => 'cover',
	],
]);

[
	'format' => 'jpeg',
	'background' => '#f5f5f5',
]

$e->committed();
$e->uncommitted();

$e->committed();    // output key => path of targets created or replaced
$e->uncommitted();  // output key => path of untouched targets

$e->target();       // output key whose target exists
$e->committed();    // empty when detected before processing
$e->uncommitted();

'image' => [
	'backends' => ['gd', 'imagick'],

	'max_pixels' => 25_000_000,

	'background' => '#ffffff',

	'color' => 'srgb',

	'jpeg' => [
		'quality' => 82,
		'progressive' => true,
	],

	'png' => [
		'compression' => 6,
	],

	'webp' => [
		'quality' => 80,
	],

	'avif' => [
		'quality' => 60,
		'speed' => 6,
	],

	'heic' => [
		'quality' => 75,
	],
],

'backends' => ['gd', 'imagick'],

'jpeg' => [
	'quality' => 82,
	'progressive' => true,
],

'png' => [
	'compression' => 6,
],

'webp' => [
	'quality' => 80,
],

'avif' => [
	'quality' => 60,
	'speed' => 6,
],

'heic' => [
	'quality' => 75,
],

$this->app->image
text
tests/geometry_test.php
tests/header_test.php
tests/service_test.php
tests/write_test.php
tests/imagick_test.php
tests/color_test.php
text
src/
├── Backend/
│   ├── GdBackend.php
│   ├── ImageBackend.php
│   └── ImagickBackend.php
├── Boot/
│   └── Registry.php
├── Color/
│   ├── IccProfile.php
│   └── Profiles.php
├── Enum/
│   ├── ColorSpace.php
│   ├── ImageAnchor.php
│   ├── ImageFit.php
│   └── ImageFormat.php
├── Exception/
│   ├── ImageCapabilityException.php
│   ├── ImageInputException.php
│   ├── ImageTargetExistsException.php
│   └── ImageWriteException.php
├── Service/
│   └── Image.php
└── Util/
    ├── Geometry.php
    ├── ImageHeader.php
    └── ProbeSamples.php