Codice PHP:
<?php
//parametri
$no_img_src = "images/noimage.gif";
if ($_GET['file']!="") {
$file = $_GET['file'];
} else {
$file = $no_img_src;
}
$resize = $_GET['w'];
$altez = $_GET['h'];
$quality = 85;
if (isset($_GET["q"])) {
$quality = $_GET["q"];
}
$cosa = getimagesize($file);
//casi
if ($resize!="" && $altez=="") {
//se c'è la larghezza e non l'altezza
if ($resize>$cosa[0]) {
$width = $cosa[0];
$alte = $cosa[1];
} else {
$width = $resize;
$alte = (($resize*$cosa[1])/$cosa[0]);
}
} elseif ($resize=="" && $altez!="") {
//se c'è l'altezza e non la larghezza
if ($altez>$cosa[1]) {
$width = $cosa[0];
$alte = $cosa[1];
} else {
$width = (($altez*$cosa[0])/$cosa[1]);
$alte = $altez;
}
} elseif ($resize!="" && $altez!="") {
//se ci sono entrambe
if ($resize>$cosa[0]) {
$width = $cosa[0];
if ($altez>$cosa[1]) {
$alte = $cosa[1];
} else {
$alte = $altez;
$width = (($altez*$cosa[0])/$cosa[1]);
}
} else {
$width = $resize;
if ($altez>$cosa[1]) {
$alte = $cosa[1];
} else {
$alte = $altez;
$width = (($altez*$cosa[0])/$cosa[1]);
}
}
} elseif ($resize=="" && $altez=="") {
//se non ci sono entrambe
$width = $cosa[0];
$alte = $cosa[1];
}
/*
$width = ($resize < $cosa[0]) ? $resize : $cosa[0];
$alte = (!$altez) ? ceil(($cosa[1] * $width)/$cosa[0])+1 : $altez;
*/
//ricreo l'img
$min = imagecreatetruecolor($width, $alte);
switch($cosa['mime']){
case 'image/png':
header('Content-Type: image/png');
$im = imagecreatefrompng($file);
imagealphablending($min, false);
imagecopyresized($min, $im, 0, 0, 0, 0, $width, $alte, imageSx($im), imageSy($im));
imagesavealpha($min, true);
imagepng($min,'',8,PNG_ALL_FILTERS);
break;
case 'image/gif':
header('Content-Type: image/png');
$im = imagecreatefromgif($file);
imagealphablending($min, false);
$colorTransparent = imagecolorallocatealpha($min, 0, 0, 0, 127);
imagefill($min, 0, 0, $colorTransparent);
imagecopyresized($min, $im, 0, 0, 0, 0, $width, $alte, imageSx($im), imageSy($im));
imagesavealpha($min, true);
imagepng($min,'',8,PNG_ALL_FILTERS);
break;
case 'image/jpeg':
case 'image/jpg':
header('Content-Type: image/jpeg');
$im = imagecreatefromjpeg($file);
imagecopyresized($min, $im, 0, 0, 0, 0, $width, $alte, imageSx($im), imageSy($im));
imagejpeg($min,'',$quality);
break;
}
imagedestroy($min);
?>