1

On a personal project I need to get from an object that Implements a ImageInterface (the image) the width and height using the php Imagine library (http://imagine.readthedocs.io).

The specific problem that I need to solve is to resize an Image in a way that the resized image maintains the original Aspect Ratio as you can see in the following class:

namespace PcMagas\AppImageBundle\Filters\Resize;

use PcMagas\AppImageBundle\Filters\AbstractFilter;
use Imagine\Image\ImageInterface;
use PcMagas\AppImageBundle\Filters\ParamInterface;
use PcMagas\AppImageBundle\Exceptions\IncorectImageProssesingParamsException;

class ResizeToLimitsKeepintAspectRatio extends AbstractFilter
{
    public function apply(ImageInterface $image, ParamInterface $p) 
    {
        /**
         * @var ResizeParams $p
         */
        if(! $p instanceof ResizeParams){
            throw new IncorectImageProssesingParamsException(ResizeParams::class);
        }

        /**
         * @var float $imageAspectRatio
         */
        $imageAspectRatio=$this->calculateImageAspectRatio($image);



    }

    /**
     * @param ImageInterface $image
     * @return float
     */
    private function calculateImageAspectRatio(ImageInterface $image)
    {
        //Calculate the Image's Aspect Ratio
    }
}

But how can I get the image's width and height?

All the solutions I found are using directly the gd, imagick etc etc library such as: Get image height and width PHP and not the Imagine one.

2 Answers 2

2

You can use the getSize() method for that:

/**
 * @param ImageInterface $image
 * @return float
 */
private function calculateImageAspectRatio(ImageInterface $image)
{
    //Calculate the Image's Aspect Ratio
    $size = $image->getSize(); // returns a BoxInterface

    $width = $size->getWidth();
    $height = $size->getHeight();

    return $width / $height; // or $height / $width, depending on your usage
}

Although, if you want to resize with aspect ratio, you can also use the scale() method for the BoxInterface to get the new measurements without having to calculate that yourself:

$size = $image->getSize();

$width = $size->getWidth();    // 640
$height = $size->getHeight();  // 480

$size->scale(1.25); // increase 25%

$width = $size->getWidth();    // 800
$height = $size->getHeight();  // 600

// or, as a quick example to scale an image up by 25% immediately:
$image->resize($image->getSize()->scale(1.25));
Sign up to request clarification or add additional context in comments.

5 Comments

The problem is that I provide a Box with the maximum width and height and I want to scale with the aspect Ratio ans fits into the provided Box.
Ah. Then you will need to do some manual calculations, I don't think Imagine supports that out-of-the-box.
That's what I am doing ;)
@DimitriosDesyllas just to mention it here you can do that by using the INSET mode of imagine: $size = new Imagine\Image\Box(800, 600); $imagine->open('/path/to/large_image.jpg') ->thumbnail($size, Imagine\Image\ImageInterface::THUMBNAIL_INSET) ; imagine.readthedocs.io/en/stable/…
@AlexanderSchranz That's a lot shorter than my solution, can you add some example code as a new answer?
1

You can scale a image and keep its dimension by using the Inset mode on the thumbnail function:

$size = new Imagine\Image\Box(40, 40);

$mode = Imagine\Image\ImageInterface::THUMBNAIL_INSET;

$imagine->open('/path/to/large_image.jpg')
    ->thumbnail($size, $mode)
    ->save('/path/to/thumbnail.png')
;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.