[PLUGINS] +set de base
[lhc/web/www.git] / www / plugins / odt2spip_30 / 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")) return;
15
16 /**
17 * retailler une image : (ne gère que les images GIF, JPG et PNG)
18 *
19 * @internal Gestion de la transparence des PNG : code de matt1walsh@gmail.com sur {@link http://fr2.php.net/manual/fr/function.imagecopyresampled.php}
20 * @param string $img_ini Le fichier à retailler
21 * @param int $l Largeur max de l'image finale
22 * @param int $h Hauteur max
23 * @return string Le message d'erreur en cas de problème
24 */
25 function inc_odt2spip_retailler_img($img_ini, $l = 0, $h = 400){
26 if (!file_exists($img_ini)) {
27 return 'Le fichier ' . $img_ini . ' n\'existe pas';
28 }
29 // determiner le type de fonction de creation d'image a utiliser
30 $param_img = getimagesize($img_ini);
31 $type_img = $param_img[2];
32 switch ($type_img) {
33 case 1 :
34 $fct_creation_ext = 'imagecreatefromgif';
35 $fct_ecrire = 'imagegif';
36 break;
37 case 2 :
38 $fct_creation_ext = 'imagecreatefromjpeg';
39 $fct_ecrire = 'imagejpeg';
40 break;
41 case 3 :
42 $fct_creation_ext = 'imagecreatefrompng';
43 $fct_ecrire = 'imagepng';
44 break;
45 default :
46 return;
47 break;
48 }
49 // calculer le ratio a appliquer aux dimensions initiales
50 $l_ini = $param_img[0];
51 $h_ini = $param_img[1];
52 $ratio = ($l != 0 ? (abs($l_ini - $l) >= abs($h_ini - $h) ? $l / $l_ini : $h / $h_ini) : $h / $h_ini);
53 $img_nv = imagecreatetruecolor($l_ini * $ratio, $h_ini * $ratio);
54 $img_acopier = $fct_creation_ext($img_ini);
55
56 // gerer la transparence pour les images PNG (le mec qui a trouve ce code est genial! :-)
57 if ($type_img == 3) {
58 imagecolortransparent($img_nv, imagecolorallocate($img_nv, 0, 0, 0));
59 imagealphablending($img_nv, false);
60 imagesavealpha($img_nv, true);
61 }
62 imagecopyresampled($img_nv, $img_acopier, 0, 0, 0, 0, $l_ini * $ratio, $h_ini * $ratio, $l_ini, $h_ini);
63 // sauvegarder l'image et eventuellement detruire le fichier image initial
64 $fct_ecrire($img_nv, $img_ini);
65 imagedestroy($img_nv);
66 imagedestroy($img_acopier);
67 }
68
69 ?>