<?php

if(isset($_POST['Submit'])) {
  $image=$_FILES['image']['name'];
  $filename = stripslashes($image);
  $copied = copy($_FILES['image']['tmp_name'], "images/" . $filename);
  echo "nice";
  
  $source_img = imagecreatefromjpeg($_FILES["image"]["tmp_name"]);
  if (!$source_img) {
	echo "could not create image from jpg";
  }
  $new_w = 120;
	$new_h = 120;
	
	$orig_w = imagesx($source_img);
	$orig_h = imagesy($source_img);
	
	$w_ratio = ($new_w / $orig_w);
	$h_ratio = ($new_h / $orig_h);
	
	if ($orig_w > $orig_h ) {//landscape
		$crop_w = round($orig_w * $h_ratio);
		$crop_h = $new_h;
	} elseif ($orig_w < $orig_h ) {//portrait
		$crop_h = round($orig_h * $w_ratio);
		$crop_w = $new_w;
	} else {//square
		$crop_w = $new_w;
		$crop_h = $new_h;
	}
	$dest_img = imagecreatetruecolor($new_w,$new_h);
    imagecopyresampled($dest_img, $source_img, 0 , 0 , 0, 0, $crop_w, $crop_h, $orig_w, $orig_h);
    
    if(imagejpeg($dest_img, "images/thumb_".$_FILES['image']['name'], 100)) {
	  imagedestroy($dest_img);
	  imagedestroy($source_img);
	} else {
		echo "could not make thumbnail image";
	}
}
 
 ?>

 <!--next comes the form, you must set the enctype to "multipart/frm-data" and use an input type "file" -->
 <form name="newad" method="post" enctype="multipart/form-data"  action="">
 <table>
 	<tr><td><input type="file" name="image"></td></tr>
 	<tr><td><input name="Submit" type="submit" value="Upload image"></td></tr>
 </table>	
 </form>