Subversion Repositories eFlore/Applications.cel

Rev

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