Search by

coercive / imgprocess

Coercive

Coercive Utility ImgProcess

Package info

github.com/Coercive/ImgProcess

Homepage

pkg:composer/coercive/imgprocess

Statistics

Installs: 1 338

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

0.2.0 2026-10-20 10:30 UTC

This package is auto-updated.

Last update: 2026-10-20 10:32:36 UTC


README

  • The ImgProcess allows you to easily resize your images in PHP with util options like "cover" which allows to cover a dimensioned area provided just as would do the css property of the same name; Or the "crop" that allows you to crop a specific area of your image.

Compatible PHP 7.4 → 8.5

Requires the GD extension (with WebP support for .webp files). The exif extension is needed for the automatic rotation (setExifRotate(), Orientation), and tidy for ImgResponsive.

Get

composer require coercive/imgprocess

Tests

composer install
vendor/bin/phpunit

PHPUnit 12 requires PHP 8.3+ to run the tests ; the library itself still runs on PHP 7.4.

Resize image

use Coercive\Utility\ImgProcess\ImgProcess;

# INIT
$img = new ImgProcess;

# QUALITY (optional : default jpg 60 / png 0 / webp 80)
$img
	->setJpgQuality(50)
	->setPngCompression(5)
	->setWebpQuality(70);

# EXAMPLE SET
$img
	->setOverwriting(true)
	->setInputPath('source/path/image_name.jpg')
	->setOutputPath('output/path/new_image.jpg')
	->setSourceCoordinate('RIGHT', 'BOTTOM')
	->setOutputSize(1000, 1000);

# PROCESS
$bVerif = $img->sameSize();
// or
$bVerif = $img->myOwnSize(500);
// or
$bVerif = $img->crop();
// or
$bVerif = $img->cover();
// or : resize the largest side (width of a landscape image, height of a portrait one)
$bVerif = $img->max(1000, 1000);
// ...

# HANDLE ERRORS
if( !$bVerif ) {
	if( $aError = $img->getErrors() ) { // getError() is an alias
		foreach ($aError as $sMessage) { echo "<p>$sMessage</p>"; }
		die('Shutdow After Process');
	}
	else {
		die('Shutdow After Process : Unknow Error.');
	}
}

Supported formats : jpg / jpeg, png, gif, webp (the output format is given by the output extension).

The output file is written in a temporary file next to it, then renamed : a visitor never gets a half-written image.

Rotation, flip, background

# Rotate and flip the image as its EXIF orientation says (photos taken with a phone)
$img->setExifRotate();

# Or manually : counterclockwise, in degrees (-90 is the same as 270)
$img->rotate(90);
$img->flip(IMG_FLIP_HORIZONTAL); // IMG_FLIP_VERTICAL, IMG_FLIP_BOTH

# Fill the transparent areas (png to jpg for example)
$img->fill(255, 255, 255);

The output sizes are calculated on the image once rotated : a portrait photo stays in portrait.

Detect image quality

# DETECT IMAGE QUALITY (base on linux 'identify')
$iQuality = ImgProcess::getImageQuality('/path/image_name.jpg');

Get image size

$sizes = ImgProcess::getImageSize('/path/image_name.jpg');
echo $sizes['width'] . ' x ' . $sizes['height']; // 0 x 0 if the file is not a readable image

EXIF orientation

use Coercive\Utility\ImgProcess\Orientation;

$orientation = new Orientation('/path/photo.jpg');
$orientation->get(); // raw EXIF value, 1 to 8 (0 if not specified)
$orientation->getAngle(); // 0, 90, 180, 270 (counterclockwise, as GD)
$orientation->getFlip(); // 0, IMG_FLIP_HORIZONTAL, IMG_FLIP_VERTICAL

Html img tag builder

use Coercive\Utility\ImgProcess\Img;

echo (new Img)
    ->setSrc('/img/photo.jpg')
    ->setAlt('My "photo"')
    ->addClass('cover')
    ->setWidth(800)
    ->setHeight(600)
    ->setLazy()
    ->addSrcsetW('/img/photo-400.jpg', 400)
    ->addSrcsetW('/img/photo-800.jpg', 800)
    ->setSizes('(max-width: 600px) 400px, 800px');

Html responsive image

Example of html content to reformat.

<section>
    <p>Hello World</p>
    <img src="my-image.jpg" alt="Hello World" />
</section>

Instantiate basic options

use Coercive\Utility\ImgProcess\ImgResponsive;

$rii = (new ImgResponsive)
    ->overwrite(true)
    ->data($data)
    ->path('/rootpath/server/img', '/realpath/img')

Mode SRCSET with multiplier option

$rii
    ->modeSrcset(true)
    ->size(500, '1x', true)
    ->size(1000, '2x')
    ->size(1500, '3x')

Mode SRCSET with query option

$rii
    ->modeSrcset()
    ->size(645, '(max-width: 750px) 645px')
    ->size(1095, '(min-width: 751px) and (max-width: 938px) 1095px')
    ->size(862, '(min-width: 939px) and (max-width: 1169px) 832px')
    ->size(1095, '(min-width: 1170px) 1095px', true)

Mode PICTURE with query option

$rii
    ->modePicture()
    ->size(645, '(max-width: 750px) 645px')
    ->size(1095, '(min-width: 751px) and (max-width: 938px) 1095px')
    ->size(862, '(min-width: 939px) and (max-width: 1169px) 832px')
    ->size(1095, '(min-width: 1170px) 1095px', true)

Resolve image src path

Here is an example of retrieving the good image path if you have some complex directory tree.

$rii->resolve(function ($path) {
    $path = urldecode($path);

    if(!preg_match('`/(?P<env>testmode|production)/filedirectory/(?P<relpath>/.+)$`', $path, $matches)) {
        return '';
    }

    $env = $matches['env'];
    $relpath = $matches['relpath'];
    return  preg_replace('`testmode|production`', $env, '/root/production/specific/path') . $relpath;
});

Start process and get HTML

try {
    $rii->process();
}
catch (Exception $e) {
    // do something
    echo $e->getMessage();
    exit;
}

$formattedHtml = $rii->getHtml();
if(!$formattedHtml) {
    die('KO');
}

echo $formattedHtml;