Skip to content

Commit 071bc42

Browse files
committed
:octocat: add mime type guesser to QROutputAbstract (#223)
1 parent b42d02d commit 071bc42

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"chillerlan/php-settings-container": "^3.2.1"
5353
},
5454
"require-dev": {
55+
"ext-fileinfo": "*",
5556
"chillerlan/php-authenticator": "^5.2.1",
5657
"intervention/image": "^3.7",
5758
"phpbench/phpbench": "^1.2.15",

examples/imagickWithLogo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function dump(string|null $file = null):string{
5656
$this->saveToFile($imageData, $file);
5757

5858
if($this->options->outputBase64){
59-
$imageData = $this->toBase64DataURI($imageData, $this->guessMimeType($imageData));
59+
$imageData = $this->toBase64DataURI($imageData);
6060
}
6161

6262
return $imageData;

src/Output/QRImagick.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
use chillerlan\QRCode\QROptions;
1717
use chillerlan\QRCode\Data\QRMatrix;
1818
use chillerlan\Settings\SettingsContainerInterface;
19-
use finfo, Imagick, ImagickDraw, ImagickPixel;
19+
use Imagick, ImagickDraw, ImagickPixel;
2020
use function extension_loaded, in_array, is_string, max, min, preg_match, sprintf, strlen;
21-
use const FILEINFO_MIME_TYPE;
2221

2322
/**
2423
* ImageMagick output module (requires ext-imagick)
@@ -123,28 +122,12 @@ public function dump(string|null $file = null):string|Imagick{
123122
$this->saveToFile($imageData, $file);
124123

125124
if($this->options->outputBase64){
126-
127-
$imageData = $this->toBase64DataURI($imageData, $this->guessMimeType($imageData));
125+
$imageData = $this->toBase64DataURI($imageData);
128126
}
129127

130128
return $imageData;
131129
}
132130

133-
/**
134-
* @todo: move to QROutputAbstract
135-
*
136-
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
137-
*/
138-
protected function guessMimeType(string $imageData):string{
139-
$mime = (new finfo(FILEINFO_MIME_TYPE))->buffer($imageData);
140-
141-
if($mime === false){
142-
throw new QRCodeOutputException('unable to detect mime type');
143-
}
144-
145-
return $mime;
146-
}
147-
148131
/**
149132
* Sets the background color
150133
*/

src/Output/QROutputAbstract.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* @author Smiley <[email protected]>
77
* @copyright 2015 Smiley
88
* @license MIT
9+
*
10+
* @noinspection PhpComposerExtensionStubsInspection
911
*/
1012
declare(strict_types=1);
1113

@@ -14,8 +16,9 @@
1416
use chillerlan\QRCode\QROptions;
1517
use chillerlan\QRCode\Data\QRMatrix;
1618
use chillerlan\Settings\SettingsContainerInterface;
17-
use Closure;
18-
use function base64_encode, dirname, file_put_contents, is_writable, ksort, sprintf;
19+
use Closure, finfo;
20+
use function base64_encode, dirname, extension_loaded, file_put_contents, is_writable, ksort, sprintf;
21+
use const FILEINFO_MIME_TYPE;
1922

2023
/**
2124
* common output abstract
@@ -191,9 +194,38 @@ protected function getModuleValueAt(int $x, int $y):mixed{
191194

192195
/**
193196
* Returns a base64 data URI for the given string and mime type
197+
*
198+
* The mime type can be set via class constant MIME_TYPE in child classes,
199+
* or given via $mime, otherwise it is guessed from the image $data.
194200
*/
195201
protected function toBase64DataURI(string $data, string|null $mime = null):string{
196-
return sprintf('data:%s;base64,%s', ($mime ?? static::MIME_TYPE), base64_encode($data));
202+
$mime ??= static::MIME_TYPE;
203+
204+
if($mime === ''){
205+
$mime = $this->guessMimeType($data);
206+
}
207+
208+
return sprintf('data:%s;base64,%s', $mime, base64_encode($data));
209+
}
210+
211+
/**
212+
* Guesses the mime type from the given $imageData
213+
*
214+
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
215+
*/
216+
protected function guessMimeType(string $imageData):string{
217+
218+
if(!extension_loaded('fileinfo')){
219+
throw new QRCodeOutputException('ext-fileinfo not loaded, cannot guess mime type');
220+
}
221+
222+
$mime = (new finfo(FILEINFO_MIME_TYPE))->buffer($imageData);
223+
224+
if($mime === false){
225+
throw new QRCodeOutputException('unable to detect mime type');
226+
}
227+
228+
return $mime;
197229
}
198230

199231
/**

0 commit comments

Comments
 (0)