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
|
<html><!-- sample image search engine, part of the libpuzzle package -->
<head>
</head>
<body>
<h1>Similar images finder using <a href="http://libpuzzle.pureftpd.org">libpuzzle</a></h1>
<?php
error_reporting(E_ALL);
require_once 'config.inc.php';
require_once 'similar.inc.php';
function display_form() {
echo '<form action="' . htmlspecialchars($_SERVER['REQUEST_URI']) . '" ' .
'method="POST">' . "\n";
echo 'Enter an image URL (http only):' . "\n";
echo '<input type="text" size="100" value="" autocomplete="off" name="url" />' . "\n";
echo '<input type="submit" />';
echo '</form>' . "\n";
}
function display_error($err) {
echo '<div id="err"><strong>' . htmlspecialchars($err) . '</strong></div>' . "\n";
}
function display_loading() {
echo '<div id="loading">Loading...</div>' . "\n";
@ob_flush(); flush();
}
function display_loaded() {
echo '<div id="loaded">Loaded.</div>' . "\n";
@ob_flush(); flush();
}
function display_signature_ok() {
echo '<div id="sig-ok">Signature computed.</div>' . "\n";
@ob_flush(); flush();
}
function remove_tmpfile($file) {
@unlink($file);
}
function get_client_info() {
return @$_SERVER['REMOTE_ADDR'] . '/' . time();
}
function display_similar_pictures($urls) {
echo '<div id="images">' . "\n";
foreach ($urls as $url) {
echo '<a href="' . htmlentities($url) . '" ' .
'onclick="window.open(this.href); return false;">';
echo ' <img src="' . htmlentities($url) . '" alt="" />';
echo '</a>' . "\n";
}
echo '</div>' . "\n";
}
function record_url($url, &$md5, &$cvec) {
if (function_exists('sys_get_temp_dir')) {
$tmpdir = sys_get_temp_dir();
} else {
$tmpdir = '/tmp';
}
$dfn = tempnam($tmpdir, 'similar-' . md5(uniqid(mt_rand(), TRUE)));
register_shutdown_function('remove_tmpfile', $dfn);
if (($dfp = fopen($dfn, 'w')) == FALSE) {
display_form();
display_error('Unable to create the temporary file');
return FALSE;
}
if (($fp = fopen($url, 'r')) == FALSE) {
display_form();
display_error('Unable to open: [' . $url . ']');
return FALSE;
}
$f = fread($fp, 4096);
$written = strlen($f);
if (empty($f)) {
display_form();
display_error('Unable to load: [' . $url . ']');
return FALSE;
}
fwrite($dfp, $f);
$infos = @getimagesize($dfn);
if (empty($infos) ||
($infos[2] !== IMAGETYPE_GIF && $infos[2] !== IMAGETYPE_JPEG &&
$infos[2] !== IMAGETYPE_PNG) ||
$infos[0] < 50 || $infos[1] < 50) {
fclose($dfp);
display_form();
display_error('Unsupported image format');
return FALSE;
}
fseek($dfp, strlen($f));
while (!feof($fp)) {
$max = MAX_IMAGE_SIZE - $written;
if ($max > 65536) {
$max = 65536;
}
$t = fread($fp, $max);
fwrite($dfp, $t);
$written += strlen($t);
if ($written > MAX_IMAGE_SIZE) {
fclose($dfp);
display_form();
display_error('File too large');
return FALSE;
}
}
unset($t);
fclose($dfp);
display_loaded();
$md5 = @md5_file($dfn);
if (empty($md5)) {
display_form();
display_error('Unable to get the MD5 of the file');
return FALSE;
}
$cvec = puzzle_fill_cvec_from_file($dfn);
if (empty($cvec)) {
display_form();
display_error('Unable to compute image signature');
return FALSE;
}
display_signature_ok();
save_signature($url, get_client_info(), $md5, $cvec);
return TRUE;
}
$url = trim(@$_POST['url']);
if (empty($url)) {
display_form();
exit(0);
}
if (strlen($url) > MAX_URL_SIZE ||
preg_match('^http://([a-z0-9-]+[.])+[a-z]{2,}/.i', $url) <= 0) {
display_form();
display_error('Invalid URL, must be http://...');
exit(1);
}
display_loading();
$md5 = FALSE;
$cvec = FALSE;
if (record_url($url, $md5, $cvec) !== TRUE) {
exit(1);
}
$urls = find_similar_pictures($md5, $cvec);
unset($cvec);
display_form();
display_similar_pictures($urls);
?>
</body>
</html>
|