-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphash.php
316 lines (264 loc) · 8.55 KB
/
phash.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
# based on links, code & comments from http://www.hackerfactor.com/blog/?/archives/529-Kind-of-Like-That.html
# requires php7
# requires php GD library, install: sudo apt install php-gd;sudo phpenmod gd;
# requires php-gmp, to install php-gmp in ubuntu 20: sudo apt install php-gmp;sudo phpenmod gmp;
# Status: dHash1 is working
# TODO: make test-able methods public & a test program
class NS_pHash
{
private static $_dctConst = null;
private static function getDctConst():array
{ # pre-cache DCT coefficients
if(self::$_dctConst){ return self::$_dctConst;}
self::$_dctConst = array();
for ($dctP=0; $dctP<8; $dctP++)
{
for ($p=0;$p<32;$p++)
{
self::$_dctConst[$dctP][$p] = cos( ((2*$p + 1)/64) * $dctP * M_PI );
}
}
return self::$_dctConst;
}
# returns 64-bit dHash
public static function dHash64($imagePath)
{
$img = self::get_resampled_gray($imagePath, 9, 8);
if(!$img){ return NULL; }
$grays = array();
for ($y=0; $y<8; $y++)
{
for ($x=0; $x<9; $x++)
{
$grays[$y][$x] = imagecolorat($img, $x, $y) & 0xFF;
}
}
imagedestroy($img);
# diffs:
$bits = array();
for ($y=0; $y<8; $y++)
{
for ($x=0; $x<8; $x++)
{
$bits[] = ($grays[$y][$x] < $grays[$y][$x+1]) ? '1' : '0';
}
}
return gmp_strval( gmp_init(implode($bits),2) ); # 64-bit unsigned number as string, ready for database insert query
}
# returns 16-bit dHash
public static function dHash16($imagePath)
{
$img = self::get_resampled_gray($imagePath, 6, 4); # 6x4 = 24
if(!$img){ return NULL; }
$grays = array();
for ($y=0; $y<4; $y++)
{
for ($x=0; $x<6; $x++)
{
$grays[$y][$x] = imagecolorat($img, $x, $y) & 0xFF;
}
}
imagedestroy($img);
# calc diffs in x-direction
$bits = [];
for ($y=0; $y<4; $y++)
{
for ($x=0; $x<5; $x++)
{
$bits[] = ($grays[$y][$x] < $grays[$y][$x+1]) ? '1' : '0';
}
}
# remove corner positions [0][0], [0][4], [3][0], [3][4]
unset($bits[0]); unset($bits[4]);
unset($bits[15]);unset($bits[19]);
return gmp_strval( gmp_init(implode($bits),2) ); # 64-bit unsigned number as string, ready for database insert query
}
private static function get_resampled_gray($ffname, $twid, $thei, $onlyJPG=TRUE)
{
try
{
$thumb = exif_thumbnail($ffname, $swid, $shei, $stype);
if (!$thumb)
{
$src = ($onlyJPG) ? imagecreatefromjpeg($ffname) : imagecreatefromstring(file_get_contents($ffname));
list($swid, $shei) = array(imagesx($src), imagesy($src));
} else
{
$src = imagecreatefromstring($thumb);
unset($thumb);
}
// resize to 32x32
$img = imagecreatetruecolor($twid, $thei);
imagecopyresampled($img, $src, 0, 0, 0, 0, $twid, $thei, $swid, $shei);
imagedestroy($src);
imagefilter($img, IMG_FILTER_GRAYSCALE);
}
catch(Exception $e)
{ echo(__FILE__.'~72: Caught exception: '.$e->getMessage());
return NULL;
}
return $img;
}
# perceptual hash1, slow DCT
public static function pHash1($ffname):string
{
// Resize the image.
$img = self::get_resampled_gray($ffname,32,32);
$matrix = [];
$row = [];
$rows = [];
$col = [];
for ($y = 0; $y < 32; $y++)
{
for ($x = 0; $x < 32; $x++)
{
$row[$x] = imagecolorat($img, $x, $y) & 0xFF;
# $row[$x] = (int) floor(($rgb[0] * 0.299) + ($rgb[1] * 0.587) + ($rgb[2] * 0.114));
}
$rows[$y] = self::calculateDCT1($row);
}
for ($x = 0; $x < 32; $x++)
{
for ($y = 0; $y < 32; $y++)
{
$col[$y] = $rows[$y][$x];
}
$matrix[$x] = self::calculateDCT1($col);
}
// Extract the top 8x8 pixels.
$pixels = [];
for ($y = 0; $y < 8; $y++)
{
for ($x = 0; $x < 8; $x++)
{
$pixels[] = $matrix[$y][$x];
}
}
// Method === MEDIAN)
//{
$compare = self::median($pixels);
//} else
// {
# $compare = self::average($pixels);
// }
// Calculate hash.
$bits = [];
foreach ($pixels as $pixel)
{
$bits[] = ($pixel > $compare) ? '1' : '0';
}
// have array of chars '0' or '1'
return gmp_strval( gmp_init(implode($bits),2) ); # 64-bit unsigned number as string, ready for database insert query
}
# Perform a 1 dimension Discrete Cosine Transformation.
protected static function calculateDCT1(array $matrix): array
{
$transformed = [];
$size = count($matrix);
for ($i = 0; $i < $size; $i++)
{
$sum = 0;
for ($j = 0; $j < $size; $j++)
{
$sum += $matrix[$j] * cos($i * pi() * ($j + 0.5) / $size);
}
$sum *= sqrt(2 / $size);
if ($i === 0)
{
$sum *= 1 / sqrt(2);
}
$transformed[$i] = $sum;
}
return $transformed;
}
# Get the median of the values.
# The median compared to the mean (average) is that it is reflects the "typical value" of the set
# based on the count of all values
protected static function median(array $pixels): float
{
sort($pixels, SORT_NUMERIC);
if (count($pixels) % 2 === 0)
{
return ($pixels[count($pixels) / 2 - 1] + $pixels[count($pixels) / 2]) / 2;
}
return $pixels[(int) floor(count($pixels) / 2)];
}
# Get the average of the values without the first one.
protected static function average(array $pixels): float
{
// Calculate the average value from top 8x8 pixels, except for the first one.
$n = count($pixels) - 1;
return array_sum( array_slice($pixels, 1, $n) ) / $n;
}
# Average Hash
public static function aHash($ffname)
{
$img = self::get_resampled_gray($ffname,8,8);
if(!$img){ return null; }
$graySum = 0;
$grays = array();
for ($y=0; $y<8; $y++)
{
for ($x=0; $x<8; $x++)
{
$gray = imagecolorat($img, $x, $y) & 0xFF;
$grays[] = $gray;
$graySum += $gray;
}
}
imagedestroy($img);
$average = $graySum/64;
foreach ($grays as $i => $gray)
{
$grays[$i] = ($gray>=$average) ? '1' : '0';
}
return gmp_strval( gmp_init(implode($grays),2) ); # 64-bit unsigned number as string, ready for database insert query
}
public static function pHash($ffname)
{
$img = self::get_resampled_gray($ffname,32,32);
if(!$img){ return null; }
$grays = array();
for ($y=0; $y<32; $y++)
{
for ($x=0; $x<32; $x++)
{
$grays[$y][$x] = imagecolorat($img, $x, $y) & 0xFF;
}
}
imagedestroy($img);
// DCT 8*8
$dctConst = self::getDctConst();
$dctSum = 0;
$dcts = array();
for ($dctY=0; $dctY<8; $dctY++)
{
for ($dctX=0; $dctX<8; $dctX++)
{
$sum = 1;
for ($y=0;$y<32;$y++)
{
for ($x=0;$x<32;$x++)
{
$sum += $dctConst[$dctY][$y] * $dctConst[$dctX][$x] * $grays[$y][$x];
}
}
// apply coefficients
$sum = $sum * 0.25;
if ($dctY == 0 || $dctX == 0)
{ # exclude term[0][0]
$sum = $sum * M_SQRT1_2; # M_SQRT1_2 == 1/sqrt(2);
}
$dcts[] = $sum;
$dctSum += $sum;
}
}
$average = $dctSum/64;
foreach ($dcts as $i => $dct)
{
$dcts[$i] = ($dct>=$average) ? '1' : '0';
}
return gmp_strval( gmp_init(implode($dcts),2) ); # 64-bit unsigned number as string, ready for database insert query
}
} # end class