:octocat: add mime type guesser to QROutputAbstract (#223)

This commit is contained in:
smiley
2024-07-21 18:04:45 +02:00
parent b42d02dcb7
commit 071bc4217d
4 changed files with 39 additions and 23 deletions
+1
View File
@@ -52,6 +52,7 @@
"chillerlan/php-settings-container": "^3.2.1"
},
"require-dev": {
"ext-fileinfo": "*",
"chillerlan/php-authenticator": "^5.2.1",
"intervention/image": "^3.7",
"phpbench/phpbench": "^1.2.15",
+1 -1
View File
@@ -56,7 +56,7 @@ class QRImagickWithLogo extends QRImagick{
$this->saveToFile($imageData, $file);
if($this->options->outputBase64){
$imageData = $this->toBase64DataURI($imageData, $this->guessMimeType($imageData));
$imageData = $this->toBase64DataURI($imageData);
}
return $imageData;
+2 -19
View File
@@ -16,9 +16,8 @@ namespace chillerlan\QRCode\Output;
use chillerlan\QRCode\QROptions;
use chillerlan\QRCode\Data\QRMatrix;
use chillerlan\Settings\SettingsContainerInterface;
use finfo, Imagick, ImagickDraw, ImagickPixel;
use Imagick, ImagickDraw, ImagickPixel;
use function extension_loaded, in_array, is_string, max, min, preg_match, sprintf, strlen;
use const FILEINFO_MIME_TYPE;
/**
* ImageMagick output module (requires ext-imagick)
@@ -123,28 +122,12 @@ class QRImagick extends QROutputAbstract{
$this->saveToFile($imageData, $file);
if($this->options->outputBase64){
$imageData = $this->toBase64DataURI($imageData, $this->guessMimeType($imageData));
$imageData = $this->toBase64DataURI($imageData);
}
return $imageData;
}
/**
* @todo: move to QROutputAbstract
*
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
*/
protected function guessMimeType(string $imageData):string{
$mime = (new finfo(FILEINFO_MIME_TYPE))->buffer($imageData);
if($mime === false){
throw new QRCodeOutputException('unable to detect mime type');
}
return $mime;
}
/**
* Sets the background color
*/
+35 -3
View File
@@ -6,6 +6,8 @@
* @author Smiley <smiley@chillerlan.net>
* @copyright 2015 Smiley
* @license MIT
*
* @noinspection PhpComposerExtensionStubsInspection
*/
declare(strict_types=1);
@@ -14,8 +16,9 @@ namespace chillerlan\QRCode\Output;
use chillerlan\QRCode\QROptions;
use chillerlan\QRCode\Data\QRMatrix;
use chillerlan\Settings\SettingsContainerInterface;
use Closure;
use function base64_encode, dirname, file_put_contents, is_writable, ksort, sprintf;
use Closure, finfo;
use function base64_encode, dirname, extension_loaded, file_put_contents, is_writable, ksort, sprintf;
use const FILEINFO_MIME_TYPE;
/**
* common output abstract
@@ -191,9 +194,38 @@ abstract class QROutputAbstract implements QROutputInterface{
/**
* Returns a base64 data URI for the given string and mime type
*
* The mime type can be set via class constant MIME_TYPE in child classes,
* or given via $mime, otherwise it is guessed from the image $data.
*/
protected function toBase64DataURI(string $data, string|null $mime = null):string{
return sprintf('data:%s;base64,%s', ($mime ?? static::MIME_TYPE), base64_encode($data));
$mime ??= static::MIME_TYPE;
if($mime === ''){
$mime = $this->guessMimeType($data);
}
return sprintf('data:%s;base64,%s', $mime, base64_encode($data));
}
/**
* Guesses the mime type from the given $imageData
*
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
*/
protected function guessMimeType(string $imageData):string{
if(!extension_loaded('fileinfo')){
throw new QRCodeOutputException('ext-fileinfo not loaded, cannot guess mime type');
}
$mime = (new finfo(FILEINFO_MIME_TYPE))->buffer($imageData);
if($mime === false){
throw new QRCodeOutputException('unable to detect mime type');
}
return $mime;
}
/**