Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#5385 in develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Maks3w committed Feb 23, 2014
169 parents 0b33b13 + f13d677 + fff768a + 2d33c4d + 4aebbda + a47e3ed + ddb35c9 + 8a05e9b + a92be28 + 5d9546f + 644a1db + 1008053 + c0d0bfc + ab83210 + 88be4ef + e672b58 + c5c6433 + a6b0c4e + 28d72f0 + 48c143c + acee230 + 9a49788 + efd47ef + 8dad02c + 9be7abf + bf9e60b + a03898c + 025fb19 + 6551ff4 + e5066db + 30065eb + cd23bdb + f518ed9 + 54153cf + e4984e8 + cc9c920 + b3042ce + e57fd9d + d07707d + 60d87e0 + dea915e + 4f2f391 + e61f957 + a301eb4 + 86ee99c + 4f18442 + 0560233 + 803be33 + b3b44b2 + 747588e + ff0efd2 + 5651d65 + a3fbf6b + 2a0a9d7 + e987caa + f6cf6f8 + a97175a + 18ecd64 + f00b78e + 18be569 + af6e96a + a62b890 + 369a5ce + 2f4d803 + 045137d + 2fdcee4 + 147c99e + d95685a + 9fceeb6 + 4682ed8 + 58e6c7d + f9a818b + b470c79 + ce259b2 + 47e8c8b + 62bd37a + f6216bb + 01cd418 + 1d5008a + f7d1866 + 339c91f + 105863b + d697919 + a07d04f + ab69e95 + a01fec1 + 79aa858 + e6c1f7b + caa3725 + 42e5478 + 2d5d2cd + 80ddcc5 + e2d6bae + 4d230a7 + eb81fcd + d11d08b + 260e6bd + 7ad155b + 5fc070f + c78a364 + 4941bab + c4245dd + b4f05cd + a274d7e + 9ddc5d3 + b327453 + 4fb6bdc + 589d10e + 1190c26 + 7a8c4e6 + 34dd90e + 181ab3e + 67215df + 3722470 + 760ae55 + 71ba5e4 + 0e69738 + 5fee79a + edb69b7 + affa8ee + b842366 + 8bcb41c + 2d61aeb + 3e577bd + 2912cc2 + 480b09d + 9ef4bbd + a9fbb32 + a3442c5 + 3ba9ec5 + 249bcf9 + 17f4e58 + f5df8ff + 09c6fb7 + 244cca2 + e70f1f7 + c0ba5d9 + af32048 + e486343 + 2925378 + 7987d36 + f33a6b9 + ccec312 + c6e5159 + 5686261 + 032f44f + c050df1 + c5316e9 + e3aa8e4 + c5faeb8 + 8cb76ea + d3e8682 + ebc5f97 + fee3d00 + 89b4efe + aabf76f + d2e2f88 + 54946f9 + f094f08 + d90409d + 111c229 + 35b35e9 + 15d7ed8 + f72bde6 + 0e2bc7a + 55f205f + 9fb4637 + 0996891 + 4b329dd commit 3388d6b
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 64 deletions.
28 changes: 28 additions & 0 deletions src/Renderer/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ abstract class AbstractRenderer implements RendererInterface
*/
protected $resource;

/**
* Show a transparent background
* @var Boolean
*/
protected $transparentBackground = false;

/**
* Constructor
* @param array|Traversable $options
Expand Down Expand Up @@ -135,6 +141,28 @@ public function getRendererNamespace()
return $this->rendererNamespace;
}

/**
* Set whether background should be transparent
* Will work for SVG and Image (png and gif only)
*
* @param $bool
* @return $this
*/
public function setTransparentBackground($bool)
{
$this->transparentBackground = $bool;

return $this;
}

/**
* @return bool
*/
public function getTransparentBackground()
{
return $this->transparentBackground;
}

/**
* Retrieve renderer type
* @return string
Expand Down
113 changes: 55 additions & 58 deletions src/Renderer/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Image extends AbstractRenderer
* @var array
*/
protected $allowedImageType = array('png',
'jpeg',
'gif' );
'jpeg',
'gif' );

/**
* Image format
Expand Down Expand Up @@ -194,22 +194,7 @@ protected function initRenderer()
$barcodeWidth = $this->barcode->getWidth(true);
$barcodeHeight = $this->barcode->getHeight(true);

if ($this->resource !== null) {
$foreColor = $this->barcode->getForeColor();
$backgroundColor = $this->barcode->getBackgroundColor();
$this->imageBackgroundColor = imagecolorallocate(
$this->resource,
($backgroundColor & 0xFF0000) >> 16,
($backgroundColor & 0x00FF00) >> 8,
$backgroundColor & 0x0000FF
);
$this->imageForeColor = imagecolorallocate(
$this->resource,
($foreColor & 0xFF0000) >> 16,
($foreColor & 0x00FF00) >> 8,
$foreColor & 0x0000FF
);
} else {
if (is_null($this->resource)) {
$width = $barcodeWidth;
$height = $barcodeHeight;
if ($this->userWidth && $this->barcode->getType() != 'error') {
Expand All @@ -219,32 +204,44 @@ protected function initRenderer()
$height = $this->userHeight;
}

$foreColor = $this->barcode->getForeColor();
$backgroundColor = $this->barcode->getBackgroundColor();
$this->resource = imagecreatetruecolor($width, $height);

$this->imageBackgroundColor = imagecolorallocate(
$this->resource,
($backgroundColor & 0xFF0000) >> 16,
($backgroundColor & 0x00FF00) >> 8,
$backgroundColor & 0x0000FF
);
$this->imageForeColor = imagecolorallocate(
$this->resource,
($foreColor & 0xFF0000) >> 16,
($foreColor & 0x00FF00) >> 8,
$foreColor & 0x0000FF
);
$white = imagecolorallocate($this->resource, 255, 255, 255);
imagefilledrectangle($this->resource, 0, 0, $width - 1, $height - 1, $white);
}

$foreColor = $this->barcode->getForeColor();
$this->imageForeColor = imagecolorallocate(
$this->resource,
($foreColor & 0xFF0000) >> 16,
($foreColor & 0x00FF00) >> 8,
$foreColor & 0x0000FF
);

$backgroundColor = $this->barcode->getBackgroundColor();
$this->imageBackgroundColor = imagecolorallocate(
$this->resource,
($backgroundColor & 0xFF0000) >> 16,
($backgroundColor & 0x00FF00) >> 8,
$backgroundColor & 0x0000FF
);

// JPEG does not support transparency, if transparentBackground is true and
// image type is JPEG, ignore transparency
if($this->getImageType() != "jpeg" && $this->transparentBackground) {
imagecolortransparent($this->resource, $this->imageBackgroundColor);
}

$this->adjustPosition(imagesy($this->resource), imagesx($this->resource));
imagefilledrectangle($this->resource,
$this->leftOffset,
$this->topOffset,
$this->leftOffset + $barcodeWidth - 1,
$this->topOffset + $barcodeHeight - 1,
$this->imageBackgroundColor);

imagefilledrectangle(
$this->resource,
$this->leftOffset,
$this->topOffset,
$this->leftOffset + $barcodeWidth - 1,
$this->topOffset + $barcodeHeight - 1,
$this->imageBackgroundColor
);
}

/**
Expand Down Expand Up @@ -330,18 +327,18 @@ public function render()
protected function drawPolygon($points, $color, $filled = true)
{
$newPoints = array($points[0][0] + $this->leftOffset,
$points[0][1] + $this->topOffset,
$points[1][0] + $this->leftOffset,
$points[1][1] + $this->topOffset,
$points[2][0] + $this->leftOffset,
$points[2][1] + $this->topOffset,
$points[3][0] + $this->leftOffset,
$points[3][1] + $this->topOffset, );
$points[0][1] + $this->topOffset,
$points[1][0] + $this->leftOffset,
$points[1][1] + $this->topOffset,
$points[2][0] + $this->leftOffset,
$points[2][1] + $this->topOffset,
$points[3][0] + $this->leftOffset,
$points[3][1] + $this->topOffset, );

$allocatedColor = imagecolorallocate($this->resource,
($color & 0xFF0000) >> 16,
($color & 0x00FF00) >> 8,
$color & 0x0000FF );
($color & 0xFF0000) >> 16,
($color & 0x00FF00) >> 8,
$color & 0x0000FF );

if ($filled) {
imagefilledpolygon($this->resource, $newPoints, 4, $allocatedColor);
Expand All @@ -365,9 +362,9 @@ protected function drawPolygon($points, $color, $filled = true)
protected function drawText($text, $size, $position, $font, $color, $alignment = 'center', $orientation = 0)
{
$allocatedColor = imagecolorallocate($this->resource,
($color & 0xFF0000) >> 16,
($color & 0x00FF00) >> 8,
$color & 0x0000FF );
($color & 0xFF0000) >> 16,
($color & 0x00FF00) >> 8,
$color & 0x0000FF );

if ($font == null) {
$font = 3;
Expand Down Expand Up @@ -422,13 +419,13 @@ protected function drawText($text, $size, $position, $font, $color, $alignment =
break;
}
imagettftext($this->resource,
$size,
$orientation,
$position[0] - ($width * cos(pi() * $orientation / 180)),
$position[1] + ($width * sin(pi() * $orientation / 180)),
$allocatedColor,
$font,
$text);
$size,
$orientation,
$position[0] - ($width * cos(pi() * $orientation / 180)),
$position[1] + ($width * sin(pi() * $orientation / 180)),
$allocatedColor,
$font,
$text);
}
}
}
35 changes: 29 additions & 6 deletions src/Renderer/Svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class Svg extends AbstractRenderer
*/
protected $userWidth = 0;


/**
* Flag to determime if drawPolygon has been run once already
* @var bool
*/
protected $drawPolygonExecuted = false;

/**
* Set height of the result image
* @param null|int $value
Expand Down Expand Up @@ -152,12 +159,17 @@ protected function initRenderer()
}
$this->adjustPosition($height, $width);

$this->appendRootElement('rect',
array('x' => $this->leftOffset,
'y' => $this->topOffset,
'width' => ($this->leftOffset + $barcodeWidth - 1),
'height' => ($this->topOffset + $barcodeHeight - 1),
'fill' => $imageBackgroundColor));
$rect = array('x' => $this->leftOffset,
'y' => $this->topOffset,
'width' => ($this->leftOffset + $barcodeWidth - 1),
'height' => ($this->topOffset + $barcodeHeight - 1),
'fill' => $imageBackgroundColor);

if($this->transparentBackground) {
$rect['fill-opacity'] = 0;
}

$this->appendRootElement('rect', $rect);
}

protected function readRootElement()
Expand Down Expand Up @@ -309,6 +321,17 @@ protected function drawPolygon($points, $color, $filled = true)
$newPoints = implode(' ', $newPoints);
$attributes['points'] = $newPoints;
$attributes['fill'] = $color;

// SVG passes a rect in as the first call to drawPolygon, we'll need to intercept
// this and set transparency if necessary.
$objId = spl_object_hash($this);
if(!$this->drawPolygonExecuted) {
if($this->transparentBackground) {
$attributes['fill-opacity'] = '0';
}
$this->drawPolygonExecuted = true;
}

$this->appendRootElement('polygon', $attributes);
}

Expand Down
72 changes: 72 additions & 0 deletions test/Renderer/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,78 @@ public function testTopOffsetOverrideVerticalPosition()
parent::testTopOffsetOverrideVerticalPosition();
}

/**
* @group 4708
*/
public function testImageGifWithNoTransparency()
{
$barcode = new Object\Code39(array('text' => '0123456789'));
$this->renderer->setBarcode($barcode);

$this->renderer->setTransparentBackground(false);
$this->assertFalse($this->renderer->getTransparentBackground());

//Test Gif output
$this->renderer->setImageType('gif');
$image = $this->renderer->draw();
$index = imagecolortransparent($image);
$this->assertEquals($index, -1);
}

/**
* @group 4708
*/
public function testImagePngWithNoTransparency()
{
$barcode = new Object\Code39(array('text' => '0123456789'));
$this->renderer->setBarcode($barcode);

$this->renderer->setTransparentBackground(false);
$this->assertFalse($this->renderer->getTransparentBackground());

//Test PNG output
$this->renderer->setImageType('png');
$image = $this->renderer->draw();
$index = imagecolortransparent($image);
$this->assertEquals($index, -1);
}

/**
* @group 4708
*/
public function testImageGifWithTransparency()
{
$barcode = new Object\Code39(array('text' => '0123456789'));
$this->renderer->setBarcode($barcode);

$this->renderer->setTransparentBackground(true);
$this->assertTrue($this->renderer->getTransparentBackground());

//Test Gif output
$this->renderer->setImageType('gif');
$image = $this->renderer->draw();
$index = imagecolortransparent($image);
$this->assertTrue($index !== -1);
}

/**
* @group 4708
*/
public function testImagePngWithTransparency()
{
$barcode = new Object\Code39(array('text' => '0123456789'));
$this->renderer->setBarcode($barcode);

$this->renderer->setTransparentBackground(true);
$this->assertTrue($this->renderer->getTransparentBackground());

//Test PNG output
$this->renderer->setImageType('png');
$image = $this->renderer->draw();
$index = imagecolortransparent($image);
$this->assertTrue($index !== -1);
}

protected function checkTTFRequirement()
{
if (!function_exists('imagettfbbox')) {
Expand Down
37 changes: 37 additions & 0 deletions test/Renderer/SvgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,42 @@ protected function getRendererObject($options = null)
return new Svg($options);
}

/**
* @group 4708
*
* Needs to be run first due to runOnce static on drawPolygon
*/
public function testSvgNoTransparency()
{
$svgCompare = file_get_contents(__DIR__ . '/_files/svg_transparency.xml');

Barcode\Barcode::setBarcodeFont(__DIR__ . '/../Object/_fonts/Vera.ttf');
$barcode = new Code39(array('text' => '0123456789'));
$this->renderer->setBarcode($barcode);

$this->assertFalse($this->renderer->getTransparentBackground());
$svgOutput = $this->renderer->draw()->saveXML();
$this->assertNotEquals($svgCompare, $svgOutput);
}

/**
* @group 4708
*
* Needs to be run first due to runOnce static on drawPolygon
*/
public function testSvgTransparency()
{
$svgCompare = file_get_contents(__DIR__ . '/_files/svg_transparency.xml');

Barcode\Barcode::setBarcodeFont(__DIR__ . '/../Object/_fonts/Vera.ttf');
$barcode = new Code39(array('text' => '0123456789'));
$this->renderer->setBarcode($barcode);
$this->renderer->setTransparentBackground(true);
$this->assertTrue($this->renderer->getTransparentBackground());
$svgOutput = $this->renderer->draw()->saveXML();
$this->assertEquals($svgCompare, $svgOutput);
}

public function testType()
{
$this->assertSame('svg', $this->renderer->getType());
Expand Down Expand Up @@ -105,4 +141,5 @@ protected function getRendererWithWidth500AndHeight300()
$svg->appendChild($rootElement);
return $this->renderer->setResource($svg);
}

}
Loading

0 comments on commit 3388d6b

Please sign in to comment.