01<?php 02 03 04// if an image is uploaded 05if(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 25if(isset($_FILES['image']['name'])) 26{ 27$prod_img=$filedir.$userfile_name; 28 29$prod_img_thumb=$thumbdir.$prefix.$userfile_name; 30move_uploaded_file($userfile_tmp,$prod_img); 31chmod($prod_img,octdec($mode)); 32 33$sizes=getimagesize($prod_img); 34 35$aspect_ratio=$sizes[1]/$sizes[0]; 36 37if($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) 47ordie('Problem In Creating image'); 48$src=imagecreatefromjpeg($prod_img) 49ordie('Problem In opening Source Image'); 50if(function_exists('imagecopyresampled')) 51{ 52imagecopyresampled($dest,$src,0,0,0,0,$new_w,$new_h,imagesx($src),imagesy($src)) 53ordie('Problem In resizing'); 54}else{ 55Imagecopyresized($dest,$src,0,0,0,0,$new_w,$new_h,imagesx($src),imagesy($src)) 56ordie('Problem In resizing'); 57} 58imagejpeg($dest,$prod_img_thumb,90) 59ordie('Problem In saving'); 60imagedestroy($dest); 61} 62 63echo' 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 71echo' 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?>