[SPIP] +2.1.12
[velocampus/web/www.git] / www / ecrire / action / tourner.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2011 *
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 // http://doc.spip.org/@action_tourner_dist
16 function action_tourner_dist() {
17 $securiser_action = charger_fonction('securiser_action', 'inc');
18 $arg = $securiser_action();
19
20 if (!preg_match(",^\W*(\d+)\W?(-?\d+)$,", $arg, $r)) {
21 spip_log("action_tourner_dist $arg pas compris");
22 } else action_tourner_post($r);
23 }
24
25 // http://doc.spip.org/@action_tourner_post
26 function action_tourner_post($r)
27 {
28 $arg = $r[1];
29 $row = sql_fetsel("fichier", "spip_documents", "id_document=$arg");
30
31 if (!$row) return;
32
33 include_spip('inc/charsets'); # pour le nom de fichier
34 include_spip('inc/documents');
35 // Fichier destination : on essaie toujours de repartir de l'original
36 $var_rot = $r[2];
37 include_spip('inc/distant'); # pour copie_locale
38 $src = _DIR_RACINE . copie_locale(get_spip_doc($row['fichier']));
39 if (preg_match(',^(.*)-r(90|180|270)\.([^.]+)$,', $src, $match)) {
40 $effacer = $src;
41 $src = $match[1].'.'.$match[3];
42 $var_rot += intval($match[2]);
43 }
44 $var_rot = ((360 + $var_rot) % 360); // 0, 90, 180 ou 270
45
46 if ($var_rot > 0) {
47 $dest = preg_replace(',\.[^.]+$,', '-r'.$var_rot.'$0', $src);
48 spip_log("rotation $var_rot $src : $dest");
49
50 $process = $GLOBALS['meta']['image_process'];
51
52 // imagick (php4-imagemagick)
53 if ($process == 'imagick') {
54 $handle = imagick_readimage($src);
55 imagick_rotate($handle, $var_rot);
56 imagick_write($handle, $dest);
57 if (!@file_exists($dest)) return; // echec imagick
58 }
59 else if ($process == "gd2") { // theoriquement compatible gd1, mais trop forte degradation d'image
60 gdRotate ($src, $dest, $var_rot);
61 }
62 else if ($process == "convert") {
63 if (_CONVERT_COMMAND!='') {
64 define ('_CONVERT_COMMAND', 'convert');
65 define ('_ROTATE_COMMAND', _CONVERT_COMMAND.' -rotate %t %src %dest');
66 } else
67 define ('_ROTATE_COMMAND', '');
68 if (_ROTATE_COMMAND!=='') {
69 $commande = str_replace(
70 array('%t', '%src', '%dest'),
71 array(
72 $var_rot,
73 escapeshellcmd($src),
74 escapeshellcmd($dest)
75 ),
76 _ROTATE_COMMAND);
77 spip_log($commande);
78 exec($commande);
79 } else
80 $dest = $src;
81 }
82 }
83 else
84 $dest = $src;
85
86 $size_image = @getimagesize($dest);
87 $largeur = $size_image[0];
88 $hauteur = $size_image[1];
89
90 // succes !
91 if ($largeur>0 AND $hauteur>0) {
92 sql_updateq('spip_documents', array('fichier' => set_spip_doc($dest), 'largeur'=>$largeur, 'hauteur'=>$hauteur), "id_document=$arg");
93 if ($effacer) {
94 spip_log("j'efface $effacer");
95 spip_unlink($effacer);
96 }
97 // pipeline pour les plugins
98 pipeline('post_edition',
99 array(
100 'args' => array(
101 'table' => 'spip_documents',
102 'table_objet' => 'documents',
103 'spip_table_objet' => 'spip_documents',
104 'type' =>'document',
105 'id_objet' => $arg,
106 'champs' => array('rotation'=>$r[2],'orientation'=>$var_rot,'fichier'=>$row),
107 'serveur' => $serveur,
108 'action'=>'tourner',
109 ),
110 'data' => array('fichier'=>$row)
111 )
112 );
113 }
114
115 }
116
117
118 /////////////////////////////////////////////////////////////////////
119 //
120 // Faire tourner une image
121 //
122 // http://doc.spip.org/@gdRotate
123 function gdRotate ($src, $dest, $rtt){
124 $src_img = '';
125 if(preg_match("/\.(png|gif|jpe?g|bmp)$/i", $src, $regs)) {
126 switch($regs[1]) {
127 case 'png':
128 if (function_exists('ImageCreateFromPNG')) {
129 $src_img=ImageCreateFromPNG($src);
130 $save = 'imagepng';
131 }
132 break;
133 case 'gif':
134 if (function_exists('ImageCreateFromGIF')) {
135 $src_img=ImageCreateFromGIF($src);
136 $save = 'imagegif';
137 }
138 break;
139 case 'jpeg':
140 case 'jpg':
141 if (function_exists('ImageCreateFromJPEG')) {
142 $src_img=ImageCreateFromJPEG($src);
143 $save = 'Imagejpeg';
144 }
145 break;
146 case 'bmp':
147 if (function_exists('ImageCreateFromWBMP')) {
148 $src_img=@ImageCreateFromWBMP($src);
149 $save = 'imagewbmp';
150 }
151 break;
152 }
153 }
154
155 if (!$src_img) {
156 spip_log("gdrotate: image non lue, $src");
157 return false;
158 }
159
160 $size=@getimagesize($src);
161 if (!($size[0] * $size[1])) return false;
162
163 if (function_exists('imagerotate')) {
164 $dst_img = imagerotate($src_img, -$rtt, 0);
165 } else {
166
167 // Creer l'image destination (hauteur x largeur) et la parcourir
168 // pixel par pixel (un truc de fou)
169 if ($rtt == 180)
170 $size_dest = $size;
171 else
172 $size_dest = array($size[1],$size[0]);
173
174 if ($GLOBALS['meta']['image_process'] == "gd2")
175 $dst_img=ImageCreateTrueColor($size_dest[0],$size_dest[1]);
176 else
177 $dst_img=ImageCreate($size_dest[0],$size_dest[1]);
178
179 // t=top; b=bottom; r=right; l=left
180 for ($t=0;$t<=$size_dest[0]-1; $t++) {
181 $b = $size_dest[0] -1 - $t;
182 for ($l=0;$l<=$size_dest[1]-1; $l++) {
183 $r = $size_dest[1] -1 - $l;
184 switch ($rtt) {
185 case 90:
186 imagecopy($dst_img,$src_img,$t,$r,$r,$b,1,1);
187 break;
188 case 270:
189 imagecopy($dst_img,$src_img,$t,$l,$r,$t,1,1);
190 break;
191 case 180:
192 imagecopy($dst_img,$src_img,$t,$l,$b,$r,1,1);
193 break;
194 }
195 }
196 }
197 }
198 ImageDestroy($src_img);
199 ImageInterlace($dst_img,0);
200
201 // obligatoire d'enregistrer dans le meme format, puisqu'on change le doc
202 // mais pas son extension
203 $save($dst_img,$dest);
204 }
205
206 // Appliquer l'EXIF orientation
207 // cf. http://trac.rezo.net/trac/spip/ticket/1494
208 // http://doc.spip.org/@tourner_selon_exif_orientation
209 function tourner_selon_exif_orientation($id_document, $fichier) {
210
211 if (function_exists('exif_read_data')
212 AND $exif = exif_read_data($fichier)
213 AND (
214 $ort = $exif['IFD0']['Orientation']
215 OR $ort = $exif['Orientation'])
216 ) {
217 spip_log("rotation: $ort");
218 $rot = null;
219 switch ($ort) {
220 case 3:
221 $rot = 180;
222 case 6:
223 $rot = 90;
224 case 8:
225 $rot = -90;
226 }
227 if ($rot)
228 action_tourner_post(array(null,$id_document, $rot));
229 }
230 }
231
232 ?>