There is my actual script to resize image without distortion to generate a thumbnail and/or show a smaller to browser.
<?php
ob_start("ob_gzhandler");
$PHP_SELF=$_SERVER['PHP_SELF'];
include "include/errors.php"; $type=false;
function open_image ($file) {
global $type;
$size=getimagesize($file);
switch($size["mime"]){
case "image/jpeg":
$im = imagecreatefromjpeg($file); break;
case "image/gif":
$im = imagecreatefromgif($file); break;
case "image/png":
$im = imagecreatefrompng($file); break;
default:
$im=false;
break;
}
return $im;
}
$url = $_GET['url'];
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == filemtime($url))) {
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($url)).' GMT', true, 304); } else {
$image = open_image($url);
if ($image === false) { die ('Unable to open image'); }
$w = imagesx($image);
$h = imagesy($image);
if(isset($_GET['w']) && !isset($_GET['h'])){
$new_w=$_GET['w'];
$new_h=$new_w * ($h/$w);
} elseif (isset($_GET['h']) && !isset($_GET['w'])) {
$new_h=$_GET['h'];
$new_w=$new_h * ($w/$h);
} else {
$new_w=isset($_GET['w'])?$_GET['w']:560;
$new_h=isset($_GET['h'])?$_GET['h']:560;
if(($w/$h) > ($new_w/$new_h)){
$new_h=$new_w*($h/$w);
} else {
$new_w=$new_h*($w/$h);
}
}
$im2 = ImageCreateTrueColor($new_w, $new_h);
imagecopyResampled ($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $w, $h);
if(isset($_GET['blur'])){
$lv=$_GET['blur'];
for($i=0; $i<$lv;$i++){
$matrix=array(array(1,1,1),array(1,1,1),array(1,1,1));
$divisor = 9;
$offset = 0;
imageconvolution($im2, $matrix, $divisor, $offset);
}
}
if(isset($_GET['sharpen'])){
$lv=$_GET['sharpen'];
for($i=0; $i<$lv;$i++){
$matrix = array(array(-1,-1,-1),array(-1,16,-1),array(-1,-1,-1));
$divisor = 8;
$offset = 0;
imageconvolution($im2, $matrix, $divisor, $offset);
}
}
header('Content-type: image/jpeg');
$name=explode(".", basename($_GET['url']));
header("Content-Disposition: inline; filename=".$name[0]."_t.jpg");
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($url)) . ' GMT');
header("Cache-Control: public");
header("Pragma: public");
imagejpeg($im2);
}
?>