Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

Random image w/ filename as title... (solved)

images php coding issues or problems here.

Moderators: macek, egami, gesf

Random image w/ filename as title... (solved)

Postby MarkMushakian » Sat Jun 18, 2011 8:53 pm

Hi, folks..

On my personal site I have a section for images that are selected at random from a directory on refresh. I'm currently using Random image script (RandIm) by Klemen Stirn (I can post code if necessary). It works fine, but these are large images, and I would like to give my users the ability to click on the image displayed on my homepage (which is already restrained in size by CSS) to reveal the full-size image in a pop-up window or Lightbox. That part isn't the issue for me, but rather getting the PHP code to display the random image as a filename, or regular img tag, so that I could then turn it into an image link.

I have looked online for a very long time (months), and the only solutions I find for random image selection either create errors I don't understand or only output the link to the php file instead of the actual image source.

So, I'm taking a shot here, on the chance that someone either has the perfect code right up their sleeve, or can point me to a solution that I just haven't found. I imagine that I need a function in the code that reads the filename of the chosen image source and outputs it, but I'm not sure how to do so. My knowledge of PHP is VERY amateurish, so I can manage understanding of a code's structure and how to implement certain things, but I can't construct my own code from scratch.

What I'm looking for is a PHP code that will:

1. allow me to display an image on my website, that is randomly selected from a directory on my server.

2. allow me to add to the files in the directory without having to list them on any kind of txt file

3. allow me to maintain mnemonic naming, as opposed to having to name the files 001, 002, etc.

4. allow the output to be sent to the browser in HTML as a fixed image source that I could display full-size in a new window without the code refreshing to a new image.

5. allow me to display the filename of the randomly chosen image as image alt/title tags.

So, it seems like a lot, and maybe even seems rather particular... but I don't imagine it being a very difficult request, or maybe even a completely unique one (I saw the recent post about wanting to use EXIF data, which is along the same vein).

I hope to find an answer, so that this doesn't just end up as one of those random forum entries someone finds by a Google search for the same problem that doesn't have a solution :)
Last edited by MarkMushakian on Sun Jun 19, 2011 7:48 pm, edited 1 time in total.
MarkMushakian
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sat Jun 18, 2011 8:17 pm

Re: Random image w/ filename as title...

Postby egami » Sun Jun 19, 2011 10:44 am

Ok, so this is pretty easy.
I'm not going to write the code for you, but I will tell you the steps and functions to make this work effectively.

Step 1:
Php.net/scandir
This will list all files in the directory, and create an array.

Step 2:
Get the amount of files in your newly created array by using the count function. Php.net/count


Step 3:
Once you have the count, create a range to randomly choose from
Php.net/rand

Step 4:
Call the picture from the array and create the a link

SUMMARY:
$directory = 'this/dir';
$array = scandir($directory);
unset($array['1']);
unset($array['0']);
$counts = count($array);

$filename = rand(0,$counts);
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Random image w/ filename as title...

Postby MarkMushakian » Sun Jun 19, 2011 11:09 am

I'd actually considered the fact that I might just have to take this upon myself, after posting here last night, pushing through and studying PHP functions to create this myself, so thank you very much for a big step in the right direction :). Haha, wish me luck.

Update: Solution

This is what I ended up with. I really appreciated the simplicity of the coding idea provided, so I did my best to stick with it. It was a fantastic challenge for my lack of knowledge, but I was able to piece it all together. Thank you, again :)

Code: Select all
<?php
$directory = '/siteroot/pathto/imagesfolder/';
$array = scandir($directory);
unset($array['1']);
unset($array['0']);
$counts = count($array);

$filename = mt_rand(0,$counts);

$img = $array[$filename];

   echo (
      '<a target="_blank" href="/pathto/imagesfolder/' . $img . '" title="' . substr($img,0,-6) . '">
         <img src="/pathto/imagesfolder/' . $img . '" alt="' . $img . '"/>
      </a>' );
?>
MarkMushakian
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sat Jun 18, 2011 8:17 pm

Re: Random image w/ filename as title... (solved)

Postby egami » Tue Jun 21, 2011 8:11 am

You got it, pretty simple isn't it.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT


Return to PHP coding => Images

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.