Kod u prilogu.
// if an image is uploaded
if(isset($_POST['submit']))
{
// the thumbnail height
$size = 150;
// the directory where the original uploaded image is saved
$filedir = 'images/';
// the directory where the thumbnail image is saved
$thumbdir = 'images2/';
// the prefix to be added to the original name to name the thumbnail
$prefix = 'small_';
// the file settings for the uploaded image
$mode = '0666';
$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];
if(isset($_FILES['image']['name']))
{
$prod_img = $filedir.$userfile_name;
$prod_img_thumb = $thumbdir.$prefix.$userfile_name;
move_uploaded_file($userfile_tmp, $prod_img);
chmod($prod_img, octdec($mode));
$sizes = getimagesize($prod_img);
$aspect_ratio = $sizes[1]/$sizes[0];
if ($sizes[1] <= $size)
{
$new_w = $sizes[0];
$new_h = $sizes[1];
}else{
$new_h = $size;
$new_w = abs($new_h/$aspect_ratio);
}
$destimg=imagecreatetruecolor($new_w,$new_h)
or die('Problem In Creating image');
$srcimg=imagecreatefromjpeg($prod_img)
or die('Problem In opening Source Image');
imagecopyresized($destimg,$srcimg,0,0,0,0,$new_w,$new_h,imagesx($srcimg),imagesy($srcimg))
or die('Problem In resizing');
imagejpeg($destimg,$prod_img_thumb,90)
or die('Problem In saving');
imagedestroy($destimg);
}
echo '
<a href="'.$prod_img.'">
<img src="'.$prod_img_thumb.'" width="'.$new_w.'" heigt="'.$new_h.'" alt="" />
</a>';
// show an upload form to upload and resize an image
}else{
echo '
<form method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="upload and resize image" />
</form>';
}
?>