I have modified the previous code for uploading images and creating the gallery automatically.

Visit Demo

The image upload interface will be displayed when we add the following code.I have named it as 'imageUpload.php'.

Note : to run this code we need to create the following file and folder structure

--imageUpload.php
--upload.php
--images(folder)
-----thumbnail(folder inside images)


Code : imageUpload.php
</code>

<html>
<head><title>Multiple Picture Upload</title>
</head><body>
<table border="0" cellspacing="3" cellpadding="15" align="center">
<tbody>
<form method="post" action="upload.php" enctype="multipart/form-data" name="form">
<tr>
<td><?php echo $change; ?> </td>
</tr>
<tr>
<td> <input size="25" name="file" type="file" /></td>
</tr>
<tr>
<td> <input size="25" name="file2" type="file" /></td>
</tr>
<tr>
<td> <input size="25" name="file3" type="file" /></td>
</tr>
<tr>
<td> <input size="25" name="file4" type="file" /></td>
</tr>
<tr>
<td> <input size="25" name="file5" type="file" /></td>
</tr>
<tr>
<td>Image maximum size&nbsp;&nbsp;<b>400 </b>&nbsp;&nbsp;kb</td>
</tr>
<tr>
<td><input type="submit" value="Upload" name="Submit"/></td>
</tr>
</tbody>
</form>
</table></body>
</html>

<code>



The uploading task will be done by the 'upload.php' file
Code : upload.php

</code>

<?php
error_reporting(0);
require_once('imageClass.php');

$change="";

//set the maximum size of the images can be uploaded
define ("MAX_SIZE","400");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) {
return "";
}
$l = strlen($str)-$i;
$ext = substr($str,$i,$l);
return $ext;
}

$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST") {
global $files;
$files = array('file','file2','file3','file4','file5');

for($m=0; $m<5; $m++ ) {

$file = $files[$m];

$image =$_FILES["$file"]["name"];
$uploadedfile = $_FILES["$file"]["tmp_name"];

if ($image) {
$filename = stripslashes($_FILES["$file"]["name"]);
$extension = getExtension($filename);
$extension = strtolower($extension);

if (($extension != ".jpg") && ($extension != ".jpeg") && ($extension != ".png") && ($extension != ".gif")) {

$change='<div>Unknown Image extension </div> ';
$errors=1;
}
else {
$size=filesize($_FILES["$file"]["tmp_name"]);
if ($size > MAX_SIZE*1024) {
$change='<div>You have exceeded the size limit!</div> ';
$errors=1;
}
else {

if($extension==".jpg" || $extension==".jpeg" ) {
$uploadedfile = $_FILES["$file"]["tmp_name"];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension==".png") {
$uploadedfile = $_FILES["$file"]["tmp_name"];
$src = imagecreatefrompng($uploadedfile);
}
else if($extension==".gif") {
$src = imagecreatefromgif($uploadedfile);
}
else {
$src = imagecreatefromjpeg($uploadedfile);
}

list($width,$height)=getimagesize($uploadedfile);

$newwidth1=$width;
$newheight1=$height;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp1,$src,0,0,0,0,$width,$height,$width,$height);

$filename1 = "images/". $_FILES["$file"]["name"];
if($extension == '.jpg' || $extension == '.jpeg') {
imagejpeg($tmp1,$filename1,100);
}
if($extension == '.png' ) {
imagepng($tmp1,$filename1,100);
}
if($extension == '.gif' ) {
imagegif($tmp1,$filename1,100);
}

$image = new Image();
$filename = "images/". $_FILES["$file"]["name"];
$info = pathinfo($filename1);
$imageName =  basename($filename,'.'.$info['extension']);
$image->load($filename);
$image->resize(100,100);
$image->save($imageName);

imagedestroy($src);
imagedestroy($tmp1);
}
}
}
}
}

header("location:gallery.php");

<code>



Here we have to create thumbnails.Image Class will do this stuff.
imageClass.php

</code>
<?php

class Image {

var $image;
var $image_type;

function load($filename) {
$image_info = getimagesize($filename);
if($image_info['mime'] == 'image/jpeg' ) {

$this->image_type = 'jpg';
$this->image = imagecreatefromjpeg($filename);
} elseif($image_info['mime'] == 'image/gif'  ) {
$this->image_type = 'gif';
$this->image = imagecreatefromgif($filename);
} elseif($image_info['mime']== 'image/png' ) {
$this->image_type = 'png';
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $compression=75) {
if( $this->image_type == 'jpg') {
imagejpeg($this->image,'images/thumbnail/thumb_'.$filename.'.'.$this->image_type,$compression);
} elseif( $this->image_type == 'gif' ) {
imagegif($this->image,'images/thumbnail/thumb_'.$filename.'.'.$this->image_type);
} elseif( $this->image_type == 'png') {
imagepng($this->image,'images/thumbnail/thumb_'.$filename.'.'.$this->image_type);
}
}

function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image,$this->image, 0, 0, 0, 0, $width, $height,imagesx($this->image),imagesy($this->image));
$this->image = $new_image;
}
}
<code>



Then we should create the gallery

Code : gallery.php

</code>

<html>
<head>
<style type="text/css">
img { border: 1px solid #000 }

#gallery img {
margin: 10px;
width: 85px;
height: 85px
}

#enlarge img {
width: 200px;
height: 200px;
}

</style>
</head>

<body>

<table>
<tr width="500">
<td width="45%" align="center">
<div id="enlarge">
<?php
$handle=opendir("images");

while (($file = readdir($handle))!==false) {
if(($file == '.' )|| ($file== '..') || ($file== 'thumbnail')) {
continue;
}
echo '<img name="LargeImage" src="images/';
echo $file;
echo '" />';
break;

}
?>

</div>
</td>

<td cellspacing=50>
<a href="imageUpload.php">Upload Images</a>
<div id="gallery">

<?php

// open the current directory by opendir
$handle=opendir("images");
$i == 2;
while (($file = readdir($handle))!==false) {

if(($file == '.' )|| ($file== '..') || ($file== 'thumbnail')) {
continue;

}
echo '<a href="#" onclick="LargeImage.src=';
echo "'images/$file'";
echo '"><img src="images/thumbnail/thumb_'.$file.'" /></a>';

}

closedir($handle);
?>

</div>
</td>
</tr>
</table>

</body>
</html>

<code>