Subversion Repositories eFlore/Applications.cel

Rev

Rev 1203 | Rev 1205 | Go to most recent revision | Blame | Compare with Previous | 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.
 *
 * @category    php 5.2
 * @package             Cel/Scripts
 * @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;

        public $config;
        public $bdd;

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


        public function __construct($config, Conteneur $conteneur = null) {
                // Tableau contenant la config de Jrest
                $this->config = $config;

                $conteneur = is_null($conteneur) ? new Conteneur($this->config) : $conteneur;
                $this->bdd = $conteneur->getBdd();
        }

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

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

                $remplacement = 'NON';
                if ($this->neccessiterRemplacement($metaFile) === true) {
                        $remplacement = 'OUI';

                        $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();
                }
                echo "Remplacement : $remplacement \n";
        }

        private function verifierConfig() {
                if (empty($this->config['cel']['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 executerCommandeSysteme($commande) {
                $handle = popen($commande, 'r');
                $metaXml = '';
                while (!feof($handle)) {
                        $metaXml .= fread($handle, 8192);
                }
                fclose($handle);
                return $metaXml;
        }

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

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

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

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

        private function obtenirDossierPourFormat($id, $format) {
                $chemin_base = $this->config['cel']['chemin_images'];
                $chemin_sur_serveur = $chemin_base;

                $id = sprintf('%09s', $id);
                $id = wordwrap($id, 3 , '_', true);
                list($dossierNiveau1, $dossierNiveau2) = explode('_', $id);

                $chemin_sur_serveur_final = $chemin_sur_serveur.'/'.$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() {
                $requete = "SELECT nom_utilisateur FROM cel_images WHERE id_image = {$this->idImg} ";
                $resultats = $this->bdd->requeter($requete, Bdd::SQL_RETOUR_COLONNE);
                print_r($resultats);
                echo "\n";
        }
}
?>