Trying to get the thumbnails on the right spot

Ask about a PHP problem here.
Post Reply
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Trying to get the thumbnails on the right spot

Post by Robbedoesie »

Hello,
i'm trying to make a image gallery were the thumbnails are in a sidebar and when someone click on a thumbnail the bigger picture appears on the right of the sidebar. I'll have the script from the tutorial from betterphp and i thought that the thumbnails where standing apart from the bigger picture. But now i want to style the thumbnails, but the thumbnails are not appearing where i thought they would appear but they appearing in the div id where the bigger picture should appear. I'll hope the code will help to make it more clear;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body{
	margin: 0;
	padding: 0;
	color: #999;
	background: #000;
	font-family: arial, sans-serif;
}
	
#sidebarleft{
	border-top: solid 1px #404040;
	border-right: solid 1px #404040;
	border-bottom: solid 1px #3a3a3a;
	border-left: solid 1px #404040;
	padding: 0 auto;
	position:absolute;
	margin:10px 0 100px 0;
	left:8px;
	width: 175px;
	height:100%;
	overflow:hidden;
	float: left;
	background-color: #171717;
}
#thumbnails img{
	position: fixed;
	margin: 10px 5px 20px 5px;
}
#image{
	padding: 0px;
	width: 50%;
	float: left;
	position:fixed;
	margin-left:28%;
	top:30%;
}
	</style>
<title>Gallery</title>
</head>

<body>
<div id="wrap">
<div id="sidebarleft">
<div id="thumbnails">
<?php

if (isset($_GET['img'])){
	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('Dit is geen afbeelding.');
		}
		
		$thumb_width   = 50;
		$thumb_height  = 50;
		
		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){
			$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_aspect > $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, "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,JPG,jpeg,JPEG,png,PNG,gif,GIF}', GLOB_BRACE);

?>
</div>
</div>
<div id="image">
<?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>";
	}
}

?>
</div>
</div>
</body>
</html>
I'll tryed to make a foreach loop for the thumbnails and chanche the foreach loop for the images but my php is not good enough and i can't figure it out how i can solve this problem.
Can someone help me here?
Thanks, Robbedoesie
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Trying to get the thumbnails on the right spot

Post by jacek »

This whole section
if (isset($_GET['img'])){
        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('Dit is geen afbeelding.');
                }
               
                $thumb_width   = 50;
                $thumb_height  = 50;
               
                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){
                        $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_aspect > $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, "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();
}
Should be before any HTML, and so should the is_dir mkdir check too really. This section of the code just performs some actions, it does not actually display anything, it's good practice to keep the action-ey code separate from the display-ey code. So with that in mind, here is the same code rearranged a bit and indented.
<?php

if (isset($_GET['img'])){
	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('Dit is geen afbeelding.');
		}
	
		$thumb_width   = 50;
		$thumb_height  = 50;
	
		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){
			$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_aspect > $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, "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,JPG,jpeg,JPEG,png,PNG,gif,GIF}', GLOB_BRACE);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<style type="text/css">
			body{
				margin: 0;
				padding: 0;
				color: #999;
				background: #000;
				font-family: arial, sans-serif;
			}
	
			#sidebarleft{
				border-top: solid 1px #404040;
				border-right: solid 1px #404040;
				border-bottom: solid 1px #3a3a3a;
				border-left: solid 1px #404040;
				padding: 0 auto;
				position:absolute;
				margin:10px 0 100px 0;
				left:8px;
				width: 175px;
				height:100%;
				overflow:hidden;
				float: left;
				background-color: #171717;
			}
			#thumbnails img{
				position: fixed;
				margin: 10px 5px 20px 5px;
			}
			#image{
				padding: 0px;
				width: 50%;
				float: left;
				position:fixed;
				margin-left:28%;
				top:30%;
			}
		</style>
		<title>Gallery</title>
	</head>

	<body>
		<div id="wrap">
			<div id="sidebarleft">
				<div id="thumbnails">
					
				</div>
			</div>
			<div id="image">
				<?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>";
					}
				}

				?>
			</div>
		</div>
	</body>
</html>
Hopefully you can see the problem now ;)
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Trying to get the thumbnails on the right spot

Post by Robbedoesie »

Oke, that section is brought back to where it belongs.

What i want is that the thumbnails and image are separated and can be shown on the same page.
I show for more clearness a template;
template.GIF
template.GIF (3.74 KiB) Viewed 1925 times
I want the thumbnails in the sidebar on the left, and the image on the same page inside the white borders.
I think its possible with PHP but i am looking for the right tutorials, but i can't find them. Maybe i search in the wrong direction. Do someone know where to look or what to do best to accomplish this?
Thanks,
Robbedoesie
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Trying to get the thumbnails on the right spot

Post by jacek »

Ah okay I see what you mean now.

You link
<a href=\"{$image}\">
Should link to the page but sending a get variable that contains the name of the image you ant to display
<a href=\"?img={$image}\">
for example.

Then you can use that in the area where the full size image should be shown to, erm, show he image ;)
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Trying to get the thumbnails on the right spot

Post by Robbedoesie »

It looks very easy but i can't imagine that it is easy.

I'll have placed the foreach loop in the leftsidebar because i want there the thumbnails to be in and want to link to the container where the image have to appear. But if i understand you correctly I'll have to place the foreach loop with the link in the part where the image have to be in?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Trying to get the thumbnails on the right spot

Post by jacek »

No, the part where the big images is displayed, just displays the image giving in the url.

Just have a go, get the thumbnails in the right place first, then go from there.
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Trying to get the thumbnails on the right spot

Post by Robbedoesie »

Hello,
I'll have being working this last week to try, with your advices, to get the image in the div container "image". Not with any succes what so ever. I'll have seen a lot of syntax and index errors. I'll solved them all and I'll think i am now the best error solver here on the forum. But no image in the right container. Most of the times the images shows up in a new venster, other times the thumbnail show up in a new venster. I'll think this is the best try.
<div id="sidebarleft">
<?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>";
	}
}

?>
</div>
<div id="image">
<?php
	if (isset($_GET['img'])){
		echo"<a href='?img={$image}'><img src='{$image}' alt='{$image}' /></a>";
	}
?>
</div>
I'll not going to write about the other 1867 efforts.
Am i on the right track or is this an absolute disgrace for everything were PHP stands for?
The problem for me is also that there is not much on the internet about this. I'll have seen questions about this subject, but no answers. The most solutions were given in javascript, but if there is a solution in PHP it is a better solution. I could'nt find any tutorials either.
I'll hope somebody can help me and let disappear my frustration mood :x . My wife, son, friends and family and of course myself will appreciative it.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Trying to get the thumbnails on the right spot

Post by jacek »

href='{$image}'
This should be
href='?img={$image}'
But then there is a bit of problem, because you use the same $_GET variable to create thumbnails, the solution is simple, just change the variable for the large image to something else...
    <div id="sidebarleft">
    <?php
    foreach ($images as $image){
            if (file_exists("./thumbs/{$image}")){
                    echo "<a href='?large_img={$image}'><img src='thumbs/{$image}' alt='{$image}' /></a>";
            }else{
                    echo "<a href='?large_img={$image}'><img src='?img={$image}' alt='{$image}' /></a>";
            }
    }
     
    ?>
    </div>
    <div id="image">
    <?php
            if (isset($_GET['full_img'])){
                    echo '<img src="', htmlentities($_GET['large_img']), '" alt="" /></a>';
            }
    ?>
    </div>
Something like this anyway.
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Trying to get the thumbnails on the right spot

Post by Robbedoesie »

It's not working yet. When i click on a thumbnail the large image is not showing. I made some changes but not with a difference. Do i have to make a variabele for the full image or is this a wrong thought.
I'll change it to this;
<div id="sidebarleft">
    <?php
    foreach ($images as $image){
            if (file_exists("./thumbs/{$image}")){
                    echo "<a href='?large_img={$image}'><img src='thumbs/{$image}' alt='{$image}' /></a>";
            }else{
                    echo "<a href='?large_img={$image}'><img src='?img={$image}' alt='{$image}' /></a>";
            }
    }
     
    ?>
    </div>
    <div id="image">
    <?php
            if (isset($_GET['full_img'])){
                    echo '<a href="?large_img={$image}"><img src="{$image}', htmlentities($_GET['large_img']), '" alt="{$image}" /></a>';
            }
    ?>
    </div>
Thanks for your help sofar and for your patience with this.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Trying to get the thumbnails on the right spot

Post by jacek »

Well you are checking for full_img and then using large_img so fix that first :)

Also the src="" of your large img tag is not quite right, it should be more like the one I posted.
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Trying to get the thumbnails on the right spot

Post by Robbedoesie »

Grrreat, thanks Jecek. :D :D
For who wants to know the full code;
<div id="sidebarleft">
    <?php
    foreach ($images as $image){
            if (file_exists("./thumbs/{$image}")){
                    echo "<a href='?full_img={$image}'><img src='thumbs/{$image}' alt='{$image}' /></a>";
            }else{
                    echo "<a href='?full_img={$image}'><img src='?img={$image}' alt='{$image}' /></a>";
            }
    }
     
    ?>
    </div>
    <div id="image">
    <?php
            if (isset($_GET['full_img'])){
                    echo '<a href="?full_img={$image}"><img src="', htmlentities($_GET['full _img']), '" alt="{$image}" /></a>';
            }
    ?>
    </div>
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Trying to get the thumbnails on the right spot

Post by jacek »

Good news, glad you got it working.
Image
Post Reply