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 :

  • gallery_com2caption.php : initialise le titre des photos dans Gallery à partir du commentaire JPEG
  • gallery_album2dir.php : sauvegarde les albums Gallery dans une arborescence (avec images nommées et commentées)
  • gallery-import.php : importe automatiquement une arborescence complète dans Gallery

Initialiser le titre des photos dans Gallery à partir du commentaire JPE

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

gallery_com2caption.php

#!/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");
 
<a href="http://www.php.net/ini_set">ini_set</a>('memory_limit', '192M');
 
function processGalleryAlbum($gallery, $album, $targetdir, $name="")
{
    $display=0;
 
    $photosfilename = $gallery . '/' . $album . '/' . 'photos.dat'; 
    if (!<a href="http://www.php.net/file_exists">file_exists</a>($photosfilename)) { print "[ERROR] $photosfilename not foundn"; return 0; }
    $fd = <a href="http://www.php.net/fopen">fopen</a>($photosfilename, "r");
    $contents = <a href="http://www.php.net/fread">fread</a>($fd, <a href="http://www.php.net/filesize">filesize</a>($photosfilename));
    <a href="http://www.php.net/fclose">fclose</a>($fd);
    $data = <a href="http://www.php.net/unserialize">unserialize</a>($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 = <a href="http://www.php.net/fopen">fopen</a>($filename, "r");
        $contents = <a href="http://www.php.net/fread">fread</a>($fd, <a href="http://www.php.net/filesize">filesize</a>($filename));
        <a href="http://www.php.net/fclose">fclose</a>($fd);
        $album_data = <a href="http://www.php.net/unserialize">unserialize</a>($contents);
        $n = <a href="http://www.php.net/sprintf">sprintf</a>("%02d %s %s", $index,  ($albumitem->isHidden()!=0)?"#":"-", <a href="http://www.php.net/html_entity_decode">html_entity_decode</a>($album_data->fields["title"]));
	$n = <a href="http://www.php.net/mb_convert_encoding">mb_convert_encoding</a>($n, "UTF-8"); 
        processGalleryAlbum($gallery, $albumitem->isAlbumName, $targetdir, $n);
     }
     elseif (($albumitem->image->name))
     {
        // C'est une image
        if ($albumitem->caption) $n = <a href="http://www.php.net/sprintf">sprintf</a>("%02d %s %s - %s", $index,  ($albumitem->isHidden()!=0)?"#":"-", $albumitem->image->name, $albumitem->caption);
         else $n = <a href="http://www.php.net/sprintf">sprintf</a>("%02d - %s", $index, $albumitem->image->name);
        if ($display) print "[IMG] " . $album . '/' . $albumitem->image->name . "." . $albumitem->image->type . " -> " . $targetdir . "/" . $n.  "n";
        $text = $albumitem->caption;
        if (<a href="http://www.php.net/array_key_exists">array_key_exists</a>('Description', $albumitem->extraFields))
		if ($albumitem->extraFields['Description'] != '') 
			$text .= "nn" . $albumitem->extraFields['Description'];
 
        $in = $gallery . '/' . $album . '/' . $albumitem->image->name . "." . $albumitem->image->type;
	<a href="http://www.php.net/unset">unset</a>($output);
        <a href="http://www.php.net/exec">exec</a>("exiftool -comment -s -s -s $in",$output);
	$comment = <a href="http://www.php.net/implode">implode</a>("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";
    <a href="http://www.php.net/copy">copy</a>($photosfilename,$photosfilename . ".bk");
    $contents = <a href="http://www.php.net/serialize">serialize</a>($data);
    $fd = <a href="http://www.php.net/fopen">fopen</a>($photosfilename, "w");
    <a href="http://www.php.net/fwrite">fwrite</a>($fd, $contents);
    <a href="http://www.php.net/fclose">fclose</a>($fd);
 
}
 
function main($argc, $argv)
{
    $gallery = $argv[1];
    $album = $argv[2];
    $targetdir = ""; //$argv[3];
    if ($album == '.')
    {
        $filename = $gallery . '/' . 'albumdb.dat'; 
        $fd = <a href="http://www.php.net/fopen">fopen</a>($filename, "r");
	$contents = <a href="http://www.php.net/fread">fread</a>($fd, <a href="http://www.php.net/filesize">filesize</a>($filename));
	<a href="http://www.php.net/fclose">fclose</a>($fd);
	$data = <a href="http://www.php.net/unserialize">unserialize</a>($contents);
	foreach($data as $album)
	{
		$filename = $gallery . '/' . $album . '/' . 'album.dat'; 
		$fd = <a href="http://www.php.net/fopen">fopen</a>($filename, "r");
		$contents = <a href="http://www.php.net/fread">fread</a>($fd, <a href="http://www.php.net/filesize">filesize</a>($filename));
		<a href="http://www.php.net/fclose">fclose</a>($fd);
		$album_data = <a href="http://www.php.net/unserialize">unserialize</a>($contents);
		if (!$album_data->fields['parentAlbumName'])
		{
			processGalleryAlbum($gallery, $album, $targetdir); 
		}
	}
 
    }
    else
    {
     processGalleryAlbum($gallery, $album, $targetdir);
     }
}
 
main($argc, $argv);
 
?>

Sauvegarder l’arborescence de vos albums

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

gallery_album2dir.php

#!/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");
 
<a href="http://www.php.net/ini_set">ini_set</a>('memory_limit', '192M');
 
function processGalleryAlbum($gallery, $album, $targetdir, $name="")
{
    $display=0;
 
    $filename = $gallery . '/' . $album . '/' . 'photos.dat'; 
    if (!<a href="http://www.php.net/file_exists">file_exists</a>($filename)) { print "[ERROR] $filename not foundn"; return 0; }
    $fd = <a href="http://www.php.net/fopen">fopen</a>($filename, "r");
    $contents = <a href="http://www.php.net/fread">fread</a>($fd, <a href="http://www.php.net/filesize">filesize</a>($filename));
    <a href="http://www.php.net/fclose">fclose</a>($fd);
    $data = <a href="http://www.php.net/unserialize">unserialize</a>($contents);
 
    if (!$name) $name = $album;
 
    $targetdir = $targetdir . '/' . $name;
    if ($display) print "MKDIR $targetdir";
    if (!<a href="http://www.php.net/is_dir">is_dir</a>($targetdir)) <a href="http://www.php.net/mkdir">mkdir</a>($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 = <a href="http://www.php.net/fopen">fopen</a>($filename, "r");
        $contents = <a href="http://www.php.net/fread">fread</a>($fd, <a href="http://www.php.net/filesize">filesize</a>($filename));
        <a href="http://www.php.net/fclose">fclose</a>($fd);
        $album_data = <a href="http://www.php.net/unserialize">unserialize</a>($contents);
        $n = <a href="http://www.php.net/sprintf">sprintf</a>("%02d %s %s", $index,  ($albumitem->isHidden()!=0)?"#":"-", <a href="http://www.php.net/html_entity_decode">html_entity_decode</a>($album_data->fields["title"]));
	$n = <a href="http://www.php.net/mb_convert_encoding">mb_convert_encoding</a>($n, "UTF-8"); 
        processGalleryAlbum($gallery, $albumitem->isAlbumName, $targetdir, $n);
     }
     elseif (($albumitem->image->name))
     {
        // C'est une image
        if ($albumitem->caption) $n = <a href="http://www.php.net/sprintf">sprintf</a>("%02d %s %s - %s", $index,  ($albumitem->isHidden()!=0)?"#":"-", $albumitem->image->name, $albumitem->caption);
         else $n = <a href="http://www.php.net/sprintf">sprintf</a>("%02d - %s", $index, $albumitem->image->name);
        $n = <a href="http://www.php.net/strip_tags">strip_tags</a>($n);
        $n = <a href="http://www.php.net/str_replace">str_replace</a>('"',"'", $n);
        $n = <a href="http://www.php.net/str_replace">str_replace</a>(':'," ", $n);
        $n = <a href="http://www.php.net/str_replace">str_replace</a>('/'," ", $n);
        $n = <a href="http://www.php.net/str_replace">str_replace</a>('*',"-", $n);
        $n = <a href="http://www.php.net/str_replace">str_replace</a>('?'," ", $n);
        $n = <a href="http://www.php.net/str_replace">str_replace</a>("n"," ", $n);
        $n = <a href="http://www.php.net/str_replace">str_replace</a>("r"," ", $n);
        $n = <a href="http://www.php.net/substr">substr</a>($n, 0, 80);
        $n = <a href="http://www.php.net/mb_convert_encoding">mb_convert_encoding</a>($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 (<a href="http://www.php.net/array_key_exists">array_key_exists</a>('Description', $albumitem->extraFields))
		if ($albumitem->extraFields['Description'] != '') 
			$text .= "nn" . $albumitem->extraFields['Description'];
        // Apply modification
        $in = $gallery . '/' . $album . '/' . $albumitem->image->name . "." . $albumitem->image->type;
        $out = $targetdir . "/" . $n . "." . $albumitem->image->type;
//  With exiftool
            <a href="http://www.php.net/copy">copy</a>($in, $out);
	    if(($text!="") && (!<a href="http://www.php.net/preg_match">preg_match</a>('/(mov|mpg|avi|wmv)$/i',$out)))
	    {
		    $tmpfname = <a href="http://www.php.net/tempnam">tempnam</a>("/tmp", "gallery2jpeg");
		    $handle = <a href="http://www.php.net/fopen">fopen</a>($tmpfname, "w");
		    <a href="http://www.php.net/fwrite">fwrite</a>($handle, $text);
		    <a href="http://www.php.net/fclose">fclose</a>($handle);
		    <a href="http://www.php.net/exec">exec</a>("exiftool -overwrite_original -Comment<=$tmpfname "$out" ");
		    <a href="http://www.php.net/unlink">unlink</a>($tmpfname);
	    }
// ----
      }
     $index++;
    }
}
 
function main($argc, $argv)
{
    $gallery = $argv[1];
    $album = $argv[2];
    $targetdir = $argv[3];
    if ($album == '.')
    {
        $filename = $gallery . '/' . 'albumdb.dat'; 
        $fd = <a href="http://www.php.net/fopen">fopen</a>($filename, "r");
	$contents = <a href="http://www.php.net/fread">fread</a>($fd, <a href="http://www.php.net/filesize">filesize</a>($filename));
	<a href="http://www.php.net/fclose">fclose</a>($fd);
	$data = <a href="http://www.php.net/unserialize">unserialize</a>($contents);
	foreach($data as $album)
	{
		$filename = $gallery . '/' . $album . '/' . 'album.dat'; 
		$fd = <a href="http://www.php.net/fopen">fopen</a>($filename, "r");
		$contents = <a href="http://www.php.net/fread">fread</a>($fd, <a href="http://www.php.net/filesize">filesize</a>($filename));
		<a href="http://www.php.net/fclose">fclose</a>($fd);
		$album_data = <a href="http://www.php.net/unserialize">unserialize</a>($contents);
		if (!$album_data->fields['parentAlbumName'])
		{
			processGalleryAlbum($gallery, $album, $targetdir); 
		}
	}
 
    }
    else
    {
     processGalleryAlbum($gallery, $album, $targetdir);
     }
}
 
main($argc, $argv);
 
?>

Import an image tree to Gallery

gallery-import.php

<?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) <a href="http://www.php.net/die">die</a>("Import Album ${conf['album_import']} not found !");
 
function import_files($album, $dir)
{
    global $albumDB;
	global $conf;
 
    foreach(<a href="http://www.php.net/scandir">scandir</a>($dir) as $entry)
    {
        if (($entry == '.') || ($entry == '..')) continue;
        $path = $dir . DIRECTORY_SEPARATOR . $entry;
        if (<a href="http://www.php.net/is_dir">is_dir</a>($path))
        {
            $entry = <a href="http://www.php.net/utf8_decode">utf8_decode</a>($entry);
			$rep = <a href="http://www.php.net/strtr">strtr</a>($entry,'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ',
'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
			$rep = <a href="http://www.php.net/preg_replace">preg_replace</a>('/[^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... ";
			<a href="http://www.php.net/unset">unset</a>($output);
            <a href="http://www.php.net/exec">exec</a>("exiftool -Description -comment -s -s -s '$path'",$output);
            $caption = <a href="http://www.php.net/implode">implode</a>("n",$output);
            $caption = <a href="http://www.php.net/utf8_decode">utf8_decode</a>($caption);
            print "($caption)";
            $path_parts = <a href="http://www.php.net/pathinfo">pathinfo</a>($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, 
                                <a href="http://www.php.net/strtolower">strtolower</a>($path_parts['extension']), 
                                $cur_name,
                                $caption);
            print $status[1];
            $last_album->save();
            print "<br />";
        }
    }
	<a href="http://www.php.net/flush">flush</a>();
}
 
import_files($album_import, $conf['dir_import']);
 
?>