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.
You can use the
getSize()
method for that:Although, if you want to resize with aspect ratio, you can also use the
scale()
method for theBoxInterface
to get the new measurements without having to calculate that yourself: