3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2020 *
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')) {
20 * https://code.spip.net/@action_tourner_dist
22 * lorsque les arguments sont passes dans arg en GET :
25 * @param int $id_document
27 * angle de rotation en degre>0
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();
35 if (!preg_match(',^\W*(\d+)\W?(-?\d+)$,', $arg, $r)) {
36 spip_log("action_tourner_dist $arg pas compris");
39 list($id_document, $angle) = $r;
42 if ($id_document and autoriser('modifier', 'document', $id_document)) {
43 action_tourner_post($id_document, $angle);
50 * https://code.spip.net/@action_tourner_post
52 * @param int $id_document
54 * angle de rotation en degre>0
57 function action_tourner_post($id_document, $angle) {
58 $row = sql_fetsel('fichier,extension', 'spip_documents', 'id_document=' . intval($id_document));
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
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)) {
74 $src = $match[1] . '.' . $match[3];
75 $var_rot +
= intval($match[2]);
77 $var_rot = ((360 +
$var_rot) %
360); // 0, 90, 180 ou 270
80 $dest = preg_replace(',\.[^.]+$,', '-r' . $var_rot . '$0', $src);
81 spip_log("rotation $var_rot $src : $dest");
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']);
88 list($hauteur, $largeur) = taille_image($res);
89 $res = extraire_attribut($res, 'src');
91 include_spip('inc/getdocument');
92 deplacer_fichier_upload($res, $dest);
95 $size_image = @getimagesize
($dest);
96 $largeur = $size_image[0];
97 $hauteur = $size_image[1];
101 if ($largeur > 0 and $hauteur > 0) {
103 'fichier' => set_spip_doc($dest),
104 'largeur' => $largeur,
105 'hauteur' => $hauteur,
106 'distant' => 'non' // le document n'est plus distant apres une transformation
108 if ($taille = @filesize
($dest)) {
109 $set['taille'] = $taille;
111 sql_updateq('spip_documents', $set, 'id_document=' . intval($id_document));
113 spip_log("rotation : j'efface $effacer");
114 spip_unlink($effacer);
116 // pipeline pour les plugins
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',
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) {
140 if (function_exists('exif_read_data')
141 and $exif = exif_read_data($fichier)
143 $ort = $exif['IFD0']['Orientation']
144 or $ort = $exif['Orientation'])
146 spip_log("rotation: $ort");
159 action_tourner_post(array(null, $id_document, $rot));