Subversion Repositories eFlore/Applications.cel

Rev

Rev 1205 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1203 jpm 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Script d'extration des métadonnées des images pour intégration dans la BDD v2.
1224 jpm 5
 * Utilisation : /opt/lampp/bin/php cli.php ExtracteurMeta
1203 jpm 6
 *
7
 * @category	php 5.2
8
 * @package		Cel/Scripts
1224 jpm 9
 * @author		Aurelien PERONNET <aurelien@tela-botanica.org>
1203 jpm 10
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
11
 * @copyright	Copyright (c) 2012, Tela Botanica (accueil@tela-botanica.org)
1224 jpm 12
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
13
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
14
 * @version		$Id$
1203 jpm 15
 */
1204 jpm 16
class ExtracteurMeta {
1203 jpm 17
 
1204 jpm 18
	const DROIT = 0705;
1203 jpm 19
 
1224 jpm 20
	private $chemin_images = '';
21
	private $bdd = null;
22
	private $script = null;
1204 jpm 23
 
24
	private $idImg = null;
25
	private $metaComplet = null;
26
	private $exif = null;
27
	private $iptc = null;
28
	private $xmp = null;
29
	private $makerNotes = null;
30
	private $metaAutres = null;
31
 
32
 
1224 jpm 33
	public function __construct(Conteneur $conteneur) {
34
		$this->chemin_images = $conteneur->getParametre('cel.chemin_images');
1204 jpm 35
		$this->bdd = $conteneur->getBdd();
1224 jpm 36
		$this->script = $conteneur->getScript();
1204 jpm 37
	}
38
 
39
	public function executer($arguments) {
1224 jpm 40
		echo "EXTRACTION des MÉTADONNÉES\n";
1204 jpm 41
		echo "Vérification config : ".$this->verifierConfig()."\n";
42
 
1224 jpm 43
		$images = $this->obtenirImagesEnBdd();
44
		foreach ($images as $img) {
45
			$this->idImg = $img['id_image'];
1204 jpm 46
 
1224 jpm 47
			$cheminImage = $this->obtenirCheminImageOriginale($this->idImg);
48
			if (file_exists($cheminImage)) {clear
49
				$cmd = "exiftool -X -file:comment $cheminImage";
50
				$metaFile = $this->executerCommandeSysteme($cmd);
1204 jpm 51
 
1224 jpm 52
				if ($this->neccessiterRemplacement($metaFile) === true) {
53
					//print "Remplacement : {$this->idImg} \n";
1204 jpm 54
 
1224 jpm 55
					$cmd = "exiftool -X -D -U -b -c '%.6f' -d '%Y-%m-%d %H:%M:%S' -All $cheminImage";
56
					$this->metaComplet = $this->executerCommandeSysteme($cmd);
1204 jpm 57
 
1224 jpm 58
					$cmd = "exiftool -X -D -c '%.6f' -d '%Y-%m-%d %H:%M:%S' -exif:all $cheminImage";
59
					$this->exif = $this->executerCommandeSysteme($cmd);
1204 jpm 60
 
1224 jpm 61
					$cmd = "exiftool -X -D -c '%.6f' -d '%Y-%m-%d %H:%M:%S' -iptc:all $cheminImage";
62
					$this->iptc = $this->executerCommandeSysteme($cmd);
1204 jpm 63
 
1224 jpm 64
					$cmd = "exiftool -X -D -c '%.6f' -d '%Y-%m-%d %H:%M:%S' -xmp:all $cheminImage";
65
					$this->xmp = $this->executerCommandeSysteme($cmd);
1204 jpm 66
 
1224 jpm 67
					$cmd = "exiftool -X -D -c '%.6f' -d '%Y-%m-%d %H:%M:%S' -makernotes:all $cheminImage";
68
					$this->makerNotes = $this->executerCommandeSysteme($cmd);
1204 jpm 69
 
1224 jpm 70
					$cmd = "exiftool -X -D -c '%.6f' -d '%Y-%m-%d %H:%M:%S' --exif:all --iptc:all --xmp:all --makernotes:all $cheminImage";
71
					$this->metaAutres = $this->executerCommandeSysteme($cmd);
72
 
73
					$this->ecrireFichierRdf();
74
					$this->mettreAJourBdd();
75
				}
76
			} else {
77
				//print "Fichier image '{$this->idImg}' introuvable\n";
78
			}
79
			$this->script->afficherAvancement("Analyse des images");
1203 jpm 80
		}
1224 jpm 81
		print "\n";
1203 jpm 82
	}
83
 
1224 jpm 84
	private function verifierConfig() {
85
		if (empty($this->chemin_images)) {
86
			$message = "Vous devez indiquer le dossier contenant la hiérarchie des images dans le paramètre : ".
87
					"[cel] chemin_images";
88
			$code = (string) E_USER_ERROR;
89
			throw new Exception($message);
90
		}
91
		return 'OK';
1203 jpm 92
	}
93
 
1224 jpm 94
	private function obtenirImagesEnBdd() {
95
		$requete = 'SELECT id_image FROM cel_images ';
96
		$images = $this->bdd->requeter($requete);
97
		return $images;
98
	}
99
 
1204 jpm 100
	private function executerCommandeSysteme($commande) {
101
		$handle = popen($commande, 'r');
102
		$metaXml = '';
103
		while (!feof($handle)) {
104
			$metaXml .= fread($handle, 8192);
1203 jpm 105
		}
1204 jpm 106
		fclose($handle);
107
		return $metaXml;
1203 jpm 108
	}
109
 
1224 jpm 110
	private function obtenirCheminImageRDF() {
111
		return $this->obtenirCheminImage('RDF', 'xml');
1204 jpm 112
	}
113
 
1224 jpm 114
	private function obtenirCheminImageOriginale() {
115
		return $this->obtenirCheminImage('O');
1204 jpm 116
	}
117
 
1224 jpm 118
	private function obtenirCheminImage($format, $extension = 'jpg') {
119
		$nom = $this->convertirIdBddVersNomFichier($format, $extension);
120
		$cheminDossier = $this->obtenirDossierPourFormat($format);
1204 jpm 121
		if (file_exists($cheminDossier) === false) {
122
			$this->creerDossier($cheminDossier);
123
		}
124
		return $cheminDossier.'/'.$nom;
1203 jpm 125
	}
126
 
1224 jpm 127
	private function convertirIdBddVersNomFichier($format, $extension) {
128
		$id_avec_zeros = sprintf('%09s', $this->idImg) ;
1203 jpm 129
		$id_avec_zeros_underscores = wordwrap($id_avec_zeros, 3 , '_', true) ;
130
		$nom_fichier = $id_avec_zeros_underscores.'_'.$format.'.'.$extension;
131
		return $nom_fichier;
132
	}
133
 
1224 jpm 134
	private function obtenirDossierPourFormat($format) {
135
		$id = sprintf('%09s', $this->idImg);
1203 jpm 136
		$id = wordwrap($id, 3 , '_', true);
137
		list($dossierNiveau1, $dossierNiveau2) = explode('_', $id);
138
 
1224 jpm 139
		$chemin_sur_serveur_final = $this->chemin_images.'/'.$dossierNiveau1.'/'.$dossierNiveau2.'/'.$format;
1203 jpm 140
		return $chemin_sur_serveur_final;
141
	}
1204 jpm 142
 
143
	private function creerDossier($cheminDossier) {
144
		umask(0);
145
		if (mkdir($cheminDossier, self::DROIT, true) === false) {
146
			$message = "Problème durant la création du dossier '$cheminDossier'.";
147
			throw new Exception($message);
148
		}
149
	}
150
 
151
	private function neccessiterRemplacement($metaXml) {
152
		$meta = new SimpleXMLElement($metaXml);
153
		$rdf = $meta->children('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
154
		$file = $rdf->children('http://ns.exiftool.ca/File/1.0/');
155
		return strpos($file->Comment, "CREATOR: gd-jpeg") === false ? true : false;
156
	}
157
 
158
	private function ecrireFichierRdf() {
159
		$cheminRdf = $this->obtenirCheminImageRDF($this->idImg);
160
		file_put_contents($cheminRdf, $this->metaComplet);
161
	}
162
 
163
	private function mettreAJourBdd() {
1205 jpm 164
		$idImg = $this->bdd->proteger($this->idImg);
165
		$exif = $this->bdd->proteger($this->exif);
166
		$iptc = $this->bdd->proteger($this->iptc);
167
		$xmp = $this->bdd->proteger($this->xmp);
168
		$makerNotes = $this->bdd->proteger($this->makerNotes);
169
		$autres = $this->bdd->proteger($this->metaAutres);
170
 
171
		$requete = 'UPDATE cel_images SET '.
172
			"	meta_exif = $exif, ".
173
			"	meta_iptc = $iptc, ".
174
			"	meta_xmp = $xmp, ".
175
			"	meta_makernote = $makerNotes, ".
176
			"	meta_autres = $autres ".
177
			"WHERE id_image = $idImg ";
178
		$resultat = $this->bdd->executer($requete);
179
 
180
		echo "Mise à jour image '{$this->idImg}' : $resultat\n";
1204 jpm 181
	}
1203 jpm 182
}
183
?>