Subversion Repositories eFlore/Applications.cel

Rev

Rev 1224 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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