-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_duplicate.module
147 lines (123 loc) · 4.2 KB
/
image_duplicate.module
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
<?php
include_once 'inc/phash/phash.inc';
include_once 'inc/surf/surf.inc';
include_once 'inc/surf/fast_hessian.inc';
include_once 'inc/surf/integral_image.inc';
include_once 'inc/surf/response_layer.inc';
include_once 'inc/surf/gaussian_constants.inc';
include_once 'inc/surf/surf_interest_point.inc';
include_once 'inc/kmeans/kmeans.inc';
function image_duplicate_block_info() {
$blocks = array();
$blocks['image_duplicate_search'] = array(
'info' => t('Image duplicate Search'),
);
return $blocks;
}
function image_duplicate_file_insert($file) {
$image = image_load($file->uri);
if ($image) {
$type = substr($image->info['mime_type'], strpos($image->info['mime_type'], "/") + 1);
$url = file_create_url($file->uri);
$image_gd = call_user_func('imagecreatefrom' . $type, $url);
$board = new Surf($image);
$points = $board->getFreeOrientedInterestPoints();
// Print points
foreach ($points as $point) {
imageellipse($image_gd, $point['x'], $point['y'], 5, 5, 0);
}
call_user_func_array('image' . $type, array($image_gd, 'points.' . $image->info['extension']));
$kmeans = new KMeans();
$kmeans->setData($points)
->setXKey('x')
->setYKey('y')
->setClusterCount(10)
->solve();
$clusters = $kmeans->getClusters();
$phash = array();
$phash[] = array(
'fid' => $file->fid,
'full' => 1,
'phash' => phash_dct_imagehash($image->resource, $image->info['extension'], $image->info['width'], $image->info['height'])
);
foreach ($clusters as $cluster) {
$data = $cluster->getData();
if (!empty($data)) {
$bounds = $cluster->getBounds();
$width = $bounds['maxX'] - $bounds['minX'];
$height = $bounds['maxY'] - $bounds['minY'];
$image_part = phash_create_tmp($image->resource, $image->info['extension'], $width, $height);
imagecopy($image_part, $image->resource, 0, 0, $bounds['minX'], $bounds['minY'], $width, $height);
$phash[] = array(
'fid' => $file->fid,
'full' => 0,
'phash' => phash_dct_imagehash($image_part, $image->info['extension'], $image->info['width'], $image->info['height'])
);
// Add cluster
imagerectangle($image_gd, $bounds['minX'], $bounds['minY'], $bounds['maxX'], $bounds['maxY'], 0);
}
}
// Print clusters
call_user_func_array('image' . $type, array($image_gd, 'clusters.' . $image->info['extension']));
$query = db_insert('image_duplicate')->fields(array('fid', 'full', 'phash'));
foreach ($phash as $record) {
$query->values($record);
}
$query->execute();
}
}
function image_duplicate_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'image_duplicate_search':
$block['subject'] = '';
$block['content'] = drupal_get_form('image_duplicate_serach_form');
break;
}
return $block;
}
function image_duplicate_serach_form() {
$form = array();
$form['image'] = array(
'#title' => t('Image'),
'#type' => 'managed_file',
'#description' => 'Upload an image and click "Search"',
'#upload_location' => 'public://',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
);
return $form;
}
function image_duplicate_serach_form_submit($form, &$form_state) {
if (isset($form_state['values']['image'])) {
$patterns = db_select('image_duplicate', 'i')
->fields('i')
->condition('fid', $form_state['values']['image'], '=')
->execute()
->fetchAll();
$haystack = db_select('image_duplicate', 'i')
->fields('i')
->condition('fid', $form_state['values']['image'], '!=')
->execute()
->fetchAll();
foreach ($patterns as $pattern) {
$pattern->phash = hex2bin($pattern->phash);
}
$candidate_list = array();
foreach ($haystack as $candidate) {
$candidate->phash = hex2bin($candidate->phash);
foreach ($patterns as $pattern) {
$diff = phash_hamming_distance();
if ($diff <= 10) {
$candidate_list[] = $candidate;
}
}
}
// Make file permanent
$file = file_load($form_state['values']['image']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
}
}