Images From Database Help

I have spent the day making a sort of photo gallery in PHP. Well it’s actually more of a multi-photogallery because they are divided up into sections, but that’s besides the point. With some help, I have been able to store user uploaded images in a mySQL database. I have also figured out how to display images.

Herein lies the problem. I created thumbnails with the GD Library, and stored the thumbnails in a column called ‘thumbnail’. Then stored the full sized image in the column ‘image’. The full sized image comes out perfectly. The problem is that the thumbnail prints out “Resource id #6”.

I would appreciate it a ton if someone could help me out.

Here is the code that I am using to withdraw the images from the database:


<?PHP
include "connect.php";
header("Content-type: image/jpeg");

$image = 1;

if($image) {
	if($thumb) {
		$query = "SELECT thumbnail FROM showcaseGal WHERE id='$image' LIMIT 1";
		$result = mysql_query($query, $myLink);
		$thumbnail = mysql_fetch_row($result);
		echo "$thumbnail[0]";
	} else {
		$query = "SELECT image FROM showcaseGal WHERE id='$image' LIMIT 1";
		$result = mysql_query($query, $myLink);
		$image = mysql_fetch_row($result);
		echo "$image[0]";
	}
}

It also occurred to me that the problem could be how I inserted the thumbnail into the database, so that code is below:


	$UPLOAD_TYPES['JPG'] = 1;
	$UPLOAD_TYPES['JPEG'] = 1;
	$UPLOAD_SIZES['max'] = 1000000;
	$UPLOAD_SIZES['min'] = 0;
	for($i=1; $i<=$numPics; $i++) {
		//Put the information into the database (ordered numerically)
		$image = "image".$i;
		$myVar = "$"."title".$i;
		eval("\$title = \"$myVar\";");
		$myVar = "$"."caption".$i;
		eval("\$caption = \"$myVar\";");
		if($image) {
		//if something was entered into the box
			$intResult = verify_uploaded_file($HTTP_POST_FILES[$image]['name'], $HTTP_POST_FILES[$image]['size']);
			
			if($intResult != 1) {
				$name = $HTTP_POST_FILES[$image]['name'];
			} else {
				//Met Criterea, so put it in the database
				if(is_uploaded_file($HTTP_POST_FILES[$image]['tmp_name'])) {
					
					$file = fopen($HTTP_POST_FILES[$image]['tmp_name'], 'r');
					$file_contents = fread($file, filesize($HTTP_POST_FILES[$image]['tmp_name']));
					fclose($file);
					
					$file_contents = AddSlashes($file_contents);
					
					define(MAX_WIDTH, 150);
					define(MAX_HEIGHT, 150);
					
					$thumb = imagecreatefromjpeg($HTTP_POST_FILES[$image]['tmp_name']);
					
					if ($thumb) {
 						# Get image size and scale ratio
						$width = imagesx($thumb);
						$height = imagesy($thumb);
						$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

					    # If the image is larger than the max shrink it
						if ($scale < 1) {
							$new_width = floor($scale*$width);
							$new_height = floor($scale*$height);

					        # Create a new temporary image
							$tmp_img = imagecreatetruecolor($new_width, $new_height);

					        # Copy and resize old image into new image
							imagecopyresized($tmp_img, $thumb, 0, 0, 0, 0,
							$new_width, $new_height, $width, $height);
							imagedestroy($thumb);
							$thumb = $tmp_img;
						}
					}
					$query = "INSERT INTO showcaseGal SET client='$client', title='$title', caption='$caption', thumbnail='$thumb', image='$file_contents'";
					$result = mysql_query($query, $myLink);
					$success++;
				}
			}
		}
	}

Thanks a ton :slight_smile:

Marz helped me. Turns out you have to save it to a temporary file before you can put it in the database.

Thanks Marz :slight_smile: