Optimize code image creation

This commit is contained in:
Dennis Eichhorn 2017-11-15 19:27:59 +01:00
parent 73d2780d48
commit 509b04f612
3 changed files with 59 additions and 79 deletions

View File

@ -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;
}

View File

@ -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 ? '<meta name="keywords" content="' . implode(',', $this->keywords) . '">"' : '')
. (isset($this->author) ? '<meta name="author" content="' . $this->author . '">' : '')
. (isset($this->description) ? '<meta name="description" content="' . $this->description . '">' : '')
. (isset($this->charset) ? '<meta charset="' . $this->charset . '">' : '')
. (!empty($this->author) ? '<meta name="author" content="' . $this->author . '">' : '')
. (!empty($this->description) ? '<meta name="description" content="' . $this->description . '">' : '')
. (!empty($this->charset) ? '<meta charset="' . $this->charset . '">' : '')
. '<meta name="generator" content="Orange Management">';
}
}

View File

@ -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;
}
}