Les scripts ci-dessous sont uniquement fonctionnels pour Gallery 1.x. Cette version est maintenant plutôt ancienne mais je n’ai pas été convaincu par les plus récentes. Cette version permet en effet d’avoir une gestion de gallery simple, sans base de données, facile à intégrer et avec les fichiers toujours accessibles.
Scripts disponibles :
Si comme moi vous mettez la description de vos photos dans le commentaire JPEG, vous devez être frustré que Gallery ne permette pas de reprendre ces commentaires pour les titres des photos. Le script ci-dessous va permettre de pallier à cela.
Nécessite exiftool du package libimage-exiftool-perl.
Exemple : php gallery_com2caption.php /home/gallery album01
#!/usr/bin/php5 <?php /* $gallery = $argv[1]; $album = $argv[2]; // "." means all */ require_once("/usr/share/gallery/classes/Image.php"); require_once("/usr/share/gallery/classes/AlbumItem.php"); require_once("/usr/share/gallery/classes/Album.php"); ini_set('memory_limit', '192M'); function processGalleryAlbum($gallery, $album, $targetdir, $name="") { $display=0; $photosfilename = $gallery . '/' . $album . '/' . 'photos.dat'; if (!file_exists($photosfilename)) { print "[ERROR] $photosfilename not found\n"; return 0; } $fd = fopen($photosfilename, "r"); $contents = fread($fd, filesize($photosfilename)); fclose($fd); $data = unserialize($contents); if (!$name) $name = $album; print "[ALBUM] $name ; $album ($photosfilename) \n"; $index = 1; foreach($data as &$albumitem) { if ($albumitem->isAlbumName) { // C'est un sous-album $filename = $gallery . '/' . $albumitem->isAlbumName . '/' . 'album.dat'; $fd = fopen($filename, "r"); $contents = fread($fd, filesize($filename)); fclose($fd); $album_data = unserialize($contents); $n = sprintf("%02d %s %s", $index, ($albumitem->isHidden()!=0)?"#":"-", html_entity_decode($album_data->fields["title"])); $n = mb_convert_encoding($n, "UTF-8"); processGalleryAlbum($gallery, $albumitem->isAlbumName, $targetdir, $n); } elseif (($albumitem->image->name)) { // C'est une image if ($albumitem->caption) $n = sprintf("%02d %s %s - %s", $index, ($albumitem->isHidden()!=0)?"#":"-", $albumitem->image->name, $albumitem->caption); else $n = sprintf("%02d - %s", $index, $albumitem->image->name); if ($display) print "[IMG] " . $album . '/' . $albumitem->image->name . "." . $albumitem->image->type . " -> " . $targetdir . "/" . $n. "\n"; $text = $albumitem->caption; if (array_key_exists('Description', $albumitem->extraFields)) if ($albumitem->extraFields['Description'] != '') $text .= "\n\n" . $albumitem->extraFields['Description']; $in = $gallery . '/' . $album . '/' . $albumitem->image->name . "." . $albumitem->image->type; unset($output); exec("exiftool -comment -s -s -s $in",$output); $comment = implode("\n",$output); //$comment = mb_convert_encoding($comment, "UTF-8"); print "[IMG] " . $albumitem->image->name . " : " . $comment . " \n"; $albumitem->caption = $comment; } $index++; } print "Save changes in $photosfilename"; copy($photosfilename,$photosfilename . ".bk"); $contents = serialize($data); $fd = fopen($photosfilename, "w"); fwrite($fd, $contents); fclose($fd); } function main($argc, $argv) { $gallery = $argv[1]; $album = $argv[2]; $targetdir = ""; //$argv[3]; if ($album == '.') { $filename = $gallery . '/' . 'albumdb.dat'; $fd = fopen($filename, "r"); $contents = fread($fd, filesize($filename)); fclose($fd); $data = unserialize($contents); foreach($data as $album) { $filename = $gallery . '/' . $album . '/' . 'album.dat'; $fd = fopen($filename, "r"); $contents = fread($fd, filesize($filename)); fclose($fd); $album_data = unserialize($contents); if (!$album_data->fields['parentAlbumName']) { processGalleryAlbum($gallery, $album, $targetdir); } } } else { processGalleryAlbum($gallery, $album, $targetdir); } } main($argc, $argv); ?>
Pour sauvegarder l’aborescence de vos albums en conservant l’ordre et les descriptions saisies dans Gallery, utilisez le script ci-dessous. Le premier argument est le répertoire de votre gallery (ex : /home/gallery), le second le nom d’un album (ex : album01), et le troisième le nom du répertoire destination (ex : /home/remi/dest)
Nécessite exiftool du package libimage-exiftool-perl.
Exemple : php gallery_album2dir.php /home/gallery album01 /home/remi/dest
#!/usr/bin/php5 <?php /* $gallery = $argv[1]; $album = $argv[2]; // "." means all $targetdir = $argv[3]; */ require_once("/usr/share/gallery/classes/Image.php"); require_once("/usr/share/gallery/classes/AlbumItem.php"); require_once("/usr/share/gallery/classes/Album.php"); ini_set('memory_limit', '192M'); function processGalleryAlbum($gallery, $album, $targetdir, $name="") { $display=0; $filename = $gallery . '/' . $album . '/' . 'photos.dat'; if (!file_exists($filename)) { print "[ERROR] $filename not found\n"; return 0; } $fd = fopen($filename, "r"); $contents = fread($fd, filesize($filename)); fclose($fd); $data = unserialize($contents); if (!$name) $name = $album; $targetdir = $targetdir . '/' . $name; if ($display) print "MKDIR $targetdir"; if (!is_dir($targetdir)) mkdir($targetdir); print "[ALBUM] $name ; $album ($filename) \n"; $index = 1; foreach($data as $albumitem) { if ($albumitem->isAlbumName) { // C'est un sous-album $filename = $gallery . '/' . $albumitem->isAlbumName . '/' . 'album.dat'; $fd = fopen($filename, "r"); $contents = fread($fd, filesize($filename)); fclose($fd); $album_data = unserialize($contents); $n = sprintf("%02d %s %s", $index, ($albumitem->isHidden()!=0)?"#":"-", html_entity_decode($album_data->fields["title"])); $n = mb_convert_encoding($n, "UTF-8"); processGalleryAlbum($gallery, $albumitem->isAlbumName, $targetdir, $n); } elseif (($albumitem->image->name)) { // C'est une image if ($albumitem->caption) $n = sprintf("%02d %s %s - %s", $index, ($albumitem->isHidden()!=0)?"#":"-", $albumitem->image->name, $albumitem->caption); else $n = sprintf("%02d - %s", $index, $albumitem->image->name); $n = strip_tags($n); $n = str_replace('"',"'", $n); $n = str_replace(':'," ", $n); $n = str_replace('/'," ", $n); $n = str_replace('*',"-", $n); $n = str_replace('?'," ", $n); $n = str_replace("\n"," ", $n); $n = str_replace("\r"," ", $n); $n = substr($n, 0, 80); $n = mb_convert_encoding($n, "UTF-8"); // "ISO-8859-15" // $n = eregi_replace ("[^[:alnum:][:space:].,!çéàèëäïöâêîôüù'-*+§_\(\)\[\]{}&²%$=<>]*", " ", sprintf("%02d - %s - %s", $index, $albumitem->image->name, $albumitem->caption)); if ($display) print "[IMG] " . $album . '/' . $albumitem->image->name . "." . $albumitem->image->type . " -> " . $targetdir . "/" . $n. "\n"; $text = $albumitem->caption; if (array_key_exists('Description', $albumitem->extraFields)) if ($albumitem->extraFields['Description'] != '') $text .= "\n\n" . $albumitem->extraFields['Description']; // Apply modification $in = $gallery . '/' . $album . '/' . $albumitem->image->name . "." . $albumitem->image->type; $out = $targetdir . "/" . $n . "." . $albumitem->image->type; // With exiftool copy($in, $out); if(($text!="") && (!preg_match('/(mov|mpg|avi|wmv)$/i',$out))) { $tmpfname = tempnam("/tmp", "gallery2jpeg"); $handle = fopen($tmpfname, "w"); fwrite($handle, $text); fclose($handle); exec("exiftool -overwrite_original -Comment\<=$tmpfname \"$out\" "); unlink($tmpfname); } // ---- } $index++; } } function main($argc, $argv) { $gallery = $argv[1]; $album = $argv[2]; $targetdir = $argv[3]; if ($album == '.') { $filename = $gallery . '/' . 'albumdb.dat'; $fd = fopen($filename, "r"); $contents = fread($fd, filesize($filename)); fclose($fd); $data = unserialize($contents); foreach($data as $album) { $filename = $gallery . '/' . $album . '/' . 'album.dat'; $fd = fopen($filename, "r"); $contents = fread($fd, filesize($filename)); fclose($fd); $album_data = unserialize($contents); if (!$album_data->fields['parentAlbumName']) { processGalleryAlbum($gallery, $album, $targetdir); } } } else { processGalleryAlbum($gallery, $album, $targetdir); } } main($argc, $argv); ?>
<?php // 2013 - Rémi Peyronnet /* To use this script : - create a destination album in your gallery, and set the name (foldername) to $conf['album_import'] - set your source path in $conf['dir_import'] - modify the location of gallery if not installed in the default path (debian) ; cf require statement - put this script in your webserver (public_html/ by example) - go in your browser and log in your gallery - go to the page of the script, and wait :) Be carefull, existing images in the album (and sub-albums) will be replaced ! */ require("/usr/share/gallery/init.php"); $conf['album_import'] = 'Import'; $conf['dir_import'] = '/warp/Work'; $albumDB = new AlbumDB(FALSE); $album_import = $albumDB->getAlbumByName($conf['album_import']); if (!$album_import) die("Import Album ${conf['album_import']} not found !"); function import_files($album, $dir) { global $albumDB; global $conf; foreach(scandir($dir) as $entry) { if (($entry == '.') || ($entry == '..')) continue; $path = $dir . DIRECTORY_SEPARATOR . $entry; if (is_dir($path)) { $entry = utf8_decode($entry); $rep = strtr($entry,'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ', 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY'); $rep = preg_replace('/[^a-zA-Z0-9_-]/s', '', $rep); if ($album->fields['name'] != $conf['album_import']) $rep = $album->fields['name'] . "_" . $rep; $subalbum = $albumDB->getAlbumByName($rep); if ($subalbum) { print "Appending existing sub-album $entry... <br />"; } else { print "Creating sub-album $entry ($rep)... "; $res = createNewAlbum($album->fields['name'], $rep, $entry, ''); print " $res <br />"; $albumDB = new AlbumDB(FALSE); $subalbum = $albumDB->getAlbumByName($rep); } import_files($subalbum, $path); } else { print "Importing $entry... "; unset($output); exec("exiftool -Description -comment -s -s -s '$path'",$output); $caption = implode("\n",$output); $caption = utf8_decode($caption); print "($caption)"; $path_parts = pathinfo($path); $last_album=$albumDB->getAlbumByName($album->fields['name']); $cur_name = $path_parts['filename']; for ($curPhoto = $last_album->numPhotos(1); $curPhoto > 0; $curPhoto--) { $photo = $last_album->getPhoto($curPhoto); if ($photo->image->name == $cur_name) { print "Deleted doublon <br />"; $last_album->deletePhoto($curPhoto); } } $last_album->save(); $last_album=$albumDB->getAlbumByName($album->fields['name']); $status = $last_album->addPhoto( $path, strtolower($path_parts['extension']), $cur_name, $caption); print $status[1]; $last_album->save(); print "<br />"; } } flush(); } import_files($album_import, $conf['dir_import']); ?>