this little script resizes your uploaded image on the fly and saves it as thumbnail.

respects aspect ratio and doesn't distort your image

image upload and resize” makes use of the built-in PHP functions move_uploaded_file( ), chmod( ), octdec( ), getimagesize( ), abs( ), imagecreatetruecolor( ), imagecreatefromjpeg( ), function_exists( ), imagecopyresampled( ), imagesx( ), imagesy( ), Imagecopyresized( ), imagejpeg( ) and imagedestroy( ).

01 <?php
02 
03  
04 // if an image is uploaded
05 if(isset($_POST['submit']))
06 {
07     // the thumbnail height
08     $size = 150;
09  
10     // the directory where the original uploaded image is saved
11     $filedir = 'pics/';
12     // the directory where the thumbnail image is saved
13     $thumbdir = 'pics/';
14     // the prefix to be added to the original name to name the thumbnail
15     $prefix = 'small_';
16  
17     // the file settings for the uploaded image
18     $mode = '0666';
19     
20     $userfile_name = $_FILES['image']['name'];
21     $userfile_tmp = $_FILES['image']['tmp_name'];
22     $userfile_size = $_FILES['image']['size'];
23     $userfile_type = $_FILES['image']['type'];
24     
25     if(isset($_FILES['image']['name'])) 
26     {
27         $prod_img = $filedir.$userfile_name;
28  
29         $prod_img_thumb = $thumbdir.$prefix.$userfile_name;
30         move_uploaded_file($userfile_tmp, $prod_img);
31         chmod($prod_img, octdec($mode));
32         
33         $sizes = getimagesize($prod_img);
34  
35         $aspect_ratio = $sizes[1]/$sizes[0]; 
36  
37         if ($sizes[1] <= $size)
38         {
39             $new_w = $sizes[0];
40             $new_h = $sizes[1];
41         }else{
42             $new_h = $size;
43             $new_w = abs($new_h/$aspect_ratio);
44         }
45  
46         $dest = imagecreatetruecolor($new_w,$new_h)
47             or die('Problem In Creating image');
48         $src = imagecreatefromjpeg($prod_img)
49             or die('Problem In opening Source Image');
50         if(function_exists('imagecopyresampled'))
51         {
52             imagecopyresampled($dest,$src,0,0,0,0,$new_w,$new_h,imagesx($src),imagesy($src))
53             or die('Problem In resizing');
54         }else{
55             Imagecopyresized($dest,$src,0,0,0,0,$new_w,$new_h,imagesx($src),imagesy($src))
56             or die('Problem In resizing');
57         }
58         imagejpeg($dest,$prod_img_thumb,90)
59             or die('Problem In saving');
60         imagedestroy($dest);
61     }
62  
63     echo '
64     <a href="'.$prod_img.'">
65         <img src="'.$prod_img_thumb.'" width="'.$new_w.'" heigt="'.$new_h.'" alt="" />
66     </a>';
67  
68 // show an upload form to upload and resize an image
69 }else{
70  
71     echo '
72     <form method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
73         <input type="file" name="image" />
74         <input type="submit" name="submit" value="upload and resize image" />
75     </form>';
76 }
77  
78 ?>