434 |
jpm |
1 |
<?php
|
|
|
2 |
// Vérification de la présence d'un chemin vers l'image
|
|
|
3 |
if($_GET['img'] == '') {
|
|
|
4 |
exit;
|
|
|
5 |
}
|
|
|
6 |
|
|
|
7 |
// Récupération de paramêtres
|
|
|
8 |
$_image_ = urldecode( $_GET['img'] );
|
|
|
9 |
$_dossier = dirname($_image_).'/cache/';
|
|
|
10 |
$_fichier = pathinfo($_image_);
|
|
|
11 |
$_fichier['filename'] = trim(basename($_image_, $_fichier['extension']), '.');
|
|
|
12 |
$_width_min_ = intval($_GET['width']);
|
|
|
13 |
$_height_min_ = intval($_GET['height']);
|
|
|
14 |
$_quality_ = intval($_GET['quality']);
|
|
|
15 |
$_centrage = false;
|
|
|
16 |
if (isset($_GET['centrage'])) {
|
|
|
17 |
$_centrage = (bool)$_GET['centrage'];
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
// Création du dossier de cache
|
|
|
21 |
if (!is_dir($_dossier)) {
|
|
|
22 |
mkdir($_dossier);
|
|
|
23 |
}
|
|
|
24 |
// Création du nom du fichier de cache
|
|
|
25 |
$fichier_cache = $_dossier.$_fichier['filename'].'_w'.$_width_min_.'_q'.$_quality_.'.'.$_fichier['extension'];
|
|
|
26 |
// Recherche de la présence d'une image en cache ou création de celle-ci
|
|
|
27 |
if (file_exists($fichier_cache)) {
|
|
|
28 |
switch (strtolower($_fichier['extension'])) {
|
|
|
29 |
case 'jpg' :
|
|
|
30 |
header('Content-type: image/jpg');
|
|
|
31 |
Imagejpeg(ImageCreateFromJpeg($fichier_cache));
|
|
|
32 |
break;
|
|
|
33 |
case 'gif' :
|
|
|
34 |
header('Content-type: image/gif');
|
|
|
35 |
Imagegif(ImageCreateFromGif($fichier_cache));
|
|
|
36 |
break;
|
|
|
37 |
case 'png' :
|
|
|
38 |
header('Content-type: image/png');
|
|
|
39 |
Imagepng(ImageCreateFromPng($fichier_cache));
|
|
|
40 |
break;
|
|
|
41 |
}
|
|
|
42 |
} else {
|
|
|
43 |
// Calcul de la hauteur et de la largeur
|
|
|
44 |
$info = getimagesize($_image_);
|
|
|
45 |
if ($info[0] == '') {
|
|
|
46 |
exit();
|
|
|
47 |
}
|
|
|
48 |
$new_w = $_width_min_;
|
|
|
49 |
$new_h = (int)($info[1]*($new_w/$info[0]));
|
|
|
50 |
if(($_height_min_) AND ($new_h > $_height_min_)) {
|
|
|
51 |
$new_h = $_height_min_;
|
|
|
52 |
$new_w = (int)($info[0]*($new_h/$info[1]));
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
// Définition des points d'origine de destination
|
|
|
56 |
$dst_x = 0;
|
|
|
57 |
$dst_y = 0;
|
|
|
58 |
$dst_l = $new_w;
|
|
|
59 |
$dst_h = $new_h;
|
|
|
60 |
if ($_centrage != false) {
|
|
|
61 |
$dst_x = (int)(($_width_min_ - $new_w) / 2);
|
|
|
62 |
$dst_y = (int)(($_height_min_ - $new_h) / 2);
|
|
|
63 |
$dst_l = $_width_min_;
|
|
|
64 |
$dst_h = $_height_min_;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
// Création de l'image
|
|
|
68 |
switch (strtolower($_fichier['extension'])) {
|
|
|
69 |
case 'jpg' :
|
|
|
70 |
header("Content-type: image/jpg");
|
|
|
71 |
$dst_img = imagecreatetruecolor($dst_l, $dst_h);
|
|
|
72 |
$c_fond = imagecolorallocate($dst_img, 255, 255, 255);
|
|
|
73 |
imagefill($dst_img, 0, 0, $c_fond);
|
|
|
74 |
$src_img = ImageCreateFromJpeg($_image_);
|
|
|
75 |
imagecopyresampled($dst_img,$src_img,$dst_x,$dst_y,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
|
|
|
76 |
$img_cache = Imagejpeg($dst_img, $fichier_cache, $_quality_);
|
|
|
77 |
$img = Imagejpeg($dst_img, '', $_quality_);
|
|
|
78 |
break;
|
|
|
79 |
case 'gif' :
|
|
|
80 |
header("Content-type: image/gif");
|
|
|
81 |
$dst_img=ImageCreate($new_w,$new_h);
|
|
|
82 |
$src_img=ImageCreateFromGif($_image_);
|
|
|
83 |
ImagePaletteCopy($dst_img,$src_img);
|
|
|
84 |
ImageCopyResized($dst_img,$src_img,$dst_x,$dst_y,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
|
|
|
85 |
$img_cache = Imagegif($dst_img, $fichier_cache, $_quality_);
|
|
|
86 |
$img = Imagegif($dst_img,'', $_quality_);
|
|
|
87 |
break;
|
|
|
88 |
case 'png' :
|
|
|
89 |
header("Content-type: image/png");
|
|
|
90 |
$dst_img=ImageCreate($new_w,$new_h);
|
|
|
91 |
$src_img=ImageCreateFromPng($_image_);
|
|
|
92 |
ImagePaletteCopy($dst_img,$src_img);
|
|
|
93 |
ImageCopyResized($dst_img,$src_img,$dst_x,$dst_y,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
|
|
|
94 |
$img_cache = Imagepng($dst_img, $fichier_cache, $_quality_);
|
|
|
95 |
$img = Imagepng($dst_img,'', $_quality_);
|
|
|
96 |
break;
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
?>
|