SimplePortal

Customization => Blocks and Modifications => Block Requests => Topic started by: SMiller on February 09, 2014, 04:02:59 AM

Title: Random image from folder block
Post by: SMiller on February 09, 2014, 04:02:59 AM
Trying to make a right-column-block which will display one random image from a folder of images. This way I can just simply add/remove images from that folder, not have to redo code in the block, to change which images it may display.

I'm using a php block with this code within it:

Code: [Select]
<?php 

$dir 
'http://mahjong.us.com/random';
$images glob($dir'*.{jpg,jpeg,png,gif}'GLOB_BRACE);
$randomImage $images[array_rand($images)];

echo 
'<img src="'.$dir.$randomImage.'" width=188>';

?>


I'm specifying the exact folder with an entire url because I'm not sure from which directory a simpleportal php block operates from.
All I get is a question-mark placeholder for a missing image in the block.

Any help?

Thanks.

simpleportal 2.3.5
SMF 2.0.7
Title: Re: Random image from folder block
Post by: AngelinaBelle on February 09, 2014, 04:18:58 PM
If you fully specify the URL to your image, it does not matter which directory your SMF forum is running in, for the purposes of
creating an img tag.

Please have a look at the HTML you have generated.
What is actually in the src= portion of the <img tag.  ?

Is there a file at that URL?

-----------

For more information about SMF global variables like $boarddir, please see http://wiki.simplemachines.org/smf/Global_variables
Title: Re: Random image from folder block
Post by: SMiller on February 09, 2014, 04:51:15 PM
Quote
Please have a look at the HTML you have generated.
What is actually in the src= portion of the <img tag.  ?

To be clear, I'm typing this code into a php block of simple portal, not an html block.

the line of code for the src= is exactly as cut and pasted from that php block:

Code: [Select]
echo '<img src="'.$dir.$randomImage.'" width=188>';
Quote
Is there a file at that URL?

Yes, I have 24 image files at that directory. Here's just one to demonstrate:

http://mahjong.us.com/random/babcock.jpg

(http://mahjong.us.com/random/babcock.jpg)
Title: Re: Random image from folder block
Post by: AngelinaBelle on February 09, 2014, 07:12:09 PM
Of course!

Please look at the HTML generated by your code, and cut/paste the URL of the broken image source from your page.
Title: Re: Random image from folder block
Post by: SMiller on February 09, 2014, 07:28:34 PM
Ok, the html source from that page as it loads into my browser:

Code: [Select]
<div class="sp_block"><img src="http://mahjong.us.com/random" width=188>
and the image address when you right-click on the place holder:

http://mahjong.us.com/random


edit to add: OK! That is the problem, glob can't work with a url, it needed a local directory address, so I changed the code to this:

Code: [Select]
<?php 

$dir 
'../random/';
$images glob($dir'*.{jpg,jpeg,png,gif}'GLOB_BRACE);
$randomImage $images[array_rand($images)];

echo 
'<img src="'.$dir.$randomImage.'" width=188>';

?>


And it works PERFECT!!!! YAY!!!

Thank you for getting me thinking int eh right direction!

OK, so let me take it up a notch... is there a way to add a button to this block to force it to refresh... to see a new random image?

Thanks.
Title: Re: Random image from folder block
Post by: AngelinaBelle on February 10, 2014, 02:43:53 PM
Congratulations!  You solved your problem by doing a little debugging.  That is always a good thing to do.

If you want a button to change the picture, you will have to use some javascript.
And, when the page loads, it will have to already contain that list of file names for your little bit of javascript to rotate, choose randomly, etc.
You can write this all yourself, or you can look into some image rotators that are already available out there for your convenience.
SimplePortal 2.3.8 © 2008-2024, SimplePortal