protected function track_similar() { if (!function_exists('puzzle_fill_cvec_from_file') || !function_exists('puzzle_vector_normalized_distance') || !function_exists('puzzle_compress_cvec') || !function_exists('puzzle_uncompress_cvec')) { return; } $unparsed = $this->db->limit(100)->get_vector('art', array('id', 'md5', 'ext'), 'vector = ""'); foreach ($unparsed as $id => $image) { $file = IMAGES . SL . 'art' . SL . $image['md5'] . '.' . $image['ext']; $vector = puzzle_fill_cvec_from_file($file); $vector = base64_encode(puzzle_compress_cvec($vector)); $this->db->update('art', array('similar_tested' => 0, 'vector' => $vector), $id); } $all = $this->db->get_vector('art', array('id', 'vector'), 'vector != ""'); $arts = $this->db->limit(100)->get_vector('art', array('id', 'vector'), 'vector != "" and similar_tested = 0'); foreach ($all as $id => $vector) { $all[$id] = puzzle_uncompress_cvec(base64_decode($vector)); } foreach ($arts as $id => $vector) { $vector = puzzle_uncompress_cvec(base64_decode($vector)); foreach ($all as $compare_id => $compare_vector) { if ($id != $compare_id && puzzle_vector_normalized_distance($vector, $compare_vector) < 0.3) { $id_first = min($id, $compare_id); $id_second = max($id, $compare_id); $this->db->insert('art_similar', array('id_first' => $id_first, 'id_second' => $id_second)); } } $this->db->update('art', array('similar_tested' => 1), $id); } }
public function insert() { if (!Check::is_hash($this->get('md5')) || !Check::is_hash($this->get('thumb')) || !$this->get('extension') || Database::get_count('art', 'md5 = ?', $this->get('md5'))) { return $this; } if ($this->get('resized') == 1) { $this->calculate_resize(); } $this->set('pretty_date', Transform_Text::rudate()); $this->set('sortdate', ceil(microtime(true) * 1000)); if (!$this->get('area')) { $this->set('area', def::area(1)); } $this->correct_tags(); parent::insert(); if (function_exists('puzzle_fill_cvec_from_file') && function_exists('puzzle_compress_cvec')) { $imagelink = IMAGES . SL . 'booru' . SL . 'thumbs' . SL . 'large_' . $this->get('thumb') . '.jpg'; $vector = puzzle_fill_cvec_from_file($imagelink); $vector = base64_encode(puzzle_compress_cvec($vector)); Database::insert('art_similar', array('id' => $this->get_id(), 'vector' => $vector)); } return $this; }
/** * Get an image signature from an arbitrary file. Currently used when * searching for faces that appear in a user-supplied image. * * @param integer $filename Image filename to check * * @return binary vector signature */ public function getSignatureFromFile($filename) { if ($GLOBALS['conf']['faces']['search'] == 0 || Horde_Util::loadExtension('libpuzzle') === false) { return ''; } return puzzle_fill_cvec_from_file($filename); }
protected function get_upload_data($key) { $md5 = substr($key, 0, 32); $id = substr($key, 32); if (!isset(self::$upload_cache[$id])) { self::$upload_cache[$id] = $this->db->get_full_row('art_upload', $id); } $data = self::$upload_cache[$id]; if (empty($data) || $data['md5'] != $md5) { throw new ErrorApi('Неверный ключ загрузки', ErrorApi::INCORRECT_INPUT); } $exist = $this->db->get_field('art', 'id', 'md5 = ?', $md5); if ($exist) { throw new ErrorApi($exist, ErrorUpload::ALREADY_EXISTS); } unset($data['id'], $data['date'], $data['name']); if (function_exists('puzzle_fill_cvec_from_file') && function_exists('puzzle_compress_cvec')) { $imagelink = $this->get_images_path() . 'art' . SL . $md5 . '_largethumb.jpg'; $vector = puzzle_fill_cvec_from_file($imagelink); $vector = base64_encode(puzzle_compress_cvec($vector)); $data['vector'] = $vector; } return $data; }
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; }
function is_dublicates() { $arts = explode(',', query::$get['data']); if ( count($arts) < 2 || !function_exists('puzzle_fill_cvec_from_file') || !function_exists('puzzle_vector_normalized_distance') ) { return false; } foreach ($arts as $key => $art) { $file = ROOT_DIR.SL.'images'.SL.'booru'.SL.'thumbs'.SL.'large_'.$art.'.jpg'; $arts[$key] = puzzle_fill_cvec_from_file($file); } foreach ($arts as $key => $art) { foreach ($arts as $key2 => $compare_art) { if ( $key != $key2 && puzzle_vector_normalized_distance($art, $compare_zart) > 0.3 ) { return false; } } } return count($arts); }