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