Skip to content

Commit

Permalink
Make getThumnailUrl() support more fileformats better
Browse files Browse the repository at this point in the history
* Serve raw url for svgs
* Only attempt to scale png, jpg and gif
  • Loading branch information
nervetattoo committed Jun 2, 2014
1 parent 431e479 commit a2484a8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
38 changes: 21 additions & 17 deletions src/Keyteq/Keymedia/Model/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public function getType()
return $this->file['type'];
}

public function isImage()
public function isImage(array $types = array())
{
return ('image' === $this->getGeneralType());
list($general,$type) = explode('/', $this->getType());
return $general === 'image' && (!$types || in_array($type, $types));
}

public function getUrl()
Expand All @@ -41,24 +42,18 @@ protected function getHost($protocol = 'http')
return "{$protocol}://{$this->host}";
}

protected function getGeneralType()
{
list($type,) = explode('/', $this->getType());
return $type;
}

public function getThumbnailUrl($width = null, $height = null)
{
if ($this->isImage()) {
if ($this->isImage(array('png', 'jpg', 'jpeg', 'gif'))) {
if (!(is_int($width) && is_int($height))) {
throw new \InvalidArgumentException('Image thumbnails require dimensions!');
}

return $this->getImageThumbnailUrl($width, $height);

} else {
return $this->getTypeThumbnailUrl();
}
elseif ($this->isImage(array('svg+xml'))) {
return $this->file['url'];
}
return $this->getTypeThumbnailUrl();
}

protected function getTypeThumbnailUrl()
Expand All @@ -72,14 +67,23 @@ protected function getTypeThumbnailUrl()

protected function getImageThumbnailUrl($width, $height)
{
return $this->buildUrl("{$width}x{$height}/{$this->_id}.png");
$ending = $this->getExtension();
return $this->buildUrl("{$width}x{$height}/{$this->_id}.{$ending}");
}

protected function getExtension()
{
$extension = $this->file['ending'];

return pathinfo($extension, PATHINFO_EXTENSION);
if ($this->file && isset($this->file['ending'])) {
$ending = $this->file['ending'];
}
else if ($this->file && isset($this->file['type'])) {
list($void,$ending) = explode('/', $this->file['type']);
$ending = '.' . $ending;
}
else {
throw new \Exception('Cant get file ending');
}
return pathinfo($ending, PATHINFO_EXTENSION);
}

protected function mapExtensionToType($extension)
Expand Down
28 changes: 20 additions & 8 deletions tests/Keyteq/Keymedia/Model/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,30 @@ public function testIsImage($data, $expected)

public function testGetImageThumbnailUrl()
{
$type = 'image/png';
$id = 'media_id';
$width = $height = 100;
$host = 'keymedia.dev';

$data = array('_id' => $id, 'host' => $host, 'file' => array('type' => $type));
$media = new Media($data);

$expected = "http://{$host}/{$width}x{$height}/{$id}.png";
$actual = $media->getThumbnailUrl($width, $height);
$fixtures = array(
array('_id' => 'png', 'host' => $host,
'file' => array('type' => 'image/png'),
'expected' => "http://{$host}/100x100/png.png"
),
array('_id' => 'jpg', 'host' => $host,
'file' => array('type' => 'image/jpeg'),
'expected' => "http://{$host}/100x100/jpg.jpeg"
),
array('_id' => 'svg', 'host' => $host,
'file' => array('type' => 'image/svg+xml', 'url' => 'relayed-url'),
'expected' => "relayed-url"
),
);

$this->assertEquals($expected, $actual);
foreach ($fixtures as $fixture) {
$expected = $fixture['expected'];
unset($fixture['expected']);
$media = new Media($fixture);
$this->assertEquals($expected, $media->getThumbnailUrl($width, $height));
}
}

/**
Expand Down

0 comments on commit a2484a8

Please sign in to comment.