Skip to content

Commit

Permalink
Allowing to input the file_path's in string format in the ImageHelper…
Browse files Browse the repository at this point in the history
… methods `getUrl`, `getHtml` as this helper should also support the array approach.
  • Loading branch information
wtfzdotnet committed Mar 6, 2014
1 parent b676689 commit d443ad6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
50 changes: 26 additions & 24 deletions lib/Tmdb/Helper/ImageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,48 +41,50 @@ public function getImageConfiguration()
/**
* Get the url for the image resource
*
* @param Image $image
* @param string $size
* @param Image|string $image Either an instance of Image or the file_path
* @param string $size
* @return string
*/
public function getUrl(Image $image, $size = 'original')
public function getUrl($image, $size = 'original')
{
$config = $this->getImageConfiguration();

return $config['base_url'] . $size . $image->getFilePath();
return $config['base_url'] . $size . $image;
}

/**
* Get an img html tag for the image in the specified size
*
* @param Image $image
* @param string $size
* @param int|null $width
* @param int|null $height
* @param Image|string $image Either an instance of Image or the file_path
* @param string $size
* @param int|null $width
* @param int|null $height
* @return string
*/
public function getHtml(Image $image, $size = 'original', $width = null, $height = null)
public function getHtml($image, $size = 'original', $width = null, $height = null)
{
if (null == $image->getFilePath()) {
return '';
}
if ($image instanceof Image) {
if (null == $image->getFilePath()) {
return '';
}

$aspectRatio = $image->getAspectRatio();
$aspectRatio = $image->getAspectRatio();

if (null !== $width && null == $height && $aspectRatio !== null) {
$height = round($width / $aspectRatio);
}
if (null !== $width && null == $height && $aspectRatio !== null) {
$height = round($width / $aspectRatio);
}

if (null !== $height && null == $width && $aspectRatio !== null) {
$width = round($height * $aspectRatio);
}
if (null !== $height && null == $width && $aspectRatio !== null) {
$width = round($height * $aspectRatio);
}

if (null == $width) {
$width = $image->getWidth();
}
if (null == $width) {
$width = $image->getWidth();
}

if (null == $height) {
$height = $image->getHeight();
if (null == $height) {
$height = $image->getHeight();
}
}

return sprintf(
Expand Down
10 changes: 10 additions & 0 deletions lib/Tmdb/Model/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,14 @@ public function getWidth()
{
return $this->width;
}

/**
* Return the file path when casted to string
*
* @return mixed
*/
public function __toString()
{
return $this->getFilePath();
}
}
23 changes: 23 additions & 0 deletions test/Tmdb/Tests/Helper/ImageHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,27 @@ public function shouldBeEmptyIfFilePathIsNotGiven()

$this->assertEquals('', $this->helper->getHtml($image));
}

/**
* @test
*/
public function shouldGetImageUrlByString()
{
$imageUrl = $this->helper->getUrl('/1NfhdnQAEqcBRCulEhOFSkRrrLv.jpg');

$this->assertEquals('http://image.tmdb.org/t/p/original/1NfhdnQAEqcBRCulEhOFSkRrrLv.jpg', $imageUrl);
}

/**
* @test
*/
public function shouldGetImageElementByString()
{
$imageUrl = $this->helper->getHtml('/1NfhdnQAEqcBRCulEhOFSkRrrLv.jpg');

$this->assertEquals(
'<img src="http://image.tmdb.org/t/p/original/1NfhdnQAEqcBRCulEhOFSkRrrLv.jpg" width="" height="" />',
$imageUrl
);
}
}

0 comments on commit d443ad6

Please sign in to comment.