Subversion Repositories Applications.wikini

Rev

Rev 6 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 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
//echo $fichier_cache;
27
// Recherche de la présence d'une image en cache ou création de celle-ci
28
if (file_exists($fichier_cache)) {
29
	switch (strtolower($_fichier['extension'])) {
30
		case 'jpg' :
31
			header('Content-type: image/jpg');
32
			Imagejpeg(ImageCreateFromJpeg($fichier_cache));
33
			break;
34
		case 'gif' :
35
			header('Content-type: image/gif');
36
			Imagegif(ImageCreateFromGif($fichier_cache));
37
			break;
38
		case 'png' :
39
			header('Content-type: image/png');
40
			Imagepng(ImageCreateFromPng($fichier_cache));
41
			break;
42
	}
43
} else {
44
	// Calcul de la hauteur et de la largeur
45
	$info = getimagesize($_image_);
46
	if ($info[0] == '') {
47
		exit();
48
	}
49
	$new_w = $_width_min_;
50
	$new_h = (int)($info[1]*($new_w/$info[0]));
51
	if(($_height_min_) AND ($new_h > $_height_min_)) {
52
		$new_h = $_height_min_;
53
		$new_w = (int)($info[0]*($new_h/$info[1]));
54
	}
55
 
56
	// Définition des points d'origine de destination
57
	$dst_x = 0;
58
	$dst_y = 0;
59
	$dst_l = $new_w;
60
	$dst_h = $new_h;
61
	if ($_centrage != false) {
62
		$dst_x = (int)(($_width_min_ - $new_w) / 2);
63
		$dst_y = (int)(($_height_min_ - $new_h) / 2);
64
		$dst_l = $_width_min_;
65
		$dst_h = $_height_min_;
66
	}
67
 
68
	// Création de l'image
69
	switch (strtolower($_fichier['extension'])) {
70
		case 'jpg' :
71
			header("Content-type: image/jpg");
72
			$dst_img = imagecreatetruecolor($dst_l, $dst_h);
73
			$c_fond = imagecolorallocate($dst_img, 255, 255, 255);
74
			imagefill($dst_img, 0, 0, $c_fond);
75
			$src_img = ImageCreateFromJpeg($_image_);
76
			imagecopyresampled($dst_img,$src_img,$dst_x,$dst_y,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
77
			$img_cache = Imagejpeg($dst_img, $fichier_cache, $_quality_);
78
			$img = Imagejpeg($dst_img, '', $_quality_);
79
			break;
80
		case 'gif' :
81
			header("Content-type: image/gif");
82
			$dst_img=ImageCreate($new_w,$new_h);
83
			$src_img=ImageCreateFromGif($_image_);
84
			ImagePaletteCopy($dst_img,$src_img);
85
			ImageCopyResized($dst_img,$src_img,$dst_x,$dst_y,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
86
			$img_cache = Imagegif($dst_img, $fichier_cache, $_quality_);
87
			$img = Imagegif($dst_img,'', $_quality_);
88
			break;
89
		case 'png' :
90
			header("Content-type: image/png");
91
			$dst_img=ImageCreate($new_w,$new_h);
92
			$src_img=ImageCreateFromPng($_image_);
93
			ImagePaletteCopy($dst_img,$src_img);
94
			ImageCopyResized($dst_img,$src_img,$dst_x,$dst_y,0,0,$new_w,$new_h,ImageSX($src_img),ImageSY($src_img));
95
			$img_cache = Imagepng($dst_img, $fichier_cache, $_quality_);
96
			$img = Imagepng($dst_img,'', $_quality_);
97
			break;
98
	}
99
}
100
?>