[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins-dist / medias / action / tourner.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) {
14 return;
15 }
16
17 /**
18 * Tourner un document
19 *
20 * https://code.spip.net/@action_tourner_dist
21 *
22 * lorsque les arguments sont passes dans arg en GET :
23 * id_document-angle
24 *
25 * @param int $id_document
26 * @param int $angle
27 * angle de rotation en degre>0
28 * @return void
29 */
30 function action_tourner_dist($id_document = null, $angle = null) {
31 if (is_null($id_document) or is_null($angle)) {
32 $securiser_action = charger_fonction('securiser_action', 'inc');
33 $arg = $securiser_action();
34
35 if (!preg_match(',^\W*(\d+)\W?(-?\d+)$,', $arg, $r)) {
36 spip_log("action_tourner_dist $arg pas compris");
37 } else {
38 array_shift($r);
39 list($id_document, $angle) = $r;
40 }
41 }
42 if ($id_document and autoriser('modifier', 'document', $id_document)) {
43 action_tourner_post($id_document, $angle);
44 }
45 }
46
47 /**
48 * Tourner un document
49 *
50 * https://code.spip.net/@action_tourner_post
51 *
52 * @param int $id_document
53 * @param int $angle
54 * angle de rotation en degre>0
55 * @return
56 */
57 function action_tourner_post($id_document, $angle) {
58 $row = sql_fetsel('fichier,extension', 'spip_documents', 'id_document=' . intval($id_document));
59
60 if (!$row) {
61 return;
62 }
63
64 include_spip('inc/charsets'); # pour le nom de fichier
65 include_spip('inc/documents');
66 // Fichier destination : on essaie toujours de repartir de l'original
67 $var_rot = $angle;
68 $effacer = false;
69
70 include_spip('inc/distant'); # pour copie_locale
71 $src = _DIR_RACINE . copie_locale(get_spip_doc($row['fichier']));
72 if (preg_match(',^(.*)-r(90|180|270)\.([^.]+)$,', $src, $match)) {
73 $effacer = $src;
74 $src = $match[1] . '.' . $match[3];
75 $var_rot += intval($match[2]);
76 }
77 $var_rot = ((360 + $var_rot) % 360); // 0, 90, 180 ou 270
78
79 if ($var_rot > 0) {
80 $dest = preg_replace(',\.[^.]+$,', '-r' . $var_rot . '$0', $src);
81 spip_log("rotation $var_rot $src : $dest");
82
83 include_spip('inc/filtres');
84 include_spip('public/parametrer'); // charger les fichiers fonctions #bugfix spip 2.1.0
85 $res = filtrer('image_rotation', $src, $var_rot);
86 $res = filtrer('image_format', $res, $row['extension']);
87
88 list($hauteur, $largeur) = taille_image($res);
89 $res = extraire_attribut($res, 'src');
90
91 include_spip('inc/getdocument');
92 deplacer_fichier_upload($res, $dest);
93 } else {
94 $dest = $src;
95 $size_image = @getimagesize($dest);
96 $largeur = $size_image[0];
97 $hauteur = $size_image[1];
98 }
99
100 // succes !
101 if ($largeur > 0 and $hauteur > 0) {
102 $set = array(
103 'fichier' => set_spip_doc($dest),
104 'largeur' => $largeur,
105 'hauteur' => $hauteur,
106 'distant' => 'non' // le document n'est plus distant apres une transformation
107 );
108 if ($taille = @filesize($dest)) {
109 $set['taille'] = $taille;
110 }
111 sql_updateq('spip_documents', $set, 'id_document=' . intval($id_document));
112 if ($effacer) {
113 spip_log("rotation : j'efface $effacer");
114 spip_unlink($effacer);
115 }
116 // pipeline pour les plugins
117 pipeline(
118 'post_edition',
119 array(
120 'args' => array(
121 'table' => 'spip_documents',
122 'table_objet' => 'documents',
123 'spip_table_objet' => 'spip_documents',
124 'type' => 'document',
125 'id_objet' => $id_document,
126 'champs' => array('rotation' => $angle, 'orientation' => $var_rot, 'fichier' => $row['fichier']),
127 'action' => 'tourner',
128 ),
129 'data' => $set
130 )
131 );
132 }
133 }
134
135 // Appliquer l'EXIF orientation
136 // cf. http://trac.rezo.net/trac/spip/ticket/1494
137 // https://code.spip.net/@tourner_selon_exif_orientation
138 function tourner_selon_exif_orientation($id_document, $fichier) {
139
140 if (function_exists('exif_read_data')
141 and $exif = exif_read_data($fichier)
142 and (
143 $ort = $exif['IFD0']['Orientation']
144 or $ort = $exif['Orientation'])
145 ) {
146 spip_log("rotation: $ort");
147 $rot = null;
148 switch ($ort) {
149 case 3:
150 $rot = 180;
151 // rotation à 180
152 case 6:
153 $rot = 90;
154 // rotation à 90
155 case 8:
156 $rot = -90;
157 }
158 if ($rot) {
159 action_tourner_post(array(null, $id_document, $rot));
160 }
161 }
162 }