Build An Automated PHP Gallery System In Minutes

the patent has expired and I seem to recall that gif support is being reintroduced into the bundled GD library , perhaps as early as 4.3.3 ?

That would be great …

Nope GD could legally support GIF at the moment as the patent has expired although as stated on the GD website earlier this year there is to be no GIF support until early 2004 for some reason or other that I cannot remember…

Though I think they’re waiting on something or other before they release the GIF support in the current GD releases… ?

Yeah great article im working through it but having a coupla issues I’m working through

What kind of issues ?

I’m getting this error on line 34 of upload.php

Fatal error: Maximum execution time of 60 seconds exceeded in /home/nathanr/public_html/radiorallyv2/pic/upload.php on line 34
this is line 34

while($counter <= count($photos_uploaded)) { 

and this is the block of code around line 34

 while($counter <= count($photos_uploaded)) {
if($photos_uploaded['size'][$counter] > 0) {
if(!array_key_exists($photos_uploaded['type'][$counter], $photo_types)) {
$result_final .= 'File ' . ($counter + 1) . ' is not a photo<br />';
} else {
// Great the file is an image, we will add this file
}
}
}

this is my whole script

<? include_once("../header.php" ); ?> 
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr> 
<td colspan="2"><img src="../img/banner2.jpg" border="0" vspace="0" hspace="0" name="banner"></td>
</tr>
<tr> 
<td valign=top width=16% class="menu"> 
<? include_once("../menu2.php" ); ?>
</td>
<td width="*%" valign=top> 
<?
// include the database specific information
include_once("../inc/db.php" );
// make a connection to the rrc database
dbConnect();
$images_dir = 'photos';
// Fetch the image array sent by preuploaed.php
$photos_uploaded = $_FILES['photo_filename'];
// Fetch the image caption array
$photo_captions = $_POST['photo_captions'];
$photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/x-png' => 'png'
);
while($counter <= count($photos_uploaded) ) {
if($photos_uploaded['size'][$counter] > 0) {
if(!array_key_exists($photos_uploaded['type'][$counter], $photo_types)) {
$result_final .= 'File ' . ($counter + 1) . ' is not a photo<br />';
} else {
// Great the file is an image, we will add this file
}
}
}
$query = "INSERT INTO gallery_photos (photo_filename, photo_caption, photo_category) VALUES ('0','" . $photo_captions[$counter] . "','" . $_POST['category'] . "')";
mysql_query($query);
$new_id = mysql_insert_id(); // New Id generated
// Get the filetype of the uploaded file
$filetype = $photos_uploaded['type'][$counter];
// Get the extension for the new name
$extension = $known_photo_types[$filetype];
// Generate a new name
$filename = "$new_id.$extension";
// let's update the filename now
$query = "UPDATE gallery_photos SET photo_filename = '$filename' WHERE photo_id = '$new_id'";
mysql_query($query);
copy($photos_uploaded['tmp_name'][$counter], $images_dir . '/' . $filename);
$size = GetImageSize($images_dir . "/" . $filename);
// Settings 
$Config_tbwidth_wide = 100; // width of wide image 
$Config_tbheight_wide = 75; // height of wide image 
$Config_tbwidth_tall = 75; // width of tall image 
$Config_tbheight_tall = 100; // height of tall image 

// The code 
if($size[0] > $size[1]) 
{ 
$thumbnail_width = $Config_tbwidth_wide; 
$thumbnail_height = (int)($Config_tbwidth_wide * $size[1] / $size[0]); 
if($thumbnail_height > $Config_tbheight_wide) 
{ 
$thumbnail_height = $Config_tbheight_wide; 
$thumbnail_width = (int)($Config_tbheight_wide * $size[0] / $size[1]); 
} 
} 
else 
{ 
$thumbnail_width = (int)($Config_tbheight_tall * $size[0] / $size[1]); 
$thumbnail_height = $Config_tbheight_tall; 
if($thumbnail_width > $Config_tbwidth_tall) 
{ 
$thumbnail_width = $Config_tbwidth_tall; 
$thumbnail_height = (int)($Config_tbwidth_tall * $size[1] / $size[0]); 
} 
} 
$gd_function_suffix = array (
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/gif' => 'GIF',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG'
);
$function_suffix = $gd_function_suffix[$filetype];
$function_to_read = 'ImageCreateFrom' . $function_suffix;
$function_to_write = 'Image' . $function_suffix;
// Read the source file
$source_handle = $function_to_read($images_dir . '/' . $filename );
if ($source_handle) {
// Let's create a blank image for the thumbnail
$destination_handle = ImageCreate($thumbnail_width, $thumbnail_height);

// Now we resize it
ImageCopyResized($destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]);
}
// Lets save the thumbnail
$function_to_write($destination_handle, $images_dir . '/tb_' . $filename);
ImageDestroy($destination_handle);
?>
</td>
</tr>
</table>
<? include_once("../footer.php" ); ?>

The while loop will never end as the counter is not being incremented. It looks like the code is not correctly formulated. You might want to download the archive with the article, from here

also, if you are uploading many images at once, you would like to reset the default execution time limit of 30 seconds,

set_time_limit(0);

Patent has expired in the U.S. The later date is when the Patent expires world wide (IIRC).

my preupload.php

 <? include_once("../header.php" ); ?> 
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr> 
<td colspan="2"><img src="../img/banner2.jpg" border="0" vspace="0" hspace="0" name="banner"></td>
</tr>
<tr> 
<td valign=top width=16% class="menu"> 
<? include_once("../menu2.php" ); ?>
</td>
<td width="*%" valign=top> 
<?
// include the database specific information
include_once("../inc/db.php" );
// make a connection to the rrc database
dbConnect();
$images_dir = 'photos';
// initialization
$photo_upload_fields = "";
$counter = 1;
// Default number of fields
$number_of_fields = 5;
// If we want more fields, then use, preupload.php?number_of_fields=20
if( $_GET['number_of_fields'])
$number_of_fields = (int) ($_GET['number_of_fields']);
// Firstly Lets build the category list
$query = "SELECT category_id,category_name FROM gallery_category";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$photo_category_list .= <<<__HTML_END
<option value="$row[0]">$row[1]</option>\

__HTML_END;
}
mysql_free_result($result);
// Lets build the Image Uploading fields
while($counter <= $number_of_fields) {
$photo_upload_fields .= <<<__HTML_END
<tr><td>
Photo ($counter):
<input name='photo_filename[]' type='file' />
</td></tr>
<tr><td>
Caption:&nbsp;&nbsp;&nbsp;
<textarea name='photo_caption[]' cols='30' rows='1'></textarea>
</td></tr>
__HTML_END;
$counter++;
}
// Final Output
echo <<<__HTML_END
<form enctype='multipart/form-data' action='upload.php' method='post' name='upload_form'>
<table width='90%' border='0' align='center' style='width: 90%;'>
<tr><td>
Select Category
<select name='category'>
$photo_category_list
</select>
</td></tr>
<tr><td>
<p>&nbsp;</p>
</td></tr>

<!--Insert the image fields here -->
$photo_upload_fields
<tr><td>
<input type='submit' name='submit' value='Add Photos' />
</td></tr>
</table>
</form>
__HTML_END;
?>
</td>
</tr>
</table>
<? include_once("../footer.php" ); ?>

and my upload.php

 <? include_once("../header.php" ); ?> 
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr> 
<td colspan="2"><img src="../img/banner2.jpg" border="0" vspace="0" hspace="0" name="banner"></td>
</tr>
<tr> 
<td valign=top width=16% class="menu"> 
<? include_once("../menu2.php" ); ?>
</td>
<td width="*%" valign=top> 
<?
// include the database specific information
include_once("../inc/db.php" );
// make a connection to the rrc database
dbConnect();
$images_dir = 'photos';
// initialization
$result_final = "";
$counter = 0;
// list of known photo types
$photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/x-png' => 'png'
);
// GD Function suffix
$gd_function_suffix = array (
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/gif' => 'GIF',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG'
);
// Fetch the image array sent by preuploaed.php
$photos_uploaded = $_FILES['photo_filename'];
// Fetch the image caption array
$photo_caption = $_POST['photo_caption'];
while($counter <= count($photos_uploaded) ) {
if($photos_uploaded['size'][$counter] > 0) {
if(!array_key_exists($photos_uploaded['type'][$counter], $photo_types)) {
$result_final .= "File " . ($counter + 1) . " is not a photo<br />";
} else {
// Great the file is an image, we will add this file
$query = "INSERT INTO gallery_photos (photo_filename, photo_caption, photo_category) VALUES ('0','" . $photo_captions[$counter] . "','" . $_POST['category'] . "')";
mysql_query($query);
$new_id = mysql_insert_id(); // New Id generated
// Get the filetype of the uploaded file
$filetype = $photos_uploaded['type'][$counter];
// Get the extension for the new name
$extension = $known_photo_types[$filetype];
// Generate a new name
$filename = "$new_id.$extension";
// let's update the filename now
$query = "UPDATE gallery_photos SET photo_filename = '$filename' WHERE photo_id = '$new_id'";
mysql_query($query);
// Store the original file
copy($photos_uploaded['tmp_name'][$counter], $images_dir . "/" . $filename);
// Let's go the thumbnail size
$size = GetImageSize($images_dir . "/" . $filename);
// Settings 
$Config_tbwidth_wide = 100; // width of wide image 
$Config_tbheight_wide = 75; // height of wide image 
$Config_tbwidth_tall = 75; // width of tall image 
$Config_tbheight_tall = 100; // height of tall image 

// The code 
if($size[0] > $size[1]) 
{ 
$thumbnail_width = $Config_tbwidth_wide; 
$thumbnail_height = (int)($Config_tbwidth_wide * $size[1] / $size[0]); 
if($thumbnail_height > $Config_tbheight_wide) 
{ 
$thumbnail_height = $Config_tbheight_wide; 
$thumbnail_width = (int)($Config_tbheight_wide * $size[0] / $size[1]); 
} 
} 
else 
{ 
$thumbnail_width = (int)($Config_tbheight_tall * $size[0] / $size[1]); 
$thumbnail_height = $Config_tbheight_tall; 
if($thumbnail_width > $Config_tbwidth_tall) 
{ 
$thumbnail_width = $Config_tbwidth_tall; 
$thumbnail_height = (int)($Config_tbwidth_tall * $size[1] / $size[0]); 
} 
} 
// Build Thumbnail with GD 1.x.x, you can use the other methods described too
$function_suffix = $gd_function_suffix[$filetype];
$function_to_read = "ImageCreateFrom".$function_suffix;
$function_to_write = "Image".$function_suffix;
// Read the source file
$source_handle = $function_to_read($images_dir."/".$filename);
if ($source_handle) {
// Let's create a blank image for the thumbnail
$destination_handle = ImageCreate($thumbnail_width, $thumbnail_height);

// Now we resize it
ImageCopyResized($destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]);
}
// Lets save the thumbnail
$function_to_write($destination_handle, $images_dir."/tb_".$filename);
ImageDestroy($destination_handle);
$result_final .= "<img src="".$images_dir. "/tb_".$filename.""/> File ".($counter+1)." Added<br />";
}
}
$counter++;
}
// Print Result
echo <<<__HTML_END
$result_final
__HTML_END;
?>
</td>
</tr>
</table>
<? include_once("../footer.php" ); ?>

I have been playing around and I can upload 3 images at once with comments and all is completed properly and all works and is entered into the database but I do more than three then it has a sad and displays the page cannot be displayed error

Hello,

Im getting a parse error from this line:

// Firstly Lets build the Category List
$result = mysql_query(‘SELECT category_id,category_name FROM gallery_category’);
while($row = mysql_fetch_array($result)) {
$photo_category_list .= <<<__HTML_END
<option value=“$row[0]”>$row[1]</option>\

__HTML_END;
}
mysql_free_result( $result );

I have no idea what that means!

Jake

it pulls the categories from the db and places them in a drop down list.

the total size of files you can upload at one go is limited to the value set in your php.ini for, upload_max_filesize

By default it is set to 2M (= 2 MB)

at which line no. do you get the error?

That was it Sweatje :smiley: Couldn’t exactly remember what it was that’s all :slight_smile:

Nah the size of all images is like 500k i check that mate

now that I got my gallery finished and completed i had images that were pulled from the old files that contained the images but now its all in the db how do i modify this code as so the images display themselves again?

 	 <?	 $dirname="./pic/photos" ;
	  $files = array(); 
		$dir = opendir( $dirname );
	  while( $file = readdir( $dir ) ) {
		 if (eregi("\\.jpe?g$", $file) || 
			 eregi("\\.gif$", $file) ||  
			 eregi("\\.png$", $file)) {
			 $files[] = $file; 
		 }
	  }
   ?>
	  <?
		$pictures = $files;	
	 shuffle($pictures);
  ?>

and the display one

  <?
					for ($i = 0; $i < 3; $i++)
					{
						echo "<td align=center valign=middle><a href=""\\"./pic/photos/$pictures[$i]\\" target=\\"_blank\\"><img src=""\\"./pic/photos/";
						echo $pictures[$i];
   echo "\\" width=180 heigh=180 alt=$pictures></a></td>";
					}
				?>

I want to use the same thumbs nails from the db which are in a folder but it doesnt have a name have everyone elses been like that too?

hmm … well i am not sure what you are trying to say … can you explain a little …

Its ok i got itn working now

great, do post your gallery url (if its public) …

sure is http://radiorally.lionslair.net.au/goto/list_all_cats

users can add their own pics and categories the admin stuff i wil do later in the meantime ill just edit it through phpmyadmin :slight_smile:

Nice tutorial, but it is a bit of re-inventing the wheel. Gallery is open source, full featured and supported by a huge community:
http://gallery.menalto.com/index.php
It’s always good to improve skills, but why not stand on the shoulders of others and make something even better?