Passed
Push — master ( 514bca...891b04 )
by smiley
02:57
created

qrcode.php ➔ send_response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @filesource   qrcode.php
4
 * @created      18.11.2017
5
 * @author       Smiley <[email protected]>
6
 * @copyright    2017 Smiley
7
 * @license      MIT
8
 */
9
10
namespace chillerlan\QRCodePublic;
11
12
use chillerlan\QRCode\QRCode;
13
use chillerlan\QRCode\QROptions;
14
15
require_once '../vendor/autoload.php';
16
17
try{
18
19
	$moduleValues = [
20
		// finder
21
		1536 => $_POST['m_finder_dark'],
22
		6    => $_POST['m_finder_light'],
23
		// alignment
24
		2560 => $_POST['m_alignment_dark'],
25
		10   => $_POST['m_alignment_light'],
26
		// timing
27
		3072 => $_POST['m_timing_dark'],
28
		12   => $_POST['m_timing_light'],
29
		// format
30
		3584 => $_POST['m_format_dark'],
31
		14   => $_POST['m_format_light'],
32
		// version
33
		4096 => $_POST['m_version_dark'],
34
		16   => $_POST['m_version_light'],
35
		// data
36
		1024 => $_POST['m_data_dark'],
37
		4    => $_POST['m_data_light'],
38
		// darkmodule
39
		512  => $_POST['m_darkmodule_dark'],
40
		// separator
41
		8    => $_POST['m_separator_light'],
42
		// quietzone
43
		18   => $_POST['m_quietzone_light'],
44
	];
45
46
	$moduleValues = array_map(function($v){
47
		if(preg_match('/[a-f\d]{6}/i', $v) === 1){
48
			return in_array($_POST['output_type'], ['png', 'jpg', 'gif'])
49
				? array_map('hexdec', str_split($v, 2))
50
				: '#'.$v ;
51
		}
52
		return null;
53
	}, $moduleValues);
54
55
56
	$ecc = in_array($_POST['ecc'], ['L', 'M', 'Q', 'H'], true) ? $_POST['ecc'] : 'L';
57
58
	$qro = new QROptions;
59
60
	$qro->version          = (int)$_POST['version'];
61
	$qro->eccLevel         = constant('chillerlan\\QRCode\\QRCode::ECC_'.$ecc);
62
	$qro->maskPattern      = (int)$_POST['maskpattern'];
63
	$qro->addQuietzone     = isset($_POST['quietzone']);
64
	$qro->quietzoneSize    = (int)$_POST['quietzonesize'];
65
	$qro->moduleValues     = $moduleValues;
66
	$qro->outputType       = $_POST['output_type'];
67
	$qro->scale            = (int)$_POST['scale'];
68
	$qro->imageTransparent = false;
69
70
	$qrcode = (new QRCode($qro))->render($_POST['inputstring']);
71
72
	if(in_array($_POST['output_type'], ['png', 'jpg', 'gif'])){
73
		$qrcode = '<img src="'.$qrcode.'" />';
74
	}
75
	elseif($_POST['output_type'] === 'text'){
76
		$qrcode = '<pre style="font-size: 75%; line-height: 1;">'.$qrcode.'</pre>';
77
	}
78
	elseif($_POST['output_type'] === 'json'){
79
		$qrcode = '<pre style="font-size: 75%; overflow-x: auto;">'.$qrcode.'</pre>';
80
	}
81
82
	send_response(['qrcode' => $qrcode]);
83
}
84
// Pokémon exception handler
85
catch(\Exception $e){
86
	header('HTTP/1.1 500 Internal Server Error');
87
	send_response(['error' => $e->getMessage()]);
88
}
89
90
/**
91
 * @param array $response
92
 */
93
function send_response(array $response){
94
	header('Content-type: application/json;charset=utf-8;');
95
	echo json_encode($response);
96
	exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
97
}
98