Skip to content

Commit e78489b

Browse files
committed
Update unit test and changelog
1 parent abc67ed commit e78489b

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This release marked heavy refactorings on internal code structure with the creat
3131
- Endnote: Ability to add endnotes - @ivanlanin
3232
- ListItem: Ability to create custom list and reset list number - @ivanlanin GH-10 GH-198
3333
- ODT Writer: Basic table writing support - @ivanlanin
34+
- Image: Keep image aspect ratio if only 1 dimension styled - @japonicus GH-194
3435

3536
### Bugfixes
3637

src/PhpWord/Element/Image.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@ public function __construct($source, $style = null, $isWatermark = false)
133133
$this->style = $this->setStyle(new ImageStyle(), $style, true);
134134
$styleWidth = $this->style->getWidth();
135135
$styleHeight = $this->style->getHeight();
136+
list($actualWidth, $actualHeight) = $imgData;
136137
if (!($styleWidth && $styleHeight)) {
137138
if ($styleWidth == null && $styleHeight == null) {
138-
$this->style->setWidth($imgData[0]);
139-
$this->style->setHeight($imgData[1]);
140-
} else if ($styleWidth) {
141-
$this->style->setHeight($imgData[1] * ($styleWidth / $imgData[0]));
139+
$this->style->setWidth($actualWidth);
140+
$this->style->setHeight($actualHeight);
141+
} elseif ($styleWidth) {
142+
$this->style->setHeight($actualHeight * ($styleWidth / $actualWidth));
142143
} else {
143-
$this->style->setWidth($imgData[0] * ($styleHeight / $imgData[1]));
144+
$this->style->setWidth($actualWidth * ($styleHeight / $actualHeight));
144145
}
145146
}
146147
$this->setImageFunctions();

tests/PhpWord/Tests/Element/ImageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function testWatermark()
129129
public function testPNG()
130130
{
131131
$src = __DIR__ . "/../_files/images/firefox.png";
132-
$oImage = new Image($src);
132+
$oImage = new Image($src, array('width' => 100));
133133

134134
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage);
135135
$this->assertEquals($oImage->getSource(), $src);
@@ -146,7 +146,7 @@ public function testPNG()
146146
public function testGIF()
147147
{
148148
$src = __DIR__ . "/../_files/images/mario.gif";
149-
$oImage = new Image($src);
149+
$oImage = new Image($src, array('height' => 100));
150150

151151
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Image', $oImage);
152152
$this->assertEquals($oImage->getSource(), $src);

0 commit comments

Comments
 (0)