-
Notifications
You must be signed in to change notification settings - Fork 1
/
QR_Generator_GD.php
85 lines (67 loc) · 2.74 KB
/
QR_Generator_GD.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php //phpcs:disable Squiz.Commenting.FunctionComment.MissingParamTag
/**
* QR_Generator_GD class file.
*
* @package Serbian Addons for WooCommerce
*/
namespace Oblak\WooCommerce\Serbian_Addons\QR;
use chillerlan\QRCode\Output\QRGdImage;
/**
* Uses GD to generate QR Codes.
*/
class QR_Generator_GD extends QRGdImage implements QR_Generator_Interface {
/**
* {@inheritDoc}
*/
public static function test( array $args ): bool {
return extension_loaded( 'gd' ) || function_exists( 'gd_info' );
}
/**
* {@inheritDoc}
*/
public static function supports_format( string $format ): bool {
$image_types = imagetypes();
return match ( mime_content_type( "a.{$format}" ) ) {
'image/jpeg' => ( IMG_JPG & $image_types ) !== 0,
'image/png' => ( IMG_PNG & $image_types ) !== 0,
'image/gif' => ( IMG_GIF & $image_types ) !== 0,
'image/webp' => ( IMG_WEBP & $image_types ) !== 0,
default => false,
};
}
/**
* {@inheritDoc}
*/
public function dump( ?string $file = null ) {
$this->options->returnResource = true;
parent::dump( $file );
if ( ! $this->options->logo ) {
$image = $this->dumpImage();
$this->saveToFile( $image, $file );
return $image;
}
$image = imagecreatefromstring( file_get_contents( $this->options->logo ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$w = imagesx( $image );
$h = imagesy( $image );
$lw = ( ( $this->options->logoSpaceWidth - 2 ) * $this->options->scale );
$lh = ( ( $this->options->logoSpaceHeight - 2 ) * $this->options->scale );
$ql = ( $this->matrix->getSize() * $this->options->scale );
imagecopyresampled( $this->image, $image, ( ( $ql - $lw ) / 2 ), ( ( $ql - $lh ) / 2 ), 0, 0, $lw, $lh, $w, $h );
$image = $this->dumpImage();
$this->saveToFile( $image, $file );
return $image;
}
/**
* {@inheritDoc}
*/
protected function renderImage(): void {
match ( $this->options->format ) {
'bmp' => imagebmp( $this->image, null, ( $this->options->quality > 0 ) ),
'gif' => imagegif( $this->image ),
'jpg', 'jpeg' => imagejpeg( $this->image, null, max( -1, min( 100, $this->options->quality ) ) ),
'webp' => imagewebp( $this->image, null, max( 0, min( 100, $this->options->quality ) ) ),
'png' => imagepng( $this->image, null, max( 0, min( 9, (int) round( $this->options->quality / 10 ) ) ) ),
default => imagepng( $this->image, null, max( 0, min( 9, (int) round( $this->options->quality / 10 ) ) ) ),
};
}
}