:octocat: introduce QROutputAbstract::getOutputDimensions() for more consistent control over the image size

This commit is contained in:
smiley
2023-09-17 19:19:24 +02:00
parent 527b67ab7a
commit 522fa7c31b
5 changed files with 61 additions and 22 deletions
+3 -2
View File
@@ -102,6 +102,7 @@ class QREps extends QROutputAbstract{
* @inheritDoc
*/
public function dump(string $file = null):string{
[$width, $height] = $this->getOutputDimensions();
$eps = [
// main header
@@ -111,7 +112,7 @@ class QREps extends QROutputAbstract{
sprintf('%%%%CreationDate: %1$s', date('c')),
'%%DocumentData: Clean7Bit',
'%%LanguageLevel: 3',
sprintf('%%%%BoundingBox: 0 0 %1$s %1$s', $this->length),
sprintf('%%%%BoundingBox: 0 0 %s %s', $width, $height),
'%%EndComments',
// function definitions
'%%BeginProlog',
@@ -123,7 +124,7 @@ class QREps extends QROutputAbstract{
if($this::moduleValueIsValid($this->options->bgColor)){
$eps[] = $this->prepareModuleValue($this->options->bgColor);
$eps[] = sprintf('0 0 %1$s %1$s F', $this->length);
$eps[] = sprintf('0 0 %s %s F', $width, $height);
}
// create the path elements
+5 -3
View File
@@ -108,7 +108,7 @@ class QRFpdf extends QROutputAbstract{
* Initializes an FPDF instance
*/
protected function initFPDF():FPDF{
return new FPDF('P', $this->options->fpdfMeasureUnit, [$this->length, $this->length]);
return new FPDF('P', $this->options->fpdfMeasureUnit, $this->getOutputDimensions());
}
/**
@@ -121,10 +121,12 @@ class QRFpdf extends QROutputAbstract{
$this->fpdf->AddPage();
if($this::moduleValueIsValid($this->options->bgColor)){
$bgColor = $this->prepareModuleValue($this->options->bgColor);
$bgColor = $this->prepareModuleValue($this->options->bgColor);
[$width, $height] = $this->getOutputDimensions();
/** @phan-suppress-next-line PhanParamTooFewUnpack */
$this->fpdf->SetFillColor(...$bgColor);
$this->fpdf->Rect(0, 0, $this->length, $this->length, 'F');
$this->fpdf->Rect(0, 0, $width, $height, 'F');
}
$this->prevColor = null;
+21 -15
View File
@@ -39,7 +39,7 @@ class QRImagick extends QROutputAbstract{
/**
* The allocated background color
*/
protected ImagickPixel $background;
protected ImagickPixel $backgroundColor;
/**
* @inheritDoc
@@ -115,15 +115,9 @@ class QRImagick extends QROutputAbstract{
* @return string|\Imagick
*/
public function dump(string $file = null){
$this->imagick = new Imagick;
$this->setBgColor();
$this->imagick->newImage($this->length, $this->length, $this->background, $this->options->imagickFormat);
if($this->options->quality > -1){
$this->imagick->setImageCompressionQuality(max(0, min(100, $this->options->quality)));
}
$this->imagick = $this->createImage();
$this->drawImage();
// set transparency color after all operations
@@ -151,17 +145,29 @@ class QRImagick extends QROutputAbstract{
*/
protected function setBgColor():void{
if(isset($this->background)){
return;
}
if($this::moduleValueIsValid($this->options->bgColor)){
$this->background = $this->prepareModuleValue($this->options->bgColor);
$this->backgroundColor = $this->prepareModuleValue($this->options->bgColor);
return;
}
$this->background = $this->prepareModuleValue('white');
$this->backgroundColor = $this->prepareModuleValue('white');
}
/**
* Creates a new Imagick instance
*/
protected function createImage():Imagick{
$imagick = new Imagick;
[$width, $height] = $this->getOutputDimensions();
$imagick->newImage($width, $height, $this->backgroundColor, $this->options->imagickFormat);
if($this->options->quality > -1){
$imagick->setImageCompressionQuality(max(0, min(100, $this->options->quality)));
}
return $imagick;
}
/**
@@ -173,7 +179,7 @@ class QRImagick extends QROutputAbstract{
return;
}
$transparencyColor = $this->background;
$transparencyColor = $this->backgroundColor;
if($this::moduleValueIsValid($this->options->transparencyColor)){
$transparencyColor = $this->prepareModuleValue($this->options->transparencyColor);
+23 -2
View File
@@ -17,7 +17,7 @@ use function array_chunk, implode, is_string, preg_match, sprintf, trim;
* SVG output
*
* @see https://github.com/codemasher/php-qrcode/pull/5
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg
* @see https://developer.mozilla.org/en-US/docs/Web/SVG
* @see https://www.sarasoueidan.com/demos/interactive-svg-coordinate-system/
* @see http://apex.infogridpacific.com/SVG/svg-tutorial-contents.html
*/
@@ -46,6 +46,13 @@ class QRMarkupSVG extends QRMarkup{
return parent::moduleValueIsValid($value);
}
/**
* @inheritDoc
*/
protected function getOutputDimensions():array{
return [$this->moduleCount, $this->moduleCount];
}
/**
* @inheritDoc
*/
@@ -69,15 +76,29 @@ class QRMarkupSVG extends QRMarkup{
return $svg;
}
/**
* returns the value for the SVG viewBox attribute
*
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
* @see https://css-tricks.com/scale-svg/#article-header-id-3
*/
protected function getViewBox():string{
[$width, $height] = $this->getOutputDimensions();
return sprintf('0 0 %s %s', ($this->options->svgViewBoxSize ?? $width), ($this->options->svgViewBoxSize ?? $height));
}
/**
* returns the <svg> header with the given options parsed
*
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg
*/
protected function header():string{
$header = sprintf(
'<svg xmlns="http://www.w3.org/2000/svg" class="qr-svg %1$s" viewBox="%2$s" preserveAspectRatio="%3$s">%4$s',
$this->options->cssClass,
($this->options->svgViewBoxSize ?? $this->moduleCount),
$this->getViewBox(),
$this->options->svgPreserveAspectRatio,
$this->options->eol
);
+9
View File
@@ -80,6 +80,15 @@ abstract class QROutputAbstract implements QROutputInterface{
$this->length = ($this->moduleCount * $this->scale);
}
/**
* Returns a 2 element array with the current output width and height
*
* The type and units of the values depend on the output class. The default value is the current module count * scale.
*/
protected function getOutputDimensions():array{
return [$this->length, $this->length];
}
/**
* Sets the initial module values
*/