Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
GridBackground | |
0.00% |
0 / 46 |
|
0.00% |
0 / 3 |
240 | |
0.00% |
0 / 1 |
render | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
createPattern | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
renderGrid | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
132 |
1 | <?php |
2 | |
3 | /** |
4 | * Grid cover background layer |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2018. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Cover_Generator |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development:plugins:hierarchy_components Wiki |
28 | */ |
29 | |
30 | namespace VuFind\Cover\Layer; |
31 | |
32 | use function strlen; |
33 | |
34 | /** |
35 | * Grid cover background layer |
36 | * |
37 | * @category VuFind |
38 | * @package Cover_Generator |
39 | * @author Demian Katz <demian.katz@villanova.edu> |
40 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
41 | * @link https://vufind.org/wiki/development:plugins:hierarchy_components Wiki |
42 | */ |
43 | class GridBackground extends AbstractBackgroundLayer |
44 | { |
45 | /** |
46 | * Render the layer |
47 | * |
48 | * @param resource $im Image resource to draw on |
49 | * @param array $details Cover details array (with title/author/call_number) |
50 | * @param object $settings Settings object |
51 | * |
52 | * @return void |
53 | */ |
54 | public function render($im, $details, $settings) |
55 | { |
56 | // Generate a grid of colors as primary feature |
57 | $seed = $this->createSeed($details['title'], $details['callnumber']); |
58 | $pattern = $this->createPattern($seed); |
59 | $accentColor = $this->getAccentColor($im, $seed, $settings); |
60 | $this->renderGrid($im, $pattern, $accentColor, $settings); |
61 | } |
62 | |
63 | /** |
64 | * Turn number into pattern |
65 | * |
66 | * @param int $seed Seed used to generate the pattern |
67 | * |
68 | * @return string binary string describing a quarter of the pattern |
69 | */ |
70 | protected function createPattern($seed) |
71 | { |
72 | // Convert to binary |
73 | $bc = decbin($seed); |
74 | // If we have less that a half of a quarter |
75 | if (strlen($bc) < 8) { |
76 | // Rotate square of the first 4 into a 4x2 |
77 | // Simulate matrix rotation on string |
78 | $bc = substr($bc, 0, 3) |
79 | . substr($bc, 0, 1) |
80 | . substr($bc, 2, 2) |
81 | . substr($bc, 3, 1) |
82 | . substr($bc, 1, 1); |
83 | } |
84 | // If we have less than a quarter |
85 | if (strlen($bc) < 16) { |
86 | // Rotate the first 8 as a 4x2 into a 4x4 |
87 | $bc .= strrev($bc); |
88 | } |
89 | return $bc; |
90 | } |
91 | |
92 | /** |
93 | * Convert 16 long binary string to 8x8 color grid |
94 | * Reflects vertically and horizontally |
95 | * |
96 | * @param resource $im Active image resource |
97 | * @param string $pattern Binary string of pattern |
98 | * @param int $color Fill color |
99 | * @param object $settings Generator settings object |
100 | * |
101 | * @return void |
102 | */ |
103 | protected function renderGrid($im, $pattern, $color, $settings) |
104 | { |
105 | imagefilledrectangle( |
106 | $im, |
107 | 0, |
108 | 0, |
109 | $settings->width, |
110 | $settings->height, |
111 | $this->getColor($im, $settings->baseColor) |
112 | ); |
113 | $halfWidth = (int)($settings->width / 2); |
114 | $halfHeight = (int)($settings->height / 2); |
115 | $boxWidth = (int)($settings->width / 8); |
116 | $boxHeight = (int)($settings->height / 8); |
117 | |
118 | $bc = str_split($pattern); |
119 | for ($k = 0; $k < 4; $k++) { |
120 | $x = $k % 2 ? $halfWidth : $halfWidth - $boxWidth; |
121 | $y = $k / 2 < 1 ? $halfHeight : $halfHeight - $boxHeight; |
122 | $u = $k % 2 ? $boxWidth : -$boxWidth; |
123 | $v = $k / 2 < 1 ? $boxHeight : -$boxHeight; |
124 | for ($i = 0; $i < 16; $i++) { |
125 | if ($bc[$i] == '1') { |
126 | imagefilledrectangle( |
127 | $im, |
128 | $x, |
129 | $y, |
130 | $x + $boxWidth - 1, |
131 | $y + $boxHeight - 1, |
132 | $color |
133 | ); |
134 | } |
135 | $x += $u; |
136 | if ($x >= $settings->width || $x < 0) { |
137 | $x = $k % 2 ? $halfWidth : $halfWidth - $boxWidth; |
138 | $y += $v; |
139 | } |
140 | } |
141 | } |
142 | } |
143 | } |