$maxw || $ih > $maxh){ if ($iw>$maxw && $ih<=$maxh){//too wide, height is OK $proportion=($maxw*100)/$iw; $neww=$maxw; $newh=ceil(($ih*$proportion)/100); } else if ($iw<=$maxw && $ih>$maxh){//too high, width is OK $proportion=($maxh*100)/$ih; $newh=$maxh; $neww=ceil(($iw*$proportion)/100); } else {//too high and too wide if ($iw/$maxw > $ih/$maxh){//width is the bigger problem $proportion=($maxw*100)/$iw; $neww=$maxw; $newh=ceil(($ih*$proportion)/100); } else {//height is the bigger problem $proportion=($maxh*100)/$ih; $newh=$maxh; $neww=ceil(($iw*$proportion)/100); } } } else {//copy image even if not resizing $neww=$iw; $newh=$ih; } if (function_exists("imagecreatetruecolor")){//GD 2.0=good! $newImage=imagecreatetruecolor($neww, $newh); imagecopyresampled($newImage, $image, 0,0,0,0, $neww, $newh, $iw, $ih); } else {//GD 1.8=only 256 colours $newImage=imagecreate($neww, $newh); imagecopyresized($newImage, $image, 0,0,0,0, $neww, $newh, $iw, $ih); } return Array($newImage, $neww, $newh); } if ($_POST){ $maxw = (int)$_POST['maxw']; $maxh = (int)$_POST['maxh']; if ($_FILES['userfile'] && $_FILES['userfile']['size']){ //handle uploaded file $tmp = explode(".", $_FILES['userfile']['name']); $suffix = strtolower($tmp[count($tmp)-1]); //images only please if (!in_array($suffix, Array("jpeg", "jpg", "gif", "png")) || !@getImageSize($_FILES['userfile']['tmp_name'])){ die("You can only upload an image file (jpeg, jpg, gif or png.) Please press back and try again"); } $imSize = getImageSize($_FILES['userfile']['tmp_name']); if ($imSize[0] > 1280 || $imSize[1] > 1280) die("Your source image is larger than 1280 x 1280. Sorry, this is the largest allowed to save on server load."); //create image based on the suffix if ($suffix=="jpeg" || $suffix=="jpg"){ $im = ImageCreateFromJPEG($_FILES['userfile']['tmp_name']); } else if ($suffix == "gif"){ $im = ImageCreateFromGif($_FILES['userfile']['tmp_name']); } else if ($suffix == "png"){ $im = imagecreatefrompng($_FILES['userfile']['tmp_name']); } $w = ImageSX($im); $h = ImageSY($im); //function resizeImage($image, $iw, $ih, $maxw, $maxh) list($image) = resizeImage($im, $w, $h, $maxw, $maxh); imagejpeg($image); die(); } } ?>