[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins / odt2spip_32 / inc / odt2spip_retailler_img.php
1 <?php
2 /**
3 * Créer un article à partir d'un fichier au format odt
4 *
5 * @author cy_altern
6 * @license GNU/LGPL
7 *
8 * @package plugins
9 * @subpackage odt2spip
10 * @category import
11 *
12 *
13 */
14 if (!defined('_ECRIRE_INC_VERSION')) {
15 return;
16 }
17
18 /**
19 * retailler une image : (ne gère que les images GIF, JPG et PNG)
20 *
21 * @internal
22 * Gestion de la transparence des PNG : code de matt1walsh@gmail.com
23 * sur {@link http://fr2.php.net/manual/fr/function.imagecopyresampled.php}
24 * @param string $img_ini Le fichier à retailler
25 * @param int $l Largeur max de l'image finale
26 * @param int $h Hauteur max
27 * @return string Le message d'erreur en cas de problème
28 */
29 function inc_odt2spip_retailler_img($img_ini, $l = 0, $h = 400) {
30 if (!file_exists($img_ini)) {
31 return 'Le fichier ' . $img_ini . ' n\'existe pas';
32 }
33 // determiner le type de fonction de creation d'image a utiliser
34 $param_img = getimagesize($img_ini);
35 $type_img = $param_img[2];
36 switch ($type_img) {
37 case 1:
38 $fct_creation_ext = 'imagecreatefromgif';
39 $fct_ecrire = 'imagegif';
40 break;
41 case 2:
42 $fct_creation_ext = 'imagecreatefromjpeg';
43 $fct_ecrire = 'imagejpeg';
44 break;
45 case 3:
46 $fct_creation_ext = 'imagecreatefrompng';
47 $fct_ecrire = 'imagepng';
48 break;
49 default:
50 return;
51 break;
52 }
53 // calculer le ratio a appliquer aux dimensions initiales
54 $l_ini = $param_img[0];
55 $h_ini = $param_img[1];
56 if ($l == 0 and $h == 0) {
57 $ratio = 1;
58 } else {
59 $ratio = ($l != 0 ? (abs($l_ini - $l) >= abs($h_ini - $h) ? $l / $l_ini : $h / $h_ini) : $h / $h_ini);
60 }
61 $img_nv = imagecreatetruecolor($l_ini * $ratio, $h_ini * $ratio);
62 $img_acopier = $fct_creation_ext($img_ini);
63
64 // gerer la transparence pour les images PNG (le mec qui a trouve ce code est genial! :-)
65 if ($type_img == 3) {
66 imagecolortransparent($img_nv, imagecolorallocate($img_nv, 0, 0, 0));
67 imagealphablending($img_nv, false);
68 imagesavealpha($img_nv, true);
69 }
70 imagecopyresampled($img_nv, $img_acopier, 0, 0, 0, 0, $l_ini * $ratio, $h_ini * $ratio, $l_ini, $h_ini);
71 // sauvegarder l'image et eventuellement detruire le fichier image initial
72 $fct_ecrire($img_nv, $img_ini);
73 imagedestroy($img_nv);
74 imagedestroy($img_acopier);
75 }