1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* VizHash_GD |
4
|
|
|
* |
5
|
|
|
* Visual Hash implementation in php4+GD, |
6
|
|
|
* stripped down and modified version for PrivateBin |
7
|
|
|
* |
8
|
|
|
* @link https://sebsauvage.net/wiki/doku.php?id=php:vizhash_gd |
9
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net) |
10
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License |
11
|
|
|
* @version 0.0.5 beta PrivateBin 1.7.1 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace PrivateBin; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Vizhash16x16 |
18
|
|
|
* |
19
|
|
|
* Example: |
20
|
|
|
* $vz = new Vizhash16x16(); |
21
|
|
|
* $data = $vz->generate(sha512('hello')); |
22
|
|
|
* header('Content-type: image/png'); |
23
|
|
|
* echo $data; |
24
|
|
|
* exit; |
25
|
|
|
*/ |
26
|
|
|
class Vizhash16x16 |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* hash values |
30
|
|
|
* |
31
|
|
|
* @access private |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $VALUES; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* index of current value |
38
|
|
|
* |
39
|
|
|
* @access private |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
private $VALUES_INDEX; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* image width |
46
|
|
|
* |
47
|
|
|
* @access private |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
private $width; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* image height |
54
|
|
|
* |
55
|
|
|
* @access private |
56
|
|
|
* @var int |
57
|
|
|
*/ |
58
|
|
|
private $height; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* constructor |
62
|
|
|
* |
63
|
|
|
* @access public |
64
|
|
|
*/ |
65
|
2 |
|
public function __construct() |
66
|
|
|
{ |
67
|
2 |
|
$this->width = 16; |
68
|
2 |
|
$this->height = 16; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Generate a 16x16 png corresponding to $text. |
73
|
|
|
* |
74
|
|
|
* The given text should to be 128 to 150 characters long |
75
|
|
|
* |
76
|
|
|
* @access public |
77
|
|
|
* @param string $text |
78
|
|
|
* @return string PNG data. Or empty string if GD is not available. |
79
|
|
|
*/ |
80
|
2 |
|
public function generate($text) |
81
|
|
|
{ |
82
|
2 |
|
if (!function_exists('gd_info')) { |
83
|
|
|
return ''; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
$textlen = strlen($text); |
87
|
|
|
|
88
|
|
|
// We convert the hash into an array of integers. |
89
|
2 |
|
$this->VALUES = array(); |
90
|
2 |
|
for ($i = 0; $i < $textlen; $i = $i + 2) { |
91
|
2 |
|
array_push($this->VALUES, hexdec(substr($text, $i, 2))); |
92
|
|
|
} |
93
|
2 |
|
$this->VALUES_INDEX = 0; // to walk the array. |
94
|
|
|
|
95
|
|
|
// Then use these integers to drive the creation of an image. |
96
|
2 |
|
$image = imagecreatetruecolor($this->width, $this->height); |
97
|
|
|
|
98
|
2 |
|
$r = $r0 = $this->getInt(); |
99
|
2 |
|
$g = $g0 = $this->getInt(); |
100
|
2 |
|
$b = $b0 = $this->getInt(); |
101
|
|
|
|
102
|
|
|
// First, create an image with a specific gradient background. |
103
|
2 |
|
$op = 'v'; |
104
|
2 |
|
if (($this->getInt() % 2) == 0) { |
105
|
1 |
|
$op = 'h'; |
106
|
|
|
} |
107
|
2 |
|
$image = $this->degrade($image, $op, array($r0, $g0, $b0), array(0, 0, 0)); |
|
|
|
|
108
|
|
|
|
109
|
2 |
|
for ($i = 0; $i < 7; ++$i) { |
110
|
2 |
|
$action = $this->getInt(); |
111
|
2 |
|
$color = imagecolorallocate($image, $r, $g, $b); |
112
|
2 |
|
$r = $r0 = ($r0 + $this->getInt() / 25) % 256; |
113
|
2 |
|
$g = $g0 = ($g0 + $this->getInt() / 25) % 256; |
114
|
2 |
|
$b = $b0 = ($b0 + $this->getInt() / 25) % 256; |
115
|
2 |
|
$this->drawshape($image, $action, $color); |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
$color = imagecolorallocate($image, $this->getInt(), $this->getInt(), $this->getInt()); |
119
|
2 |
|
$this->drawshape($image, $this->getInt(), $color); |
120
|
2 |
|
ob_start(); |
121
|
2 |
|
imagepng($image); |
122
|
2 |
|
$imagedata = ob_get_contents(); |
123
|
2 |
|
ob_end_clean(); |
124
|
2 |
|
imagedestroy($image); |
125
|
|
|
|
126
|
2 |
|
return $imagedata; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns a single integer from the $VALUES array (0...255) |
131
|
|
|
* |
132
|
|
|
* @access private |
133
|
|
|
* @return int |
134
|
|
|
*/ |
135
|
2 |
|
private function getInt() |
136
|
|
|
{ |
137
|
2 |
|
$v = $this->VALUES[$this->VALUES_INDEX]; |
138
|
2 |
|
++$this->VALUES_INDEX; |
139
|
2 |
|
$this->VALUES_INDEX %= count($this->VALUES); // Warp around the array |
140
|
2 |
|
return $v; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns a single integer from the array (roughly mapped to image width) |
145
|
|
|
* |
146
|
|
|
* @access private |
147
|
|
|
* @return int |
148
|
|
|
*/ |
149
|
2 |
|
private function getX() |
150
|
|
|
{ |
151
|
2 |
|
return $this->width * $this->getInt() / 256; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns a single integer from the array (roughly mapped to image height) |
156
|
|
|
* |
157
|
|
|
* @access private |
158
|
|
|
* @return int |
159
|
|
|
*/ |
160
|
2 |
|
private function getY() |
161
|
|
|
{ |
162
|
2 |
|
return $this->height * $this->getInt() / 256; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Gradient function |
167
|
|
|
* |
168
|
|
|
* taken from: |
169
|
|
|
* @link https://www.supportduweb.com/scripts_tutoriaux-code-source-41-gd-faire-un-degrade-en-php-gd-fonction-degrade-imagerie.html |
170
|
|
|
* |
171
|
|
|
* @access private |
172
|
|
|
* @param resource $img |
173
|
|
|
* @param string $direction |
174
|
|
|
* @param array $color1 |
175
|
|
|
* @param array $color2 |
176
|
|
|
* @return resource |
177
|
|
|
*/ |
178
|
2 |
|
private function degrade($img, $direction, $color1, $color2) |
179
|
|
|
{ |
180
|
2 |
|
if ($direction == 'h') { |
181
|
1 |
|
$size = imagesx($img); |
182
|
1 |
|
$sizeinv = imagesy($img); |
183
|
|
|
} else { |
184
|
2 |
|
$size = imagesy($img); |
185
|
2 |
|
$sizeinv = imagesx($img); |
186
|
|
|
} |
187
|
2 |
|
$diffs = array( |
188
|
2 |
|
($color2[0] - $color1[0]) / $size, |
189
|
2 |
|
($color2[1] - $color1[1]) / $size, |
190
|
2 |
|
($color2[2] - $color1[2]) / $size, |
191
|
2 |
|
); |
192
|
2 |
|
for ($i = 0; $i < $size; ++$i) { |
193
|
2 |
|
$r = $color1[0] + ($diffs[0] * $i); |
194
|
2 |
|
$g = $color1[1] + ($diffs[1] * $i); |
195
|
2 |
|
$b = $color1[2] + ($diffs[2] * $i); |
196
|
2 |
|
if ($direction == 'h') { |
197
|
1 |
|
imageline($img, $i, 0, $i, $sizeinv, imagecolorallocate($img, $r, $g, $b)); |
198
|
|
|
} else { |
199
|
2 |
|
imageline($img, 0, $i, $sizeinv, $i, imagecolorallocate($img, $r, $g, $b)); |
200
|
|
|
} |
201
|
|
|
} |
202
|
2 |
|
return $img; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Draw a shape |
207
|
|
|
* |
208
|
|
|
* @access private |
209
|
|
|
* @param resource $image |
210
|
|
|
* @param int $action |
211
|
|
|
* @param int $color |
212
|
|
|
*/ |
213
|
2 |
|
private function drawshape($image, $action, $color) |
214
|
|
|
{ |
215
|
2 |
|
switch ($action % 7) { |
216
|
2 |
|
case 0: |
217
|
1 |
|
imagefilledrectangle($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color); |
218
|
1 |
|
break; |
219
|
2 |
|
case 1: |
220
|
2 |
|
case 2: |
221
|
2 |
|
imagefilledellipse($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $color); |
222
|
2 |
|
break; |
223
|
2 |
|
case 3: |
224
|
1 |
|
$points = array($this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY(), $this->getX(), $this->getY()); |
225
|
1 |
|
imagefilledpolygon($image, $points, 4, $color); |
226
|
1 |
|
break; |
227
|
|
|
default: |
228
|
2 |
|
$start = $this->getInt() * 360 / 256; |
229
|
2 |
|
$end = $start + $this->getInt() * 180 / 256; |
230
|
2 |
|
imagefilledarc($image, $this->getX(), $this->getY(), $this->getX(), $this->getY(), $start, $end, $color, IMG_ARC_PIE); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|