3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2011 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
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 \***************************************************************************/
13 if (!defined('_ECRIRE_INC_VERSION')) return;
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();
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);
25 // http://doc.spip.org/@action_tourner_post
26 function action_tourner_post($r)
29 $row = sql_fetsel("fichier", "spip_documents", "id_document=$arg");
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
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)) {
41 $src = $match[1].'.'.$match[3];
42 $var_rot +
= intval($match[2]);
44 $var_rot = ((360 +
$var_rot) %
360); // 0, 90, 180 ou 270
47 $dest = preg_replace(',\.[^.]+$,', '-r'.$var_rot.'$0', $src);
48 spip_log("rotation $var_rot $src : $dest");
50 $process = $GLOBALS['meta']['image_process'];
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
59 else if ($process == "gd2") { // theoriquement compatible gd1, mais trop forte degradation d'image
60 gdRotate ($src, $dest, $var_rot);
62 else if ($process == "convert") {
63 if (_CONVERT_COMMAND
!='') {
64 define ('_CONVERT_COMMAND', 'convert');
65 define ('_ROTATE_COMMAND', _CONVERT_COMMAND
.' -rotate %t %src %dest');
67 define ('_ROTATE_COMMAND', '');
68 if (_ROTATE_COMMAND
!=='') {
69 $commande = str_replace(
70 array('%t', '%src', '%dest'),
86 $size_image = @getimagesize
($dest);
87 $largeur = $size_image[0];
88 $hauteur = $size_image[1];
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");
94 spip_log("j'efface $effacer");
95 spip_unlink($effacer);
97 // pipeline pour les plugins
98 pipeline('post_edition',
101 'table' => 'spip_documents',
102 'table_objet' => 'documents',
103 'spip_table_objet' => 'spip_documents',
106 'champs' => array('rotation'=>$r[2],'orientation'=>$var_rot,'fichier'=>$row),
107 'serveur' => $serveur,
110 'data' => array('fichier'=>$row)
118 /////////////////////////////////////////////////////////////////////
120 // Faire tourner une image
122 // http://doc.spip.org/@gdRotate
123 function gdRotate ($src, $dest, $rtt){
125 if(preg_match("/\.(png|gif|jpe?g|bmp)$/i", $src, $regs)) {
128 if (function_exists('ImageCreateFromPNG')) {
129 $src_img=ImageCreateFromPNG($src);
134 if (function_exists('ImageCreateFromGIF')) {
135 $src_img=ImageCreateFromGIF($src);
141 if (function_exists('ImageCreateFromJPEG')) {
142 $src_img=ImageCreateFromJPEG($src);
147 if (function_exists('ImageCreateFromWBMP')) {
148 $src_img=@ImageCreateFromWBMP
($src);
156 spip_log("gdrotate: image non lue, $src");
160 $size=@getimagesize
($src);
161 if (!($size[0] * $size[1])) return false;
163 if (function_exists('imagerotate')) {
164 $dst_img = imagerotate($src_img, -$rtt, 0);
167 // Creer l'image destination (hauteur x largeur) et la parcourir
168 // pixel par pixel (un truc de fou)
172 $size_dest = array($size[1],$size[0]);
174 if ($GLOBALS['meta']['image_process'] == "gd2")
175 $dst_img=ImageCreateTrueColor($size_dest[0],$size_dest[1]);
177 $dst_img=ImageCreate($size_dest[0],$size_dest[1]);
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;
186 imagecopy($dst_img,$src_img,$t,$r,$r,$b,1,1);
189 imagecopy($dst_img,$src_img,$t,$l,$r,$t,1,1);
192 imagecopy($dst_img,$src_img,$t,$l,$b,$r,1,1);
198 ImageDestroy($src_img);
199 ImageInterlace($dst_img,0);
201 // obligatoire d'enregistrer dans le meme format, puisqu'on change le doc
202 // mais pas son extension
203 $save($dst_img,$dest);
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) {
211 if (function_exists('exif_read_data')
212 AND $exif = exif_read_data($fichier)
214 $ort = $exif['IFD0']['Orientation']
215 OR $ort = $exif['Orientation'])
217 spip_log("rotation: $ort");
228 action_tourner_post(array(null,$id_document, $rot));