Resolves #4708 - adding transparent background support to barcode #5385

Merged
merged 11 commits into from Feb 23, 2014

Projects

None yet

4 participants

@steverhoades
Contributor

There are currently no tests for this, the code in general here could probably be cleaned up a bit. Also, I have noticed that there is currently no 2.0 documentation on Barcode and ZendPdf doesn't exist.

Example Usage for Image:

        // Only the text to draw is required
        $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');

        // No required options
        $rendererOptions = array('imageType'=> 'png', 'transparentBackground' => true);

        \Zend\Barcode\Barcode::factory(
            'code39', 'image', $barcodeOptions, $rendererOptions
        )->render();

Example Usage for SVG:

        // Only the text to draw is required
        $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');

        // No required options
        $rendererOptions = array('transparentBackground' => true);

        \Zend\Barcode\Barcode::factory(
            'code39', 'svg', $barcodeOptions, $rendererOptions
        )->render();
@steverhoades
Contributor

Want to follow this up with a quick note, that it is discouraged to output barcodes that are NOT black on white. They may still work with your scanner though.

Source

@weierophinney
Member

@steverhoades We deprecated ZendPdf with ZF2; too difficult to maintain, as the spec is constantly evolving, and it's a very difficult thing to get the various features right.

We do have the old Barcode documentation -- I suspect it's simply not linked in the TOC and/or index, which is why you cannot see it in the rendered manual.

@weierophinney
Member

@steverhoades Any chance I can get some unit tests?

@steverhoades
Contributor

@weierophinney Actually, I think I may have been wrong with with the documentation, it may be in 2.

Should we remove the PDF Renderer for barcode?

I'll work on getting some unit tests in to support this.

@weierophinney
Member

@steverhoades Let's deprecate the PDF renderer for the next minor revision (2.3.0), and then we can remove in 3.

@samsonasik samsonasik commented on the diff Nov 12, 2013
library/Zend/Barcode/Renderer/AbstractRenderer.php
@@ -136,6 +142,28 @@ public function getRendererNamespace()
}
/**
+ * Set whether background should be transparent
+ * Will work for SVG and Image (png and gif only)
+ *
+ * @param $bool
+ * @return $this
@samsonasik
samsonasik Nov 12, 2013 Contributor

return self

@samsonasik samsonasik commented on the diff Nov 12, 2013
library/Zend/Barcode/Renderer/AbstractRenderer.php
@@ -136,6 +142,28 @@ public function getRendererNamespace()
}
/**
+ * Set whether background should be transparent
+ * Will work for SVG and Image (png and gif only)
+ *
+ * @param $bool
@samsonasik
samsonasik Nov 12, 2013 Contributor

@param bool $bool

@samsonasik samsonasik commented on the diff Nov 12, 2013
library/Zend/Barcode/Renderer/AbstractRenderer.php
@@ -80,6 +80,12 @@
protected $resource;
/**
+ * Show a transparent background
+ * @var Boolean
@weierophinney
Member

@steverhoades Any chance of getting this completed soon?

@steverhoades
Contributor

Matthew,

So sorry! Been on vacation with the Family for the Holidays. I'll look at
this as soon as I get settled in to see what is left.

Happy New Year!

Steve

On Fri, Jan 3, 2014 at 1:55 PM, weierophinney notifications@github.comwrote:

@steverhoades https://github.com/steverhoades Any chance of getting
this completed soon?


Reply to this email directly or view it on GitHubhttps://github.com/zendframework/zf2/pull/5385#issuecomment-31556929
.

@steverhoades
Contributor

Matthew,

Better late than never, just added the unit tests for the new feature to
barcode. Sorry it took so long!

#5385

Steve

On Wed, Jan 8, 2014 at 8:41 AM, Steve Rhoades sedonami@gmail.com wrote:

Matthew,

So sorry! Been on vacation with the Family for the Holidays. I'll look
at this as soon as I get settled in to see what is left.

Happy New Year!

Steve

On Fri, Jan 3, 2014 at 1:55 PM, weierophinney notifications@github.comwrote:

@steverhoades https://github.com/steverhoades Any chance of getting
this completed soon?

Reply to this email directly or view it on GitHubhttps://github.com/zendframework/zf2/pull/5385#issuecomment-31556929
.

@Maks3w Maks3w commented on an outdated diff Feb 21, 2014
library/Zend/Barcode/Renderer/Image.php
+ $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
+ );
+
+ if($this->getImageType() != "jpeg" && $this->transparentBackground) {
@Maks3w
Maks3w Feb 21, 2014 Member

Please add a comment explaining why jpeg is excluded here.

@Maks3w Maks3w commented on the diff Feb 21, 2014
tests/ZendTest/Barcode/Renderer/ImageTest.php
@@ -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 PNG output
@Maks3w
Maks3w Feb 21, 2014 Member

GIF output

@Maks3w Maks3w commented on the diff Feb 21, 2014
tests/ZendTest/Barcode/Renderer/ImageTest.php
+ $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 PNG output
@Maks3w
Maks3w Feb 21, 2014 Member

same here

@steverhoades
steverhoades Feb 21, 2014 Contributor

@Maks fixed per your suggestions.

On Fri, Feb 21, 2014 at 12:23 AM, Maks notifications@github.com wrote:

In tests/ZendTest/Barcode/Renderer/ImageTest.php:

  •    $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 PNG output
    

same here

Reply to this email directly or view it on GitHubhttps://github.com/zendframework/zf2/pull/5385/files#r9938990
.

@Maks3w
Member
Maks3w commented Feb 21, 2014

@steverhoades Fix the CS and ready to merge :)

@Maks3w Maks3w was assigned by steverhoades Feb 23, 2014
@Maks3w Maks3w merged commit ae011e6 into zendframework:master Feb 23, 2014

1 check passed

default The Travis CI build passed
Details
@Maks3w Maks3w added the Barcode label Feb 23, 2014
@weierophinney weierophinney pushed a commit to zendframework/zend-barcode that referenced this pull request May 14, 2015
@Maks3w Maks3w Merge pull request zendframework/zendframework#5385 4b329dd
@weierophinney weierophinney pushed a commit to zendframework/zend-barcode that referenced this pull request May 14, 2015
@Maks3w Maks3w Merge pull request zendframework/zendframework#5385 in develop 3388d6b
@weierophinney weierophinney pushed a commit to zendframework/zend-barcode that referenced this pull request May 14, 2015
@Maks3w Maks3w Merge pull request zendframework/zendframework#5385 in master 8afd3ba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment