Page 1 of 1

Image gallery

Posted: Wed Dec 14, 2011 11:26 am
by Robbedoesie
Hello,
i am making an image gallery. I am having the thumbnails and full image on the same page. It all works well but when i open this gallery page the full image is not there. The full image appears only when i click on a thumbnail.

How can i let the full image appears immediately after opening that page. Can somebody help me or show me a tutorial about this. I really have no idea how to solve this problem and i can't find anything about this on the internet.

The code is;
 <div id="sidebarleft">	
     <?php
	 //thumbnails
    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
//Full image
        if (isset($_GET['full_img'])){
                echo '<a href="?full_img={$image}"><img src="', htmlentities($_GET['full_img']), '" alt="{$image}" /></a>';
        }
?>
</div>	

Thanks,
Robbedoesie

Re: Image gallery

Posted: Thu Dec 15, 2011 1:51 am
by jacek
Are the full images in the same folder as this script ?

If you view the page source, does the src="" of the image look as it should ?

Re: Image gallery

Posted: Thu Dec 15, 2011 9:37 am
by Robbedoesie
Hello Jacek,
yes, the full images are in the same folder as this script.

Re: Image gallery

Posted: Sat Dec 17, 2011 3:31 pm
by jacek
Ah, I think I misunderstood. To make the full image be displayed all of the time you would need to give it some sort of default value to use if one is not specified in the URL. you could do it like this.
if (isset($_GET['full_img'])){
    $full_image = $_GET['full_image'];
}else{
    $full_image = $images[0];
}

echo '<a href="?full_img={$image}"><img src="', htmlentities($full_image), '" alt="{$image}" /></a>';
Which would display the first image by default., this can be shortened a little to be
$full_image = (isset($_GET['full_image'])) ? $_GET['full_image'] : $images[0];

echo '<a href="?full_img={$image}"><img src="', htmlentities($full_image), '" alt="{$image}" /></a>';

Re: Image gallery

Posted: Sun Dec 18, 2011 9:11 pm
by Robbedoesie
thanks for your help Jacek.

Is it also possible to show there the latest uploaded image?

Re: Image gallery

Posted: Mon Dec 19, 2011 1:12 am
by jacek
Robbedoesie wrote:Is it also possible to show there the latest uploaded image?
Yes, it would require sorting the result of scandir() using filectime(), I would get what you have now perfect before trying that.

An easy solution to not always showing the same image would be to pick a random one from the list if that is the reason what want to show the newest ? Have a look at the array_rand() function.

Re: Image gallery

Posted: Tue Dec 20, 2011 10:05 pm
by Robbedoesie
Everything works fine, thanks for your help here, but i am trying to get the image filename in the url because i use the imagenames to get it by the image from the url. I thought that with the ?full_image part in the img src section would do the trick but it did'nt. If/else statments did not work for me either. How can i get the filename from the full image what displays all of the time in the url?

To get the latest uploaded image there and working with filectime, i think i'll have to study on that first. There is enough to find on the internet about that. I'll hope it is working also with glob() because of course i followed your automatic image gallery tutorial and there you are using glob().

Re: Image gallery

Posted: Wed Dec 21, 2011 12:37 am
by jacek
Robbedoesie wrote:Everything works fine, thanks for your help here, but i am trying to get the image filename in the url because i use the imagenames to get it by the image from the url. I thought that with the ?full_image part in the img src section would do the trick but it did'nt. If/else statments did not work for me either. How can i get the filename from the full image what displays all of the time in the url?
I have no idea what you mean :? The name of the default is in the $full_image variable after you have done the check I showed above :?
Robbedoesie wrote:To get the latest uploaded image there and working with filectime, i think i'll have to study on that first. There is enough to find on the internet about that. I'll hope it is working also with glob() because of course i followed your automatic image gallery tutorial and there you are using glob().
Yeah you can do it using the result of glob(), you need to sort the result of glob() by the time the file was created.

Re: Image gallery

Posted: Wed Dec 21, 2011 1:57 pm
by Robbedoesie
Maybe better to show you the code as it is now;
 <?php
	 //thumbnails
    foreach ($images as $image){
            if (file_exists("./thumbs/{$image}")){
                    echo "<a href='?full_image={$image}'><img src='thumbs/{$image}' alt='{$image}' /></a>";
            }else{
                    echo "<a href='?full_image={$image}'><img src='?img={$image}' alt='{$image}' /></a>";
            }
    }     
?>	
</div>
<div id="image">
<?php
//Full image
            $full_image = (isset($_GET['full_image'])) ? $_GET['full_image'] : $images[0];
     
    echo '<a href="?full_image={$image}"><img src="',htmlentities($full_image), '" alt="{$image}" /></a>';

?>
<div id="caption">
 <?php	
 //Filename placed under each image
$filename = basename($_SERVER['REQUEST_URI'], ".jpg");
$filename = str_replace("_", " ", $filename);
$filename = substr_replace($filename,"",0,23);
echo $filename;
?>
All the filenames are showing up in the url like this http://localhost/robsgallery/uploads/ga ... _image.jpg, except the filename from the image what shows up when you open the page, there the url is http://localhost/robsgallery/uploads/gallery.php. So i am trying to get also that filename in the url but i do'nt know how i have to do this. For example by doing this;
<?php
//Full image
            $full_image = (isset($_GET['full_image'])) ? $_GET['full_image'] : $images[0];
     
    echo '<a href="?full_image={$image}"><img src="',htmlentities(?full_image={$full_image}), '" alt="{$image}" /></a>';

?>
I tried a lot of variations to get the filename in the url, but nothing worked for me. I hope you can give me the right answer or send me in the right direction.

I hope this make sense for you. If not, let me know and i gonna try to explain it better.

Re: Image gallery

Posted: Wed Dec 21, 2011 4:03 pm
by jacek
If you want it like that you will have to redirect the browser to the page with the filename and then handle it as normal.

Re: Image gallery

Posted: Thu Dec 22, 2011 10:33 am
by Robbedoesie
Oke, thanks. I am gonna search out how this is working.