Intervention / image

PHP Image Processing
https://image.intervention.io
MIT License
13.86k stars 1.5k forks source link

Using resize() and getSize() to get image dimensions without computing the resulting image #1227

Closed marios88 closed 5 months ago

marios88 commented 10 months ago

Hello, in my use case i want to know the resulting image dimensions without generating them ahead of time.

I believe "getSize()" should be documented as an API function as its provides valuable information

Here is a example for anyone interested in calculating output dimensions without actually resizing the image

ImageManagerStatic::configure(['driver' => 'imagick']);
$img = ImageManagerStatic::make('path');
$sizes = $img->getSize()->resize(1000,1000, function ($constraint) {
    $constraint->aspectRatio();
    $constraint->upsize();
});

// Even faster without parsing the image if you know original's w,h
$sizes = ImageManagerStatic::canvas($w,$h)->getSize()->resize($maxW,$maxH, function ($constraint) {
            $constraint->aspectRatio();
            $constraint->upsize();
        });
olivervogel commented 5 months ago

You can use Intervention\Image\Geometry\Tools\RectangleResizer::class to calculate size without actual images. You have to use version 3 of Intervention Image though.

$targetWidth = 60;
$targetHeight = 60;

$size = new Rectangle(300, 200); // original 300 x 200
$resized = $size->scaleDown($targetWidth, $targetHeight); // result: 60 x 40

See Rectangle::class and RectangleResizer::class