Skip to content

Commit

Permalink
more caption tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraser Reed committed Feb 10, 2014
1 parent 4344698 commit 99b51ef
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ meme-puush
[![Build Status](https://secure.travis-ci.org/fraserreed/meme-puush.png?branch=master)](http://travis-ci.org/fraserreed/meme-puush)
[![Coverage Status](https://coveralls.io/repos/fraserreed/meme-puush/badge.png?branch=master)](https://coveralls.io/r/fraserreed/meme-puush?branch=master)

Meme generator using Imagick. Either output local file path or Puu.sh URL. Puu.sh api key required (http://puu.sh)
Meme generator using Imagick. Either output local file path or Puu.sh URL. Puu.sh api key required (http://puu.sh)
86 changes: 81 additions & 5 deletions src/MemePuush/Caption.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace MemePuush;


use Imagick, ImagickDraw, ImagickPixel;
use Imagick;
use ImagickDraw;
use ImagickPixel;

class Caption
{
Expand All @@ -12,6 +14,21 @@ class Caption
*/
protected $target;

/**
* @var ImagickDraw
*/
protected $draw;

/**
* @var ImagickPixel
*/
protected $pixel;

/**
* @var int
*/
protected $gravity;

/**
* @var string
*/
Expand Down Expand Up @@ -180,24 +197,83 @@ public function getBoundingBox()
return $this->boundingBox;
}

/**
* @param \ImagickDraw $draw
*/
public function setDraw( ImagickDraw $draw )
{
$this->draw = $draw;
}

/**
* @return \ImagickDraw
*/
private function getDraw()
{
if( !$this->draw )
$this->draw = new ImagickDraw();

return $this->draw;
}

/**
* @param \ImagickPixel $pixel
*/
public function setPixel( ImagickPixel $pixel )
{
$this->pixel = $pixel;
}

/**
* @param $color
*
* @return \ImagickPixel
*/
private function getPixel( $color )
{
if( !$this->pixel )
$this->pixel = new ImagickPixel( $color );

return $this->pixel;
}

/**
* @param int $gravity
*/
public function setGravity( $gravity )
{
$this->gravity = $gravity;
}

/**
* @return int
*/
private function getGravity()
{
if( !$this->gravity )
$this->gravity = Imagick::GRAVITY_NORTH;

return $this->gravity;
}

/**
* @return ImagickDraw
*/
public function getDrawLayer()
{
//initialize the draw layer
$drawLayer = new ImagickDraw();
$drawLayer = $this->getDraw();

//set the font
$drawLayer->setFont( $this->getFontPath() );

//set stroke colour to black
$drawLayer->setStrokeColor( new ImagickPixel( "#000000" ) );
$drawLayer->setStrokeColor( $this->getPixel( "#000000" ) );
$drawLayer->setStrokeAntialias( true );
$drawLayer->setTextAntialias( true );

//north gravity is top center
$drawLayer->setGravity( Imagick::GRAVITY_NORTH );
$drawLayer->setGravity( $this->getGravity() );

//set alignment to center
$drawLayer->setTextAlignment( 2 );
Expand All @@ -206,7 +282,7 @@ public function getDrawLayer()
$drawLayer->setTextKerning( 0.75 );

//set font colour to black initially to create a smooth stroke
$drawLayer->setFillColor( new ImagickPixel( "#000000" ) );
$drawLayer->setFillColor( $this->getPixel( "#000000" ) );

//and make the stroke transparent
$drawLayer->setStrokeAlpha( 100 );
Expand Down
32 changes: 27 additions & 5 deletions tests/MemePuush/Tests/CaptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CaptionTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->image = new Image();
$imagick = $this->getMock( 'Imagick', array( 'getImageGeometry' ), array() );
$imagick = $this->getMock( 'Imagick', array( 'getImageGeometry', 'queryFontMetrics' ), array() );
$imagick->expects( $this->any() )->method( 'getImageGeometry' )->will( $this->returnValue( array( 'width' => 480, 'height' => 640 ) ) );

$this->image->setImage( $imagick );
Expand Down Expand Up @@ -149,14 +149,36 @@ public function testGetBoundingBoxDefaultBottom()

/**
* @covers MemePuush\Caption::getDrawLayer
* @todo Implement testGetDrawLayer().
* @covers MemePuush\Caption::setDraw
* @covers MemePuush\Caption::getDraw
* @covers MemePuush\Caption::setPixel
* @covers MemePuush\Caption::getPixel
* @covers MemePuush\Caption::setGravity
* @covers MemePuush\Caption::getGravity
*/
public function testGetDrawLayer()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
$object = new Caption( $this->image, 'twelve' );

$draw = $this->getMock(
'ImagickDraw',
array(
'setFont', 'setFontSize',
'setStrokeColor', 'setStrokeAntialias', 'setStrokeAlpha',
'setTextAntialias', 'setTextAlignment', 'setTextKerning', 'setGravity', 'setFillColor'
),
array()
);
$object->setDraw( $draw );

$pixel = $this->getMock( 'ImagickPixel' );
$object->setPixel( $pixel );

$object->setGravity( 2 );

$drawLayer = $object->getDrawLayer();

$this->assertEquals( $draw, $drawLayer );
}

/**
Expand Down

0 comments on commit 99b51ef

Please sign in to comment.