Keep image aspect ratio if only 1 dimension styled

If only one of image width or height is specified, then scale missing
dimension to maintain the aspect ratio.
This commit is contained in:
japonicus 2014-04-08 17:42:01 +01:00
parent cf7263b97c
commit 833dfea1e0
1 changed files with 11 additions and 3 deletions

View File

@ -131,9 +131,17 @@ class Image extends AbstractElement
$this->source = $source; $this->source = $source;
$this->isWatermark = $isWatermark; $this->isWatermark = $isWatermark;
$this->style = $this->setStyle(new ImageStyle(), $style, true); $this->style = $this->setStyle(new ImageStyle(), $style, true);
if ($this->style->getWidth() == null && $this->style->getHeight() == null) { $styleWidth = $this->style->getWidth();
$this->style->setWidth($imgData[0]); $styleHeight = $this->style->getHeight();
$this->style->setHeight($imgData[1]); if (!($styleWidth && $styleHeight)) {
if ($styleWidth == null && $styleHeight == null) {
$this->style->setWidth($imgData[0]);
$this->style->setHeight($imgData[1]);
} else if ($styleWidth) {
$this->style->setHeight($imgData[1] * ($styleWidth / $imgData[0]));
} else {
$this->style->setWidth($imgData[0] * ($styleHeight / $imgData[1]));
}
} }
$this->setImageFunctions(); $this->setImageFunctions();
} }