/
check_similar.php
98 lines (79 loc) · 2.31 KB
/
check_similar.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
<?php
$scale= return_scale( 'foo','bar'); //Enter your both file names here.
echo $scale; //For troubleshooting purpose :)
function test ($filename)
{
$filename = convertImage($filename, uniqid().'.jpg');
list($width, $height) = getimagesize($filename);
$img = imagecreatefromjpeg($filename);
$new_img = imagecreatetruecolor(8, 8);
imagecopyresampled($new_img, $img, 0, 0, 0, 0, 8, 8, $width, $height);
imagefilter($new_img, IMG_FILTER_GRAYSCALE);
$colors = array();
$sum = 0;
for ($i = 0; $i < 8; $i++) {
for ($j = 0; $j < 8; $j++) {
$color = imagecolorat($new_img, $i, $j) & 0xff;
$sum += $color;
$colors[] = $color;
}
}
$avg = $sum / 64;
$hash = '';
$curr = '';
$count = 0;
foreach ($colors as $color) {
if ($color > $avg) {
$curr .= '1';
} else {
$curr .= '0';
}
$count++;
if (!($count % 4)) {
$hash .= dechex(bindec($curr));
$curr = '';
}
}
unlink($filename);
return $hash;
}
function convertImage($originalImage, $outputImage, $quality=100)
{
$myfile = fopen($outputImage, "w");
// jpg, png, gif or bmp?
$exploded = explode('.',$originalImage);
$ext = $exploded[count($exploded) - 1];
if (preg_match('/jpg|jpeg/i',$ext))
$imageTmp=imagecreatefromjpeg($originalImage);
else if (preg_match('/png/i',$ext))
$imageTmp=imagecreatefrompng($originalImage);
else if (preg_match('/gif/i',$ext))
$imageTmp=imagecreatefromgif($originalImage);
else if (preg_match('/bmp/i',$ext))
$imageTmp=imagecreatefrombmp($originalImage);
else
return 0;
imagejpeg($imageTmp, $outputImage, $quality);
imagedestroy($imageTmp);
return $outputImage;
}
function return_scale($filename1, $filename2)
{
$hash1=hash_file('md5', $filename1);
$hash2=hash_file('md5', $filename2);
if( strcmp($hash1,$hash2) == 0 )
return 100;
else
{
$hash1 = test($filename1);
$hash2 = test($filename2);
$diff=0;
for($i=0 ; $i<16 ; $i++) //Hash value length is 16
{
if( $hash1[$i] != $hash2[$i])
$diff++;
}
return ( (16-$diff) /16)*100;
}
}
?>