0
在一個個人項目中,我需要從一個使用php Imagine庫(http://imagine.readthedocs.io)實現ImageInterface
(圖像)寬度和高度的對象獲得。Php想象一下獲取圖像的寬度和高度
,我需要解決的具體問題的方式,調整後的圖像保持原始寬高比,你可以在下面的類看到調整圖像大小:
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
}
}
但我怎麼能得到圖像的寬度和高度?
我發現的所有解決方案都是直接使用gd,imagick等etc庫,如:Get image height and width PHP而不是Imagine。
問題是,我提供了一個最大寬度和高度的盒子,我想用長寬比來適應所提供的盒子。 –
啊。那麼你需要做一些手動計算,我不認爲Imagine支持那個開箱即用的。 – rickdenhaan
這就是我所做的;) –