Specific Image gallery for individual user

Post here is you are having problems with any of the tutorials.
Post Reply
tom777duffy
Posts: 1
Joined: Fri Mar 02, 2012 4:53 pm

Specific Image gallery for individual user

Post by tom777duffy »

So I followed your Image Gallery tutorial and got it full working and im very pleased with it.

But i just wanted to make a query on how I would go about changing your code to only display images
that relate to a specific user logged in? Just a quick point in the right direction would be great! :)
here is my working gallery.php code
						
<?php
if (isset($_GET['img'])){
	//make thumbnail
	if(file_exists($_GET['img'])){
		ignore_user_abort(true);
		set_time_limit(120);
		ini_set('memory_limit', '512M');
		
		$src_size = getimagesize($_GET['img']);
		
		if ($src_size === false){
			die('That does not look like an image.');
		}
		
		$thumb_width	= 200;
		$thumb_height	= 150;
		
		if ($src_size['mime'] === 'image/jpeg'){
			$src = imagecreatefromjpeg($_GET['img']);
		}else if ($src_size['mime'] === 'image/png'){
			$src = imagecreatefrompng($_GET['img']);
		}else if ($src_size['mime'] === 'image/gif'){
			$src = imagecreatefromgif($_GET['img']);
		}
		
		$src_aspect = round (($src_size[0] / $src_size[1]), 1);
		$thumb_aspect = round(($thumb_width / $thumb_height), 1);
		
		if ($src_aspect < $thumb_aspect){
			//higher than thumb
			$new_size = array ($thumb_width, ($thumb_width / $src_size[0]) * $src_size[1]);
			$src_pos = array (0, (($new_size[1] - $thumb_height) * ($src_size[1] / $new_size[1])) / 2);
		}else if ($src_aspect > $thumb_aspect){
			// wider than thumb
			$new_size = array(($thumb_width / $src_size[1] ) * $src_size[0], $thumb_height);
			$src_pos = array((($new_size[0] - $thumb_width) * ($src_size[0] / $new_size[0])) / 2, 0);
		}else{
			// same shape as thumb
			$new_size = array($thumb_width, $thumb_height);
			$src_pos = array(0, 0);
		}
		
		if ($new_size[0] < 1) $new_size[0] = 1;
		if ($new_size[1] < 1) $new_size[1] = 1;

		$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
		imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
		
		if ($src_size['mime'] === 'image/jpeg'){
			imagejpeg($thumb, "thumbs/{$_GET[ 'img' ]}");
		}else if ($src_size['mime'] === 'image/png'){
			imagepng($thumb, "thumbs/{$_GET[ 'img' ]}");
		}else if ($src_size['mime'] === 'image/gif'){
			imagegif($thumb, "thumbs/{$_GET[ 'img' ]}");
		}
		
		header("Location: thumbs/{$_GET[ 'img' ]}");
	}
	
	die();
}

if (is_dir('./thumbs') === false){
	mkdir('./thumbs', 0744);
}

$images = glob('*.{jpg, jpeg, png, gif}', GLOB_BRACE);

?>

			<?php
			
			foreach ($images as $image){
				if (file_exists("./thumbs/{image}")){
					echo "<a href=\"{$image}\"><img src=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
				}else{
					echo "<a href=\"{$image}\"><img src=\"?img={$image}\" alt=\"{$image}\" /></a>";
				
				}
			}

			?>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Specific Image gallery for individual user

Post by jacek »

You could give each user their own folder of images, or maybe prefix the images with their username. Even somethign with databases if you wan to do it the easy way ;)
Image
Post Reply