143 lines
5.3 KiB
PHP
143 lines
5.3 KiB
PHP
<?
|
|
$config_dircache="images/cache/";
|
|
# Constants
|
|
if(isset($_GET['w']))
|
|
$maxwidth = $_GET['w'];
|
|
|
|
if(isset($_GET['h']))
|
|
$maxheight = $_GET['h'];
|
|
|
|
# Get image location
|
|
if(isset($_GET['src'])){
|
|
$image_path = str_replace(" ", "%20", $_GET['src']);
|
|
|
|
# Load image
|
|
$img = null;
|
|
$ext = strtolower(end(explode('.', $image_path)));
|
|
|
|
$filename_cache = md5(md5_file($image_path).md5($maxwidth."x".$maxheight));
|
|
|
|
if(!file_exists($config_dircache.$filename_cache.'.'.$ext)){
|
|
|
|
if($ext == 'jpg' || $ext == 'jpeg')
|
|
$img = @imagecreatefromjpeg($image_path);
|
|
else if($ext == 'png')
|
|
$img = @imagecreatefrompng($image_path);
|
|
else if($ext == 'gif')
|
|
$img = @imagecreatefromgif($image_path);
|
|
|
|
# If an image was successfully loaded, test the image for size
|
|
if($img){
|
|
|
|
# Get image size and scale ratio
|
|
$width = imagesx($img);
|
|
$height = imagesy($img);
|
|
|
|
if($width>$height){
|
|
$new_width=$maxwidth;
|
|
$new_height = round(($maxwidth*$height)/$width);
|
|
if($new_height>$maxheight) {
|
|
$new_width=round(($maxheight*$width)/$height);
|
|
$new_height=$maxheight;
|
|
}
|
|
}
|
|
elseif($height>$width){
|
|
$new_width = round(($maxheight*$width)/$height);
|
|
$new_height = $maxheight;
|
|
if($new_width>$maxwidth) {
|
|
$new_width = $maxwidth;
|
|
$new_height = round(($maxwidth*$height)/$width);
|
|
}
|
|
}
|
|
else{
|
|
if($maxwidth>$maxheight) {
|
|
$new_width = round(($maxheight*$width)/$height);
|
|
$new_height = $maxheight;
|
|
}
|
|
elseif($maxheight>$maxwidth) {
|
|
$new_width = $maxwidth;
|
|
$new_height = round(($maxwidth*$height)/$width);
|
|
}
|
|
else{ // sono uguali
|
|
$new_width = $new_height = $maxwidth;
|
|
}
|
|
}
|
|
|
|
# Create a new temporary image
|
|
if($ext=='jpg' || $ext=='jpeg'){
|
|
$tmp_img = imagecreatetruecolor($new_width, $new_height);
|
|
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
|
|
}
|
|
else if($ext=='png'){
|
|
$tmp_img = imagecreatetruecolor($new_width, $new_height);
|
|
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
|
|
/* $color_transparent = imagecolortransparent($img); //codice per la gestione delle trasparenze
|
|
$tmp_img = imagecreate($new_width, $new_height);
|
|
imagepalettecopy($tmp_img, $img);
|
|
imagefill($tmp_img, 0, 0, $color_transparent);
|
|
imagecolortransparent($tmp_img, $color_transparent);
|
|
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); */
|
|
}
|
|
else if($ext=='gif'){
|
|
$color_transparent = imagecolortransparent($img);
|
|
$tmp_img = imagecreate($new_width, $new_height);
|
|
imagepalettecopy($tmp_img, $img);
|
|
imagefill($tmp_img, 0, 0, $color_transparent);
|
|
imagecolortransparent($tmp_img, $color_transparent);
|
|
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
|
|
}
|
|
|
|
# Copy and resize old image into new image
|
|
$img = $tmp_img;
|
|
}
|
|
|
|
if ($ext=='jpg' || $ext=='jpeg'){
|
|
header("Content-type: image/jpeg");
|
|
//imagejpeg($img, $config_dircache.$filename_cache.'.'.$ext, 100);
|
|
imagejpeg($img);
|
|
imagedestroy($img);
|
|
}
|
|
else if ($ext=='png'){
|
|
header("Content-type: image/png");
|
|
//imagepng($img, $config_dircache.$filename_cache.'.'.$ext);
|
|
imagepng($img);
|
|
imagedestroy($img);
|
|
}
|
|
else if ($ext=='gif') {
|
|
header("Content-type: image/gif");
|
|
//imagegif($img, $config_dircache.$filename_cache.'.'.$ext);
|
|
imagegif($img);
|
|
imagedestroy($img);
|
|
}
|
|
else{/*/*Messaggio di errore. Impossibile visualizzare l'immagine.*/}
|
|
}
|
|
|
|
if ($ext == 'jpg' || $ext == 'jpeg')
|
|
$img = @imagecreatefromjpeg($config_dircache.$filename_cache.'.'.$ext);
|
|
else if ($ext == 'png')
|
|
$img = @imagecreatefrompng($config_dircache.$filename_cache.'.'.$ext);
|
|
else if ($ext == 'gif')
|
|
$img = @imagecreatefromgif($config_dircache.$filename_cache.'.'.$ext);
|
|
|
|
if ($ext == 'jpg' || $ext == 'jpeg'){
|
|
header("Content-type: image/jpeg");
|
|
imagejpeg($img, '', 95);
|
|
imagedestroy($img);
|
|
}
|
|
else{
|
|
if($ext=='png'){
|
|
header("Content-type: image/png");
|
|
imagepng($img);
|
|
imagedestroy($img);
|
|
}
|
|
else{
|
|
if($ext=='gif'){
|
|
header("Content-type: image/gif");
|
|
imagegif($img);
|
|
imagedestroy($img);
|
|
}
|
|
else {/*Messaggio di errore. Impossibile visualizzare l'immagine.*/}
|
|
}
|
|
}
|
|
}
|
|
?>
|