Skip to content

Commit

Permalink
Improve performance of Image::getDimensions()
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Jul 22, 2016
1 parent 81a32c0 commit 4f15a56
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Contao\Image;

use Imagine\Image\Box;
use Imagine\Image\ImagineInterface;
use Imagine\Image\Point;
use Symfony\Component\Filesystem\Filesystem;
Expand Down Expand Up @@ -108,9 +109,20 @@ public function getUrl($rootDir)
public function getDimensions()
{
if (null === $this->dimensions) {
$this->dimensions = new ImageDimensions(
$this->imagine->open($this->getPath())->getSize()
);

// Try native getimagesize() for better performance
$size = @getimagesize($this->getPath());
if (!empty($size[0]) && !empty($size[1])) {
$this->dimensions = new ImageDimensions(new Box($size[0], $size[1]));
}

// Fallback to Imagine
else {
$this->dimensions = new ImageDimensions(
$this->imagine->open($this->getPath())->getSize()
);
}

}

return $this->dimensions;
Expand Down

0 comments on commit 4f15a56

Please sign in to comment.