Image gallery

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

Image gallery

Post 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
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Image gallery

Post 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 ?
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Image gallery

Post by Robbedoesie »

Hello Jacek,
yes, the full images are in the same folder as this script.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Image gallery

Post 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>';
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Image gallery

Post by Robbedoesie »

thanks for your help Jacek.

Is it also possible to show there the latest uploaded image?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Image gallery

Post 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.
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Image gallery

Post 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().
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Image gallery

Post 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.
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Image gallery

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Image gallery

Post 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.
Image
User avatar
Robbedoesie
Posts: 97
Joined: Thu May 19, 2011 7:37 pm
Location: Enkhuizen, Holland

Re: Image gallery

Post by Robbedoesie »

Oke, thanks. I am gonna search out how this is working.
Post Reply