From 509b04f612d1436a5f161239dd22e9fff4b610a6 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 15 Nov 2017 19:27:59 +0100 Subject: [PATCH] Optimize code image creation --- Model/Html/Head.php | 24 ++++++++---- Model/Html/Meta.php | 46 +++-------------------- Utils/Barcode/C128Abstract.php | 68 ++++++++++++++++++---------------- 3 files changed, 59 insertions(+), 79 deletions(-) diff --git a/Model/Html/Head.php b/Model/Html/Head.php index 0f0638299..e9c96a33b 100644 --- a/Model/Html/Head.php +++ b/Model/Html/Head.php @@ -17,7 +17,7 @@ namespace phpOMS\Model\Html; use phpOMS\Asset\AssetType; use phpOMS\Contract\RenderableInterface; - +use phpOMS\Localization\ISO639x1Enum; /** * Head class. @@ -39,7 +39,7 @@ class Head implements RenderableInterface * @var string * @since 1.0.0 */ - private $language = ''; + private $language = ISO639x1Enum::_EN; /** * Page title. @@ -166,6 +166,18 @@ class Head implements RenderableInterface $this->language = $language; } + /** + * Get page language. + * + * @return string + * + * @since 1.0.0 + */ + public function getLanguage() : string + { + return $this->language; + } + /** * Get the evaluated contents of the object. * @@ -176,11 +188,9 @@ class Head implements RenderableInterface public function render() : string { $head = ''; - if ($this->hasContent) { - $head .= $this->meta->render(); - $head .= $this->renderStyle(); - $head .= $this->renderScript(); - } + $head .= $this->meta->render(); + $head .= $this->renderStyle(); + $head .= $this->renderScript(); return $head; } diff --git a/Model/Html/Meta.php b/Model/Html/Meta.php index 204ef8e32..4d3e2e5d7 100644 --- a/Model/Html/Meta.php +++ b/Model/Html/Meta.php @@ -43,7 +43,7 @@ class Meta implements RenderableInterface * @var string * @since 1.0.0 */ - private $author = null; + private $author = ''; /** * Charset. @@ -51,7 +51,7 @@ class Meta implements RenderableInterface * @var string * @since 1.0.0 */ - private $charset = null; + private $charset = ''; /** * Description. @@ -59,15 +59,7 @@ class Meta implements RenderableInterface * @var string * @since 1.0.0 */ - private $description = null; - - /** - * Language. - * - * @var string - * @since 1.0.0 - */ - private $language = 'en'; + private $description = ''; /** * Add keyword. @@ -175,41 +167,15 @@ class Meta implements RenderableInterface $this->description = $description; } - /** - * Get language. - * - * @return string Language - * - * @since 1.0.0 - */ - public function getLanguage() : string - { - return $this->language; - } - - /** - * Set language. - * - * @param string $language Language - * - * @return void - * - * @since 1.0.0 - */ - public function setLanguage(string $language) /* : void */ - { - $this->language = $language; - } - /** * {@inheritdoc} */ public function render() : string { return (count($this->keywords) > 0 ? '"' : '') - . (isset($this->author) ? '' : '') - . (isset($this->description) ? '' : '') - . (isset($this->charset) ? '' : '') + . (!empty($this->author) ? '' : '') + . (!empty($this->description) ? '' : '') + . (!empty($this->charset) ? '' : '') . ''; } } diff --git a/Utils/Barcode/C128Abstract.php b/Utils/Barcode/C128Abstract.php index e02a65897..95c7c2d3e 100644 --- a/Utils/Barcode/C128Abstract.php +++ b/Utils/Barcode/C128Abstract.php @@ -224,7 +224,7 @@ abstract class C128Abstract { $codeString = static::$CODE_START . $this->generateCodeString() . static::$CODE_END; - return $this->createImage($codeString, 20); + return $this->createImage($codeString); } /** @@ -291,55 +291,59 @@ abstract class C128Abstract * Create barcode image * * @param string $codeString Code string to render - * @param int $codeLength Barcode length (based on $codeString) * * @return mixed * * @since 1.0.0 */ - protected function createImage(string $codeString, int $codeLength = 20) + protected function createImage(string $codeString) { - for ($i = 1; $i <= strlen($codeString); $i++) { - $codeLength = $codeLength + (int) (substr($codeString, ($i - 1), 1)); - } - - if ($this->orientation === OrientationType::HORIZONTAL) { - $imgWidth = max($codeLength, $this->dimension['width']); - $imgHeight = $this->dimension['height']; - } else { - $imgWidth = $this->dimension['width']; - $imgHeight = max($codeLength, $this->dimension['height']); - } - - $image = imagecreate($imgWidth, $imgHeight); - $black = imagecolorallocate($image, 0, 0, 0); - $white = imagecolorallocate($image, 255, 255, 255); - $location = 0; - $length = strlen($codeString); + $dimensions = $this->calculateDimensions($codeString); + $image = imagecreate($dimensions['width'], $dimensions['height']); + $black = imagecolorallocate($image, 0, 0, 0); + $white = imagecolorallocate($image, 255, 255, 255); + $location = 0; + $length = strlen($codeString); imagefill($image, 0, 0, $white); for ($position = 1; $position <= $length; $position++) { $cur_size = $location + (int) (substr($codeString, ($position - 1), 1)); if ($this->orientation === OrientationType::HORIZONTAL) { - imagefilledrectangle($image, $location + $this->margin, 0 + $this->margin, $cur_size + $this->margin, $imgHeight - $this->margin, ($position % 2 == 0 ? $white : $black)); + imagefilledrectangle($image, $location + $this->margin, 0 + $this->margin, $cur_size + $this->margin, $dimensions['height'] - $this->margin, ($position % 2 == 0 ? $white : $black)); } else { - imagefilledrectangle($image, 0 + $this->margin, $location + $this->margin, $imgWidth - $this->margin, $cur_size + $this->margin, ($position % 2 == 0 ? $white : $black)); + imagefilledrectangle($image, 0 + $this->margin, $location + $this->margin, $dimensions['width'] - $this->margin, $cur_size + $this->margin, ($position % 2 == 0 ? $white : $black)); } $location = $cur_size; } - if($location + $this->margin < $this->dimension['width']) { - if ($this->orientation === OrientationType::HORIZONTAL) { - $image = imagecrop($image, [ - 'x' => 0, 'y' => 0, - 'width' => $location + $this->margin * 2, - 'height' => $this->dimension['height']] - ); - } - } - return $image; } + + private function calculateCodeLength(string $codeString) : int + { + $codeLength = 0; + for ($i = 1; $i <= strlen($codeString); $i++) { + $codeLength = $codeLength + (int) (substr($codeString, ($i - 1), 1)); + } + + return $codeLength; + } + + private function calculateDimensions(string $codeString) : array + { + $codeLength = $this->calculateCodeLength($codeString); + $dimensions = ['width' => 0, 'height' => 0]; + + if ($this->orientation === OrientationType::HORIZONTAL) { + $dimensions['width'] = $codeLength + $this->margin * 2; + $dimensions['height'] = $this->dimension['height']; + } else { + $dimensions['width'] = $this->dimension['width']; + $dimensions['height'] = $codeLength + $this->margin; + } + + return $dimensions; + } }