Display custom link for thumbnails to full sized images

Hi everyone!

I was looking for some help with my PHP snippet that I’m using.

Basically the code automatically looks through a thumbnail folder on my website and essentially sorts them and displays them as is. Now I got that portion to work great because my site is now displaying all the thumbnails as I wished them to be displayed. Where my issue falls at this point is having them link to a specific page each. Here is my code and maybe I can explain better once you have a look;


            $dir = "../corq media 3.1/thumbnail/";
            $dh = opendir($dir);
            while($filename = readdir($dh))
                $filepath = $dir.$filename;
                if(is_file($filepath) and ereg("\.jpg" ,$filename))
                    $gallery[] = $filepath;

                foreach($gallery as $image)

                    echo "<article class ='????'><a class ='shrink' href='$image'><img src='$image' height='310' width='355' /></a></article>";


Okay so if you look at where it says echo everything works fine however what I am trying to do is have the “href” change to a specific link to match which thumbnail is shown.

I attempted a few things…created a var $linkExten which basically displayed the beginning portion of the echo and tried to have the href call the $filename and created a new while loop that ended the filename off with .php however that didn’t work. To be honest I have tried a few methods and nothing seemed to work…

Any help would be greatly appreciated.

***EXTRA INFO IF IT HELPS: I have a thumbnail folder on in my root directory that holds all the thumbnails. I have an img folder which holds the same images however the full sizes of them with the same names as the corresponding thumbnail version.

If you check the value of $image it will be something like …/corq media 3.1/thumbnail/image.jpg and you want something like …/corq media 3.1/large/image.jpg to use in your link.

So you need to replace thumbnail with large and I would try:

$link = str_replace( 'thumbnail', 'large', $image);
echo "<article class ='????'><a class ='shrink' href='$link'><img src='$image' height='310' width='355' /></a></article>";
1 Like

Yes thats pretty much what I am looking for! However I would like the link to actually go to a .php page which will show some details on the image. Is there a way to replace the .jpg with .php?

I suppose you could do another string replace to change the jpg to a php or if you check out the php manaual you should be able to do it in one go with two arrays: http://php.net/manual/en/function.str-replace.php

Something like:

$search  = array('thumbnail', 'jpg');
$replace = array('large', 'php');
$large = str_replace($search, $replace, $image);

Although I have not tested it.

1 Like

awesome! Thanks a lot I will look into this :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.