#!/usr/bin/php5 '_', '.' => '')); $str = strtr($str, "ÀÁÂÃÄÅàáâãäåÇçÒÓÔÕÖØòóôõöøÈÉÊËèéêëÌÍÎÏìíîïÙÚÛÜùúûü¾ÝÿýÑñ", "AAAAAAaaaaaaCcOOOOOOooooooEEEEeeeeIIIIiiiiUUUUuuuuYYyyNn"); $str = preg_replace('/[^a-zA-Z0-9_-]/', '', $str); return $str; } function renameImage($file, $path) { if (!preg_match('!^([0-9]+)[ _.-].*\.jpe?g$!', $file, $match)) return false; $id = trim($match[1]); if (!preg_match('!^([0-9]+)_[a-f0-9]+.*\.jpe?g$!', $file) && !FORCE_NON_FLICKR) { echo "Will not treat {$file} because it's not a native flickr photo.\n" . "If {$id} is a real flickr ID, you can try to use the --force argument.\n"; return false; } $url = 'http://flickr.com/photo.gne?id='.$id; $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"Accept-language: en\r\n" . "User-Agent: Opera/9.62 (X11; Linux i686; U; fr) Presto/2.1.1\r\n" ) ); $context = stream_context_create($opts); $content = @file_get_contents($url, false, $context); if (!$content) die("Can't connect to $url\n"); $title = false; $author = false; $copy = false; foreach ($http_response_header as $header) { if (preg_match("!location: /photos/(.+)/".preg_quote($id, '!')."/!iU", $header, $match)) { $author = $match[1]; break; } } if (preg_match('!!iU', $content, $match)) { $title = html_entity_decode($match[1]); } if (preg_match('!http://creativecommons\.org/licenses/(.+)/2\.0/!iU', $content, $match)) { $copy = 'CC-' . $match[1]; } else { $copy = 'Copyright'; } if (!$author || !$title || !$copy) { echo "Can't find informations for {$url} " ."(that's probably a private image or is subject to safesearch)\n"; return false; } $new_name = strtr(RENAME_PATTERN, array( '%TITLE' => cleanName($title), '%ID' => $id, '%LICENSE' => $copy, '%AUTHOR' => cleanName($author), ) ); rename($path . '/' . $file, $path . '/' . $new_name); echo "$file -> $new_name\n"; return true; } $path = false; foreach ($_SERVER['argv'] as $k => $v) { if ($k < 1) continue; if ($v == '-f' || $v == '--force') define('FORCE_NON_FLICKR', true); else $path = $v; } if (!defined('FORCE_NON_FLICKR')) define('FORCE_NON_FLICKR', false); if (!$path) { $path = $_SERVER['PWD']; } else { $path = realpath($path); } $path = preg_replace('!/+$!', '', $path); if (!file_exists($path)) die("$path does not exists\n"); if (is_dir($path)) { $dir = dir($path); while ($file = $dir->read()) { if ($file[0] == '.') continue; renameImage($file, $path); } $dir->close(); } elseif (is_file($path)) { if (!renameImage(basename($path), dirname($path))) { die("Can't process $path (is it a flickr photo ?)\n"); } } ?>