Jump to content
xisto Community
Sign in to follow this  
caronthegerman

Bennet's Multifolder Random Image Problems with Bennets Random Image Selector

Recommended Posts

Something rather annoying about this post being stretched out like this, but never mind lol

 

Ok I'm going to write a function below for you so you can select a random image from a random folder each time the function is called.

What I have done is basically taken the coding example from here (so the credit really goes to that person) and just added a bit of extra functionality

too incorporate multiple folders.

 


$randir[0] = 'http linenums:0'><?phpfunction getRandomImage(){// Change directories to your image directories, you can also add more if you wish eg. $randir[2] = 'images/randomdirectory3/'; just remember to add the trailing slash$randir[0] = 'http://forums.xisto.com/no_longer_exists/; = 'http://forums.xisto.com/no_longer_exists/ = array_rand($randir, 1);$dir=opendir($randir[$getrandir[0]]);$i=0;while($imgfile = readdir($dir)){if (($imgfile != ".") && ($imgfile != "..")){$imgarray[$i] = $imgfile;$i++;}}closedir($dir);$rand=rand(0,count($imgarray)-1);if($rand >= 0){return '<img src="'.$randir[$getrandir[0]].''.$imgarray[$rand].'" width="170" height="137">';}}echo getRandomImage();?>


But this gives me an error:

Warning: readdir(): supplied argument is not a valid Directory resource in /home/caronthegerman/loldmypants.com/picturearchive/randomizer.php on line 12

 

Warning: closedir(): supplied argument is not a valid Directory resource in /home/caronthegerman/loldmypants.com/picturearchive/randomizer.php on line 18

 

 

There is a broken image box under this error message.

 

I have no idea what to do. Even the single folder random picture does not work anymore. :-X

 

 

Also, would I be able to

<a href=""><img src="http://forums.xisto.com/no_longer_exists/;
this randomizer?

Share this post


Link to post
Share on other sites

Right I know what the problem is, i'm guessing that the folder that contains all the images also contains other files as well such as php documents?If that's the case it may be prudent to slightly restructure your site and keep the images in a seperate folder to any other files, however if that's not possible then just let me know and I'll rewrite the function again to ONLY pick up images (the reason I said try restructuring first is because modififying the code for images only is going to be annoying lol but i'll do it if need be)*EDIT*Hold on, scrap all that, I can see the problem, the directory entered isn't valid. Show me what you have replaced the images/randomdirectoryX/ with and I'll see wuts up. setting the php file as the source would mean using the gd2 library and putting the image in the header, it's a completely different set of code, I'd stick with just echoing the function for now.

Edited by 8ennett (see edit history)

Share this post


Link to post
Share on other sites

Right, ignore all that above, sorry I'm not long out of bed and haven't had any coffee yet.The problem is the directories you have set it to. It needs to be a relative path not an absolute path. Also like I said you can't have any other files apart from images in the same directory as the images, but I will re-write the code as needed if there is no other way.Now move your randomizer.php to the base directory and change the directory lines to the following:$randir[0] = './picturearchive/';$randir[1] = './picturearchive/useruploads/';Instead of working on little *BLEEP*-bits I've got a better idea, if you want to add the contents of the picturearchive directory: randomizer.php 16-Aug-2010 07:13 722 rotate.php 16-Aug-2010 07:07 5.5K upload.php 16-Aug-2010 07:03 3.2K to a rar archive and PM me the link then I'll go through and restructure the entire directory and PHP code so it will work and is efficient.Just working on one problem at a time as they come up can take forever and leave you with some very messy code!

Edited by 8ennett (see edit history)

Share this post


Link to post
Share on other sites

Ok, had a look through all the code, very simple to do. The images in your http://forums.xisto.com/no_longer_exists/ (random001.jpg to random167.jpg) need to be put in a new folder and we will call this folder http://forums.xisto.com/no_longer_exists/ Now here is the MOST important part. If you are INCLUDING randomizer.php in a different php document in the base directory, then change your images directories to the following: $randir[0] = 'picturearchive/archive/'; $randir[1] = 'picturearchive/useruploads/'; However, if the user will be going to http://forums.xisto.com/no_longer_exists/randomizer.php directly then change the directories to: $randir[0] = 'archive/'; $randir[1] = 'useruploads/'; That's it, it should all work now without any problems.

Share this post


Link to post
Share on other sites

I changed everything as you said, but now I'm getting this error:Warning: readdir(): supplied argument is not a valid Directory resource in /home/caronthegerman/loldmypants.com/picturearchive/randomizer.php on line 107Warning: closedir(): supplied argument is not a valid Directory resource in /home/caronthegerman/loldmypants.com/picturearchive/randomizer.php on line 113I uploaded randomizer.php as an attachment.

Share this post


Link to post
Share on other sites

My mistake sorry, on the line where is says:$dir=opendir($randir[$getrandir[0]]);Change that to:$dir=opendir($randir[$getrandir]);And also change the next line:return '<img src="'.$randir[$getrandir[0]].''.$imgarray[$rand].'" width="170" height="137">';to:return '<img src="'.$randir[$getrandir].''.$imgarray[$rand].'" width="170" height="137">';I forgot to remove the [0] from both lines

Share this post


Link to post
Share on other sites

It works!!! Thank you, thank you, thank you.Also, is there any way to make the width about 600 pixels and to calculate a new height to keep the aspect ratio the same?For example something like this:

    list($width, $height)=getimagesize($uploaded_file)    $new_width   = 600;    $new_height  = ($height/$width)*$new_width;    $tmp         = imagecreatetruecolor($new_width, $new_height);    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

Share this post


Link to post
Share on other sites

Yes not a problem, simply use this function:

function resizeImage($originalImage,$return,$toWidth,$toHeight){// Get the original geometry and calculate scaleslist($width, $height) = getimagesize($originalImage);$xscale=$width/$toWidth;$yscale=$height/$toHeight;// Recalculate new size with default ratioif ($yscale>$xscale){$new_width = round($width * (1/$yscale));$new_height = round($height * (1/$yscale));}else {$new_width = round($width * (1/$xscale));$new_height = round($height * (1/$xscale));}if ($return == 'height'){return $new_height;}else {return $new_width;}}

You use it like this, if you want your image to be a max height of 100 and a max width of 100 then you would type this in to your html:

<img src="yourimage.jpg" width="<?php echo resizeImage('yourimage.jpg', 'width', 100, 100); ?>" height="<?php echo resizeImage('yourimage.jpg', 'height', 100, 100); ?>" border="0"/>

So if you wanted to add this function in to your randomize.php file then add the image resize function and then change the <img> tag at the bottom of the random image function to the following:

return '<img src="'.$randir[$getrandir[0]].''.$imgarray[$rand].'" width="'.resizeImage($randir[$getrandir[0]].''.$imgarray[$rand], 'width', 100, 100).'" height="'.resizeImage($randir[$getrandir[0]].''.$imgarray[$rand], 'height', 100, 100).'">';

Edited by 8ennett (see edit history)

Share this post


Link to post
Share on other sites

On the HTML side, the sizes are optional and putting only width

<img src="'.$randir[$getrandir].''.$imgarray[$rand].'" width="600">

will tell HTML to make the image 600pixels wide and maintain aspect ratio for the height and this goes the same with the height tag which is

<img src="'.$randir[$getrandir].''.$imgarray[$rand].'" height="150">

which tells the HTML renderer that the image will be 150px and use a proportional width with it.

Share this post


Link to post
Share on other sites

I put those damn [0] in again, sorry here is the modified code for the getRandomImage();

return '<img src="'.$randir[$getrandir].''.$imgarray[$rand].'" width="'.resizeImage($randir[$getrandir].''.$imgarray[$rand], 'width', 100, 100).'" height="'.resizeImage($randir[$getrandir].''.$imgarray[$rand], 'height', 100, 100).'">';

Or you can use vhortex's method above, however I have found on some touchscreen phones that can cause errors.

Share this post


Link to post
Share on other sites

Or you can use vhortex's method above, however I have found on some touchscreen phones that can cause errors.

I forgot to mention, I don't work with touchscreen phones so take it also into considerations. Touchscreens needs to have a fixed width and height as part of the declaration but this gets automatically fixed computer clients but not 100% sure on phones. If you wont work with phones, my shortcut code will work else use the longer way ;D

Share this post


Link to post
Share on other sites

Hey it's me again. For some reason the pictures that come up on my randomizer.php are sometimes forced under where the picture is supposed to be. For example, if it is supposed to be in the middle of the screen, the top of the picture will be right where the bottom of the picture is supposed to be. When this happens the picture is under the "New Picture" button and you can only see a tiny bit of the top.


Do you know what the problem is?

It happens very randomly, but, because the images are random too, it might be because of the picture.

Share this post


Link to post
Share on other sites

Yeah i think I know what you mean, add this to the end of the <img> tag in randomizer.phpalign="top"or failing that tryalign="texttop"It's because of the adverts either side of the image, just change the image alignment and it should work fine. eg.<img src="blah.jpg" align="texttop" />

Share this post


Link to post
Share on other sites

i checked your site and there was a problem..

if you added an extra '/' on the end of the URL.. the picture fails to load. you can fix this by rewriting this part of your code

 

<img src="useruploads/random139.jpg" width="600" align="top">

into

<img src="/useruploads/random139.jpg" width="600" align="top">

to do that change on your generating script, just locate this line of code

 

return '<img src="'.$randir[$getrandir].''.$imgarray[$rand].'" width="'

and change it to

return '<img src="/'.$randir[$getrandir].''.$imgarray[$rand].'" width="'

see the red item..

 

i discovered the problem when i click your hotlink.. normally this wont cause problems but you must also note that some links are being padded with an extra / on some browsers.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.