Skip to content

Commit fe49102

Browse files
authored
Merge pull request #18 from tbielek/rotate
Add support for rotation of images according to EXIF header
2 parents d0e0f80 + 15f967c commit fe49102

7 files changed

Lines changed: 56 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor/
22
/coverage/
33
/clover.xml
4+
/nbproject/private/

src/Image.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ public static function resizeMulti(\Imagick $source, array $boxSizes, array $opt
101101

102102
$clone = clone $source;
103103

104+
$orientation = $clone->getImageOrientation();
105+
switch ($orientation)
106+
{
107+
case \Imagick::ORIENTATION_BOTTOMRIGHT:
108+
$clone->rotateimage('#fff', 180);
109+
$clone->stripImage();
110+
break;
111+
case \Imagick::ORIENTATION_RIGHTTOP:
112+
$clone->rotateimage('#fff', 90);
113+
$clone->stripImage();
114+
break;
115+
case \Imagick::ORIENTATION_LEFTBOTTOM:
116+
$clone->rotateimage('#fff', -90);
117+
$clone->stripImage();
118+
break;
119+
}
120+
104121
$width = $clone->getImageWidth();
105122
$height = $clone->getImageHeight();
106123

tests/ImageTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,44 @@ public function resize_nonBoolUpsize()
309309
Image::resize(new \Imagick(), 10, 10, ['upsize' => 'not bool']);
310310
}
311311

312+
/**
313+
* Verify images are rotated according to EXIF header
314+
* @test
315+
* @covers ::resize
316+
* @covers ::resizeMulti
317+
*/
318+
public function resize_orientation()
319+
{
320+
$files = [
321+
"{$this->_sourceFilesDir}/bottom-right.jpg",
322+
"{$this->_sourceFilesDir}/left-bottom.jpg",
323+
"{$this->_sourceFilesDir}/right-top.jpg",
324+
"{$this->_sourceFilesDir}/top-left.jpg",
325+
];
326+
327+
$imageResults = [];
328+
329+
foreach ($files as $file) {
330+
$source = new \Imagick($file);
331+
$imageWidth = $source->getimagewidth();
332+
$imageHeight = $source->getimageheight();
333+
$imageResults[] = Image::resize($source, $imageWidth, $imageHeight, []);
334+
}
335+
336+
$this->assertSame(
337+
['r' => 254, 'g' => 0, 'b' => 0, 'a' => 1], $imageResults[0]->getImagePixelColor(0, 0)->getColor()
338+
);
339+
$this->assertSame(
340+
['r' => 0, 'g' => 0, 'b' => 0, 'a' => 1], $imageResults[1]->getImagePixelColor(0, 0)->getColor()
341+
);
342+
$this->assertSame(
343+
['r' => 0, 'g' => 255, 'b' => 1, 'a' => 1], $imageResults[2]->getImagePixelColor(0, 0)->getColor()
344+
);
345+
$this->assertSame(
346+
['r' => 0, 'g' => 0, 'b' => 254, 'a' => 1], $imageResults[3]->getImagePixelColor(0, 0)->getColor()
347+
);
348+
}
349+
312350
/**
313351
* Downsize ratio 2.0 to 0.25 and 2.0 to 4.0
314352
*

tests/_files/bottom-right.jpg

876 Bytes
Loading

tests/_files/left-bottom.jpg

876 Bytes
Loading

tests/_files/right-top.jpg

876 Bytes
Loading

tests/_files/top-left.jpg

876 Bytes
Loading

0 commit comments

Comments
 (0)