Subversion Repositories eFlore/Applications.cel

Rev

Rev 1205 | Blame | Last modification | View Log | RSS feed

<?php
// declare(encoding='UTF-8');
/**
 * Script d'extration des métadonnées des images pour intégration dans la BDD v2.
 * Utilisation : /opt/lampp/bin/php cli.php ExtracteurMeta
 *
 * @category    php 5.2
 * @package             Cel/Scripts
 * @author              Aurelien PERONNET <aurelien@tela-botanica.org>
 * @author              Jean-Pascal MILCENT <jpm@tela-botanica.org>
 * @copyright   Copyright (c) 2012, Tela Botanica (accueil@tela-botanica.org)
 * @license             http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
 * @license             http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
 * @version             $Id$
 */
class ExtracteurMeta {

        const DROIT = 0705;

        private $chemin_images = '';
        private $bdd = null;
        private $script = null;

        private $idImg = null;
        private $metaComplet = null;
        private $exif = null;
        private $iptc = null;
        private $xmp = null;
        private $makerNotes = null;
        private $metaAutres = null;


        public function __construct(Conteneur $conteneur) {
                $this->chemin_images = $conteneur->getParametre('cel.chemin_images');
                $this->bdd = $conteneur->getBdd();
                $this->script = $conteneur->getScript();
        }

        public function executer($arguments) {
                echo "EXTRACTION des MÉTADONNÉES\n";
                echo "Vérification config : ".$this->verifierConfig()."\n";

                $images = $this->obtenirImagesEnBdd();
                foreach ($images as $img) {
                        $this->idImg = $img['id_image'];

                        $cheminImage = $this->obtenirCheminImageOriginale($this->idImg);
                        if (file_exists($cheminImage)) {clear
                                $cmd = "exiftool -X -file:comment $cheminImage";
                                $metaFile = $this->executerCommandeSysteme($cmd);

                                if ($this->neccessiterRemplacement($metaFile) === true) {
                                        //print "Remplacement : {$this->idImg} \n";

                                        $cmd = "exiftool -X -D -U -b -c '%.6f' -d '%Y-%m-%d %H:%M:%S' -All $cheminImage";
                                        $this->metaComplet = $this->executerCommandeSysteme($cmd);

                                        $cmd = "exiftool -X -D -c '%.6f' -d '%Y-%m-%d %H:%M:%S' -exif:all $cheminImage";
                                        $this->exif = $this->executerCommandeSysteme($cmd);

                                        $cmd = "exiftool -X -D -c '%.6f' -d '%Y-%m-%d %H:%M:%S' -iptc:all $cheminImage";
                                        $this->iptc = $this->executerCommandeSysteme($cmd);

                                        $cmd = "exiftool -X -D -c '%.6f' -d '%Y-%m-%d %H:%M:%S' -xmp:all $cheminImage";
                                        $this->xmp = $this->executerCommandeSysteme($cmd);

                                        $cmd = "exiftool -X -D -c '%.6f' -d '%Y-%m-%d %H:%M:%S' -makernotes:all $cheminImage";
                                        $this->makerNotes = $this->executerCommandeSysteme($cmd);

                                        $cmd = "exiftool -X -D -c '%.6f' -d '%Y-%m-%d %H:%M:%S' --exif:all --iptc:all --xmp:all --makernotes:all $cheminImage";
                                        $this->metaAutres = $this->executerCommandeSysteme($cmd);

                                        $this->ecrireFichierRdf();
                                        $this->mettreAJourBdd();
                                }
                        } else {
                                //print "Fichier image '{$this->idImg}' introuvable\n";
                        }
                        $this->script->afficherAvancement("Analyse des images");
                }
                print "\n";
        }

        private function verifierConfig() {
                if (empty($this->chemin_images)) {
                        $message = "Vous devez indiquer le dossier contenant la hiérarchie des images dans le paramètre : ".
                                        "[cel] chemin_images";
                        $code = (string) E_USER_ERROR;
                        throw new Exception($message);
                }
                return 'OK';
        }

        private function obtenirImagesEnBdd() {
                $requete = 'SELECT id_image FROM cel_images ';
                $images = $this->bdd->requeter($requete);
                return $images;
        }

        private function executerCommandeSysteme($commande) {
                $handle = popen($commande, 'r');
                $metaXml = '';
                while (!feof($handle)) {
                        $metaXml .= fread($handle, 8192);
                }
                fclose($handle);
                return $metaXml;
        }

        private function obtenirCheminImageRDF() {
                return $this->obtenirCheminImage('RDF', 'xml');
        }

        private function obtenirCheminImageOriginale() {
                return $this->obtenirCheminImage('O');
        }

        private function obtenirCheminImage($format, $extension = 'jpg') {
                $nom = $this->convertirIdBddVersNomFichier($format, $extension);
                $cheminDossier = $this->obtenirDossierPourFormat($format);
                if (file_exists($cheminDossier) === false) {
                        $this->creerDossier($cheminDossier);
                }
                return $cheminDossier.'/'.$nom;
        }

        private function convertirIdBddVersNomFichier($format, $extension) {
                $id_avec_zeros = sprintf('%09s', $this->idImg) ;
                $id_avec_zeros_underscores = wordwrap($id_avec_zeros, 3 , '_', true) ;
                $nom_fichier = $id_avec_zeros_underscores.'_'.$format.'.'.$extension;
                return $nom_fichier;
        }

        private function obtenirDossierPourFormat($format) {
                $id = sprintf('%09s', $this->idImg);
                $id = wordwrap($id, 3 , '_', true);
                list($dossierNiveau1, $dossierNiveau2) = explode('_', $id);

                $chemin_sur_serveur_final = $this->chemin_images.'/'.$dossierNiveau1.'/'.$dossierNiveau2.'/'.$format;
                return $chemin_sur_serveur_final;
        }

        private function creerDossier($cheminDossier) {
                umask(0);
                if (mkdir($cheminDossier, self::DROIT, true) === false) {
                        $message = "Problème durant la création du dossier '$cheminDossier'.";
                        throw new Exception($message);
                }
        }

        private function neccessiterRemplacement($metaXml) {
                $meta = new SimpleXMLElement($metaXml);
                $rdf = $meta->children('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
                $file = $rdf->children('http://ns.exiftool.ca/File/1.0/');
                return strpos($file->Comment, "CREATOR: gd-jpeg") === false ? true : false;
        }

        private function ecrireFichierRdf() {
                $cheminRdf = $this->obtenirCheminImageRDF($this->idImg);
                file_put_contents($cheminRdf, $this->metaComplet);
        }

        private function mettreAJourBdd() {
                $idImg = $this->bdd->proteger($this->idImg);
                $exif = $this->bdd->proteger($this->exif);
                $iptc = $this->bdd->proteger($this->iptc);
                $xmp = $this->bdd->proteger($this->xmp);
                $makerNotes = $this->bdd->proteger($this->makerNotes);
                $autres = $this->bdd->proteger($this->metaAutres);

                $requete = 'UPDATE cel_images SET '.
                        "       meta_exif = $exif, ".
                        "       meta_iptc = $iptc, ".
                        "       meta_xmp = $xmp, ".
                        "       meta_makernote = $makerNotes, ".
                        "       meta_autres = $autres ".
                        "WHERE id_image = $idImg ";
                $resultat = $this->bdd->executer($requete);

                echo "Mise à jour image '{$this->idImg}' : $resultat\n";
        }
}
?>