|
|
Bennet's Multifolder Random Image - Problems with Bennet's Random Image Selector | ||
Discussion by caronthegerman with 20 Replies.
Last Update: August 19, 2010, 11:58 pm (View Latest) | Page 1 of 2 pages. | ||
QUOTE (8ennett)
Something rather annoying about this post being stretched out like this, but never mind lolOk 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.
CODE
<?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://www.loldmypants.com/picturearchive/';
$randir[1] = 'http://www.loldmypants.com/picturearchive/useruploads/';
$getrandir = 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
CODE
<a href=""><img src="http://www.loldmypants.com/picturearchive/randomizer.php"></a>Link: view Post: 147920
Mon Aug 16, 2010 Reply New Discussion
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.
Mon Aug 16, 2010 Reply New Discussion
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 tit-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!
Mon Aug 16, 2010 Reply New Discussion
The images in your http://www.loldmypants.com/picturearchive/ (random001.jpg to random167.jpg) need to be put in a new folder and we will call this folder http://www.loldmypants.com/picturearchive/archive
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://www.loldmypants.com/picturearchive/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.
Mon Aug 16, 2010 Reply New Discussion
Warning: readdir(): supplied argument is not a valid Directory resource in /home/caronthegerman/loldmypants.com/picturearchive/randomizer.php on line 107
Warning: closedir(): supplied argument is not a valid Directory resource in /home/caronthegerman/loldmypants.com/picturearchive/randomizer.php on line 113
I uploaded randomizer.php as an attachment.
Mon Aug 16, 2010 Reply New Discussion
$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
Mon Aug 16, 2010 Reply New Discussion
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:
CODE
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);
Mon Aug 16, 2010 Reply New Discussion
CODE
function resizeImage($originalImage,$return,$toWidth,$toHeight){// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;
// Recalculate new size with default ratio
if ($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:
CODE
<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:
CODE
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).'">';Mon Aug 16, 2010 Reply New Discussion
CODE
<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
CODE
<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.
Mon Aug 16, 2010 Reply New Discussion
CODE
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.
Mon Aug 16, 2010 Reply New Discussion
QUOTE (8ennett)
Or you can use vhortex's method above, however I have found on some touchscreen phones that can cause errors.Link: view Post: 147931
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
Mon Aug 16, 2010 Reply New Discussion
Mon Aug 16, 2010 Reply New Discussion
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.
Wed Aug 18, 2010 Reply New Discussion
align="top"
or failing that try
align="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" />
Wed Aug 18, 2010 Reply New Discussion
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
QUOTE
<img src="useruploads/random139.jpg" width="600" align="top">into
QUOTE
<img src="/useruploads/random139.jpg" width="600" align="top">to do that change on your generating script, just locate this line of code
QUOTE
return '<img src="'.$randir[$getrandir].''.$imgarray[$rand].'" width="'and change it to
QUOTE
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.
Thu Aug 19, 2010 Reply New Discussion
Turn based web game in PHP - session handling issue I Want To Create A PHP Text Based Web Game (2)
|
(6) How To Set Up Xampp
|
Index




