2014-09-06 21 views
2

我與想象庫Zend Framework中2個工作:Imagine想象一下,ZF2:比調整

我想得做的就是一些圖片以縮略圖我的圖片頁面,也使其加載速度更快。 但問題是我不知道如何在庫中調整比例。

有人知道你如何調整比例。 所以我想說,圖片是300寬度,它會自動計算高度。

這是我的代碼在這一刻:

public function resizeImage($photo , $width, $height) 
{ 
    $sm = $this->getServiceLocator(); 
    $imagine = $sm->get('image_service'); 
    $image = $imagine->open('public/img/gallery/album1/thumbnails/klj1.jpg'); 
    $image->resize(new Box($width, $height)); 
    $image->save('public/img/gallery/album1/thumbnails/klj1-thumb.jpg'); 
} 

回答

3

我使用下面的計算來計算比例:

$size = $image->getSize(); 
if ($height < $width) 
{ 
    $divider = $size->getWidth()/$width; 
    $calcHeight = $size->getHeight()/$divider; 
    $calcWidth = $width; 
} else { 
    $divider = $size->getHeight()/$height; 
    $calcWidth = $size->getWidth()/$divider; 
    $calcHeight = $height; 
}  
$image->resize(new Box($calcWidth, $calcHeight));