Page 1 of 1

Need Help With Automatic Image Gallery

Posted: Wed Apr 04, 2012 2:41 am
by robc2236
Hey i have done the Automatic Image Gallery tutorial and tried to adapt it so that images can load from the users folder cut i can’t seem to get it to work i think it is something simple but after 2 hours of looking at the code i can’t see it can someone help please

thanks robc
<?php
//$username = strtolower($_POST['member_name']);
$username = "robc2236";
echo $username;
$images = glob("images/{$username}/*.{jpg,jpeg,,png,gif}",GLOB_BRACE);
if(isset($_GET['img']))
{

	if(file_exists("images/{$username}/".$_GET['img']))
	{
		ignore_user_abort(true);
		set_time_limit(120);
		ini_set('memory_limit','512M');
		
		$src_size = getimagesize("images/{$username}/".$_GET['img']);
		if($src_size === false)
		{
		die('that does not look like an image.');
		}
		$thumb_width = 250;
		$thumd_height = 200;
		if($src_size['mime'] === 'image/jpeg')
		{
		$src = imagecreatefromjpeg("images/{$username}/".$_GET['img']);
		}
		else if($src_size['mime'] === 'image/png')
		{
		$src = imagecreatefrompng("images/{$username}/".$_GET['img']);
		}
		else if($src_size['mime'] === 'image/gif')
		{
		$src = imagecreatefromgif("images/{$username}/".$_GET['img']);
		}
		$src_aspect = round(($src_size[0]/$src_size[1]),1);
		$thumb_aspect = round(($thumb_width / $thumd_height),1);
		
		if($src_aspec<$thumb_aspect )
		{
			$new_size = array($thumb_width,($thumb_width/$src_size[0]) * $src_size[1]);
			$src_pos = array(0,($new_size[1] - $thumb_height)/2);
		}
		else if($src_aspec>$thumb_aspect )
		{
			$new_size = array(($thumb_width/ $src_size[1]) * $src_size[0], $thumb_height);
			$src_pos = array(($new_size[0] - $thumb_width)/2,0);
		}
		else
		{
		 $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,"images/{$username}thumbs/{$_GET['img']}");
		}
		else if($src_size['mime'] === 'image/png')
		{
		echo"test";
			imagepng($thumb,"images/{$username}/thumbs/{$_GET['img']}");
		}
		else if($src_size['mime'] === 'image/gif')
		{
			imagegif($thumb,"images/".$username."/thumbs/{$_GET['img']}");
		}
		header("Location: images/".$username."/thumbs/{$_GET['img']}");
	}
	die();
}


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

?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Gallery</title>
</head>

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

Re: Need Help With Automatic Image Gallery

Posted: Wed Apr 04, 2012 12:05 pm
by SkillBuzz
What errors are you getting?

Re: Need Help With Automatic Image Gallery

Posted: Wed Apr 04, 2012 3:11 pm
by robc2236
im not geting any errors from what i can see its not getting past
if(isset($_GET['img']))

Re: Need Help With Automatic Image Gallery

Posted: Thu Apr 05, 2012 8:28 am
by SkillBuzz
Add
error_reporting(E_ALL);
to your file. If it's not working, it has errors. =)

Re: Need Help With Automatic Image Gallery

Posted: Thu Apr 05, 2012 1:16 pm
by jacek
It looks like you are passing the full path to the file into $_GET['img'] here
echo "<a href=\"\"><img src=\"?img=images/{$username}/{$image}\" alt=\"{$image}\"/></a>";
but you seem to treat it as a file name at the top
if(file_exists("images/{$username}/".$_GET['img']))
This will be checking
if(file_exists("images/{$username}/images/{$username}/".$_GET['img_name']))