Example #1
0
 /**
  * Renderer Constructor
  *
  * @param mixed $renderer           String name of renderer class, or \Zend\Config\Config object.
  * @param mixed $rendererConfig     OPTIONAL; an array or \Zend\Config\Config object with renderer parameters.
  * @return \Zend\Barcode\Renderer
  */
 public static function makeRenderer($renderer = 'image', $rendererConfig = array())
 {
     if ($renderer instanceof Renderer) {
         return $renderer;
     }
     /*
      * Convert \Zend\Config\Config argument to plain string
      * barcode name and separate config object.
      */
     if ($renderer instanceof Config) {
         if (isset($renderer->rendererParams)) {
             $rendererConfig = $renderer->rendererParams->toArray();
         }
         if (isset($renderer->renderer)) {
             $renderer = (string) $renderer->renderer;
         }
     }
     if ($rendererConfig instanceof Config) {
         $rendererConfig = $rendererConfig->toArray();
     }
     /*
      * Verify that barcode parameters are in an array.
      */
     if (!is_array($rendererConfig)) {
         $e = new Exception('Barcode parameters must be in an array or a \\Zend\\Config\\Config object');
         $e->setIsRenderable(false);
         throw $e;
     }
     /*
      * Verify that an barcode name has been specified.
      */
     if (!is_string($renderer) || empty($renderer)) {
         $e = new Exception('Renderer name must be specified in a string');
         $e->setIsRenderable(false);
         throw $e;
     }
     $rendererName = self::getPluginLoader(self::RENDERER)->load($renderer);
     /*
      * Create an instance of the barcode class.
      * Pass the config to the barcode class constructor.
      */
     $rdrAdapter = new $rendererName($rendererConfig);
     /*
      * Verify that the object created is a descendent of the abstract barcode type.
      */
     if (!$rdrAdapter instanceof Renderer) {
         $e = new Exception("Renderer class '{$rendererName}' does not implements \\Zend\\Barcode\\Renderer");
         $e->setIsRenderable(false);
         throw $e;
     }
     return $rdrAdapter;
 }
Example #2
0
 /**
  * Initialize the image resource
  *
  * @return void
  */
 protected function _initRenderer()
 {
     if (!extension_loaded('gd')) {
         $e = new Barcode\Exception('Gd extension must be loaded to render barcode as image');
         $e->setIsRenderable(false);
         throw $e;
     }
     $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 & 0xff00) >> 8, $backgroundColor & 0xff);
         $this->_imageForeColor = imagecolorallocate($this->_resource, ($foreColor & 0xff0000) >> 16, ($foreColor & 0xff00) >> 8, $foreColor & 0xff);
     } else {
         $width = $barcodeWidth;
         $height = $barcodeHeight;
         if ($this->_userWidth && $this->_barcode->getType() != 'error') {
             $width = $this->_userWidth;
         }
         if ($this->_userHeight && $this->_barcode->getType() != 'error') {
             $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 & 0xff00) >> 8, $backgroundColor & 0xff);
         $this->_imageForeColor = imagecolorallocate($this->_resource, ($foreColor & 0xff0000) >> 16, ($foreColor & 0xff00) >> 8, $foreColor & 0xff);
         $white = imagecolorallocate($this->_resource, 255, 255, 255);
         imagefilledrectangle($this->_resource, 0, 0, $width - 1, $height - 1, $white);
     }
     $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);
 }
Example #3
0
 /**
  * Renderer Constructor
  *
  * @param mixed $renderer           String name of renderer class, or \Zend\Config\Config object.
  * @param mixed $rendererConfig     OPTIONAL; an array or \Zend\Config\Config object with renderer parameters.
  * @return \Zend\Barcode\Renderer\RendererInterface
  */
 public static function makeRenderer($renderer = 'image', $rendererConfig = array())
 {
     if ($renderer instanceof Renderer\RendererInterface) {
         return $renderer;
     }
     /*
      * Convert \Zend\Config\Config argument to plain string
      * barcode name and separate config object.
      */
     if ($renderer instanceof Config\Config) {
         if (isset($renderer->rendererParams)) {
             $rendererConfig = $renderer->rendererParams->toArray();
         }
         if (isset($renderer->renderer)) {
             $renderer = (string) $renderer->renderer;
         }
     }
     if ($rendererConfig instanceof Config\Config) {
         $rendererConfig = $rendererConfig->toArray();
     }
     /*
      * Verify that barcode parameters are in an array.
      */
     if (!is_array($rendererConfig)) {
         $e = new Exception('Barcode parameters must be in an array or a \\Zend\\Config\\Config object');
         $e->setIsRenderable(false);
         throw $e;
     }
     /*
      * Verify that an barcode name has been specified.
      */
     if (!is_string($renderer) || empty($renderer)) {
         $e = new Exception('Renderer name must be specified in a string');
         $e->setIsRenderable(false);
         throw $e;
     }
     /*
      * Form full barcode class name
      */
     $rendererNamespace = '\\Zend\\Barcode\\Renderer';
     if (isset($rendererConfig['rendererNamespace'])) {
         $rendererNamespace = $rendererConfig['rendererNamespace'];
     }
     /** @todo Check if it's correct to drop case transformation */
     $rendererName = $rendererNamespace . '\\' . ucfirst($renderer);
     /*
      * Load the renderer class.
      * Important! This throws an exception if the specified class cannot be loaded.
      */
     if (!class_exists($rendererName, false)) {
         Loader::loadClass($rendererName);
     }
     /*
      * Create an instance of the barcode class.
      * Pass the config to the barcode class constructor.
      */
     $rdrAdapter = new $rendererName($rendererConfig);
     /*
      * Verify that the object created is a descendent of the abstract barcode type.
      */
     if (!$rdrAdapter instanceof Renderer\RendererInterface) {
         $e = new Exception("Renderer class '{$rendererName}' does not implements \\Zend\\Barcode\\Renderer\\RendererInterface");
         $e->setIsRenderable(false);
         throw $e;
     }
     return $rdrAdapter;
 }