1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\AssetsOverview\Api; |
4
|
|
|
|
5
|
|
|
class CompareImages |
6
|
|
|
{ |
7
|
|
|
public function compare($a, $b) |
8
|
|
|
{ |
9
|
|
|
// main function. returns the hammering distance of two images' bit value |
10
|
|
|
$i1 = $this->createImage($a); |
11
|
|
|
$i2 = $this->createImage($b); |
12
|
|
|
|
13
|
|
|
if (! $i1 || ! $i2) { |
14
|
|
|
return false; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
$i1 = $this->resizeImage($i1, $a); |
18
|
|
|
$i2 = $this->resizeImage($i2, $b); |
19
|
|
|
|
20
|
|
|
imagefilter($i1, IMG_FILTER_GRAYSCALE); |
21
|
|
|
imagefilter($i2, IMG_FILTER_GRAYSCALE); |
22
|
|
|
|
23
|
|
|
$colorMean1 = $this->colorMeanValue($i1); |
24
|
|
|
$colorMean2 = $this->colorMeanValue($i2); |
25
|
|
|
|
26
|
|
|
$bits1 = $this->bits($colorMean1); |
27
|
|
|
$bits2 = $this->bits($colorMean2); |
28
|
|
|
|
29
|
|
|
$hammeringDistance = 0; |
30
|
|
|
|
31
|
|
|
for ($a = 0; $a < 64; ++$a) { |
32
|
|
|
if ($bits1[$a] !== $bits2[$a]) { |
33
|
|
|
++$hammeringDistance; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $hammeringDistance; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function mimeType($i) |
41
|
|
|
{ |
42
|
|
|
// returns array with mime type and if its jpg or png. Returns false if it isn't jpg or png |
43
|
|
|
$mime = getimagesize($i); |
44
|
|
|
$return = [$mime[0], $mime[1]]; |
45
|
|
|
|
46
|
|
|
switch ($mime['mime']) { |
47
|
|
|
case 'image/jpeg': |
48
|
|
|
$return[] = 'jpg'; |
49
|
|
|
|
50
|
|
|
return $return; |
51
|
|
|
case 'image/png': |
52
|
|
|
$return[] = 'png'; |
53
|
|
|
|
54
|
|
|
return $return; |
55
|
|
|
default: |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function createImage($i) |
61
|
|
|
{ |
62
|
|
|
// retuns image resource or false if its not jpg or png |
63
|
|
|
$mime = $this->mimeType($i); |
64
|
|
|
|
65
|
|
|
if ('jpg' === $mime[2]) { |
66
|
|
|
return imagecreatefromjpeg($i); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ('png' === $mime[2]) { |
70
|
|
|
return imagecreatefrompng($i); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function resizeImage($i, $source) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
// resizes the image to a 8x8 squere and returns as image resource |
79
|
|
|
$mime = $this->mimeType($source); |
80
|
|
|
|
81
|
|
|
$t = imagecreatetruecolor(8, 8); |
82
|
|
|
|
83
|
|
|
$source = $this->createImage($source); |
84
|
|
|
|
85
|
|
|
imagecopyresized($t, $source, 0, 0, 0, 0, 8, 8, $mime[0], $mime[1]); |
|
|
|
|
86
|
|
|
|
87
|
|
|
return $t; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function colorMeanValue($i) |
91
|
|
|
{ |
92
|
|
|
// returns the mean value of the colors and the list of all pixel's colors |
93
|
|
|
$colorList = []; |
94
|
|
|
$colorSum = 0; |
95
|
|
|
for ($a = 0; $a < 8; ++$a) { |
96
|
|
|
for ($b = 0; $b < 8; ++$b) { |
97
|
|
|
$rgb = imagecolorat($i, $a, $b); |
98
|
|
|
$colorList[] = $rgb & 0xFF; |
99
|
|
|
$colorSum += $rgb & 0xFF; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return [$colorSum / 64, $colorList]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function bits($colorMean) |
107
|
|
|
{ |
108
|
|
|
// returns an array with 1 and zeros. If a color is bigger than the mean value of colors it is 1 |
109
|
|
|
$bits = []; |
110
|
|
|
|
111
|
|
|
foreach ($colorMean[1] as $color) { |
112
|
|
|
$bits[] = $color >= $colorMean[0] ? 1 : 0; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $bits; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.