Skip to content

Commit

Permalink
handle image extensions in doctrine loader
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Dec 4, 2013
1 parent 61597fc commit 4774329
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions Imagine/Data/Loader/AbstractDoctrineLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,29 @@ abstract class AbstractDoctrineLoader implements LoaderInterface
*/
protected $class;

/**
* @var array
*/
protected $formats;

/**
* Constructor.
*
* @param ImagineInterface $imagine
* @param ObjectManager $manager
* @param string $class
* @param array $formats possible image formats to look up file ids
*/
public function __construct(ImagineInterface $imagine, ObjectManager $manager, $class = null)
{
public function __construct(
ImagineInterface $imagine,
ObjectManager $manager,
$class = null,
array $formats = array()
) {
$this->imagine = $imagine;
$this->manager = $manager;
$this->class = $class;
$this->formats = $formats;
}

/**
Expand Down Expand Up @@ -64,6 +75,24 @@ public function find($path)
{
$image = $this->manager->find($this->class, $this->mapPathToId($path));

if (!$image) {
// try to find the image without extension
$info = pathinfo($path);
$name = $info['dirname'].'/'.$info['filename'];

// attempt to determine available format
foreach ($this->formats as $format) {
if ($image = $this->manager->find($this->class, $this->mapPathToId($name.'.'.$format))) {
break;
}
}

// maybe the image has an id without extension
if (!$image) {
$image = $this->manager->find($this->class, $this->mapPathToId($name));
}
}

if (!$image) {
throw new NotFoundHttpException(sprintf('Source image not found with id "%s"', $path));
}
Expand Down

0 comments on commit 4774329

Please sign in to comment.