parent
1d28714d0a
commit
c2871fca76
2 changed files with 175 additions and 0 deletions
107
src/classes/API/PlaceholdIt.php
Normal file
107
src/classes/API/PlaceholdIt.php
Normal file
|
@ -0,0 +1,107 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Placehold.it Class File for PICKLES
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistribution of these files must retain the above copyright notice.
|
||||
*
|
||||
* @author Josh Sherman <josh@gravityblvd.com>
|
||||
* @copyright Copyright 2007-2014, Josh Sherman
|
||||
* @license http://www.opensource.org/licenses/mit-license.html
|
||||
* @package PICKLES
|
||||
* @link https://github.com/joshtronic/pickles
|
||||
*/
|
||||
|
||||
/**
|
||||
* Placehold.it API Interface
|
||||
*
|
||||
* @link http://placehold.it
|
||||
*/
|
||||
class API_PlaceholdIt extends Object
|
||||
{
|
||||
/**
|
||||
* URL
|
||||
*
|
||||
* Generates a Placehold.it URL based on the passed parameters.
|
||||
*
|
||||
* @param integer $width width of the image
|
||||
* @param integer $height optional height of the image
|
||||
* @param string $format optional format of the image
|
||||
* @param string $background optional background color of the image
|
||||
* @param string $foreground optional foreground color of the image
|
||||
* @param string $text optional text to display in the image
|
||||
* @return string Placehold.it URL
|
||||
*/
|
||||
public function url($width, $height = null, $format = 'gif', $background = null, $foreground = null, $text = null)
|
||||
{
|
||||
// Checks if the format is valid
|
||||
if (!in_array($format, ['gif', 'jpeg', 'jpg', 'png']))
|
||||
{
|
||||
throw new Exception('Invalid format. Valid formats: gif, jpeg, jpg and png.');
|
||||
}
|
||||
// Checks if foreground is present without background
|
||||
elseif ($foreground && !$background)
|
||||
{
|
||||
throw new Exception('You must specify a background color if you wish to specify a foreground color.');
|
||||
}
|
||||
// Checks the background color's length
|
||||
elseif ($background && strlen($background) < 6)
|
||||
{
|
||||
throw new Exception('The background color must be a 6 character hex code.');
|
||||
}
|
||||
// Checks the foreground color's length
|
||||
elseif ($foreground && strlen($foreground) < 6)
|
||||
{
|
||||
throw new Exception('The foreground color must be a 6 character hex code.');
|
||||
}
|
||||
|
||||
$url = 'http://placehold.it/' . $width;
|
||||
|
||||
if ($height)
|
||||
{
|
||||
$url .= 'x' . $height;
|
||||
}
|
||||
|
||||
$url .= '.' . $format;
|
||||
|
||||
if ($background)
|
||||
{
|
||||
$url .= '/' . $background;
|
||||
}
|
||||
|
||||
if ($foreground)
|
||||
{
|
||||
$url .= '/' . $foreground;
|
||||
}
|
||||
|
||||
if ($text)
|
||||
{
|
||||
$url .= '&text=' . urlencode($text);
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* URL
|
||||
*
|
||||
* Generates a Placehold.it <img> tag based on the passed parameters.
|
||||
*
|
||||
* @param integer $width width of the image
|
||||
* @param integer $height optional height of the image
|
||||
* @param string $format optional format of the image
|
||||
* @param string $background optional background color of the image
|
||||
* @param string $foreground optional foreground color of the image
|
||||
* @param string $text optional text to display in the image
|
||||
* @return string <img> tag with the Placehold.it URL
|
||||
*/
|
||||
public function img($width, $height = null, $format = 'gif', $background = null, $foreground = null, $text = null)
|
||||
{
|
||||
return '<img src="' . $this->url($width, $height, $format, $background, $foreground, $text) . '">';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
68
tests/classes/API/PlaceholdItTest.php
Normal file
68
tests/classes/API/PlaceholdItTest.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
class API_PlaceholdIt_Test extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $placeholdit;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->placeholdit = new API_PlaceholdIt();
|
||||
}
|
||||
|
||||
public function testInstantiateObject()
|
||||
{
|
||||
$this->assertInstanceOf('API_PlaceholdIt', $this->placeholdit);
|
||||
}
|
||||
|
||||
public function testURL()
|
||||
{
|
||||
$expected = 'http://placehold.it/350x150.png/ffffff/000000&text=PICKLES+Rules%21';
|
||||
$url = $this->placeholdit->url(350, 150, 'png', 'ffffff', '000000', 'PICKLES Rules!');
|
||||
$this->assertEquals($expected, $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage Invalid format. Valid formats: gif, jpeg, jpg and png.
|
||||
*/
|
||||
public function testInvalidFormat()
|
||||
{
|
||||
$this->placeholdit->url(350, 150, 'invalid');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage You must specify a background color if you wish to specify a foreground color.
|
||||
*/
|
||||
public function testForegroundNoBackground()
|
||||
{
|
||||
$this->placeholdit->url(350, 150, 'png', false, '000000');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage The background color must be a 6 character hex code.
|
||||
*/
|
||||
public function testInvalidBackground()
|
||||
{
|
||||
$this->placeholdit->url(350, 150, 'png', 'fff');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage The foreground color must be a 6 character hex code.
|
||||
*/
|
||||
public function testInvalidForeground()
|
||||
{
|
||||
$this->placeholdit->url(350, 150, 'png', 'ffffff', '000');
|
||||
}
|
||||
|
||||
public function testIMG()
|
||||
{
|
||||
$expected = '<img src="http://placehold.it/350x150.png/ffffff/000000&text=PICKLES+Rules%21">';
|
||||
$url = $this->placeholdit->img(350, 150, 'png', 'ffffff', '000000', 'PICKLES Rules!');
|
||||
$this->assertEquals($expected, $url);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue