News:

Wondering if this will always be free?  See why free is better.

Main Menu

Opinions on a banner rotator script.

Started by Antechinus, January 14, 2015, 02:46:19 PM

Previous topic - Next topic

Antechinus

I've been thinking about making a change to a feature on my new theme.

What I did initially was to hijack the SMF random news lines to create a banner change on page load. This was fun on a test site, livens up the testing time, and works just fine, but has the obvious disadvantages of a/ hijacking the SMF random news lines :D and b/ not being theme-specific.

So, obvious next step is to put the SMF stuff back to normal and think about incorporating a dedicated script to do the job, with theme-specific custom settings so different themes can run a different pack of banners (for instance, dark themes and light themes may want a different look up top).

I've checked the mod site and most of the banner flipping mods seem to use this free script: Marcofolio - PHP Random Image Rotation. This looks like it would do the job nicely if included in the theme.

What I'm wondering now is if, given what it does, this script is straightforward, secure and reasonably performant, or if there would be a markedly better way of doing the job.

Code is below, for convenience.

<?php

/*
DOWNLOADED FROM http://www.marcofolio.net/
Check it out for more interesting scripts & downloads

AUTOMATIC IMAGE ROTATOR
Version 2.2 - December 4, 2003
Copyright (c) 2002-2003 Dan P. Benjamin, Automatic, Ltd.
All Rights Reserved.

http://www.hiveware.com/imagerotator.php

http://www.automaticlabs.com/


DISCLAIMER
Automatic, Ltd. makes no representations or warranties about
the suitability of the software, either express or
implied, including but not limited to the implied
warranties of merchantability, fitness for a particular
purpose, or non-infringement. Dan P. Benjamin and Automatic, Ltd.
shall not be liable for any damages suffered by licensee
as a result of using, modifying or distributing this
software or its derivatives.


ABOUT
This PHP script will randomly select an image file from a
folder of images on your webserver.  You can then link to it
as you would any standard image file and you'll see a random
image each time you reload.

When you want to add or remove images from the rotation-pool,
just add or remove them from the image rotation folder.


VERSION CHANGES
Version 1.0
- Release version

Version 1.5
- Tweaked a few boring bugs

Version 2.0
- Complete rewrite from the ground-up
- Made it clearer where to make modifications
- Made it easier to specify/change the rotation-folder
- Made it easier to specify/change supported image types
- Wrote better instructions and info (you're them reading now)
- Significant speed improvements
- More error checking
- Cleaner code (albeit more PHP-specific)
- Better/faster random number generation and file-type parsing
- Added a feature where the image to display can be specified
- Added a cool feature where, if an error occurs (such as no
  images being found in the specified folder) *and* you're
  lucky enough to have the GD libraries compiled into PHP on
  your webserver, we generate a replacement "error image" on
  the fly.

    Version 2.1
        - Updated a potential security flaw when value-matching
          filenames

    Version 2.2
        - Updated a few more potential security issues
        - Optimized the code a bit.
        - Expanded the doc for adding new mime/image types.

        Thanks to faithful ALA reader Justin Greer for
        lots of good tips and solid code contribution!


INSTRUCTIONS
1. Modify the $folder setting in the configuration section below.
2. Add image types if needed (most users can ignore that part).
3. Upload this file (rotate.php) to your webserver.  I recommend
   uploading it to the same folder as your images.
4. Link to the file as you would any normal image file, like this:

<img src="http://example.com/rotate.php">

5. You can also specify the image to display like this:

<img src="http://example.com/rotate.php?img=gorilla.jpg">

This would specify that an image named "gorilla.jpg" located
in the image-rotation folder should be displayed.

That's it, you're done.

*/




/* ------------------------- CONFIGURATION -----------------------


Set $folder to the full path to the location of your images.
For example: $folder = '/user/me/example.com/images/';
If the rotate.php file will be in the same folder as your
images then you should leave it set to $folder = '.';

*/


$folder '.';


/*

Most users can safely ignore this part.  If you're a programmer,
keep reading, if not, you're done.  Go get some coffee.

    If you'd like to enable additional image types other than
gif, jpg, and png, add a duplicate line to the section below
for the new image type.

Add the new file-type, single-quoted, inside brackets.

Add the mime-type to be sent to the browser, also single-quoted,
after the equal sign.

For example:

PDF Files:

$extList['pdf'] = 'application/pdf';

    CSS Files:

        $extList['css'] = 'text/css';

    You can even serve up random HTML files:

    $extList['html'] = 'text/html';
    $extList['htm'] = 'text/html';

    Just be sure your mime-type definition is correct!

*/

    
$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';


// You don't need to edit anything after this point.


// --------------------- END CONFIGURATION -----------------------

$img null;

if (
substr($folder,-1) != '/') {
$folder $folder.'/';
}

if (isset(
$_GET['img'])) {
$imageInfo pathinfo($_GET['img']);
if (
    isset( $extListstrtolower$imageInfo['extension'] ) ] ) &&
        
file_exists$folder.$imageInfo['basename'] )
    ) {
$img $folder.$imageInfo['basename'];
}
} else {
$fileList = array();
$handle opendir($folder);
while ( false !== ( $file readdir($handle) ) ) {
$file_info pathinfo($file);
if (
    isset( $extListstrtolower$file_info['extension'] ) ] )
) {
$fileList[] = $file;
}
}
closedir($handle);

if (count($fileList) > 0) {
$imageNumber time() % count($fileList);
$img $folder.$fileList[$imageNumber];
}
}

if (
$img!=null) {
$imageInfo pathinfo($img);
$contentType 'Content-type: '.$extList$imageInfo['extension'] ];
header ($contentType);
readfile($img);
} else {
if ( function_exists('imagecreate') ) {
header ("Content-type: image/png");
$im = @imagecreate (100100)
    or die ("Cannot initialize new GD image stream");
$background_color imagecolorallocate ($im255255255);
$text_color imagecolorallocate ($im0,0,0);
imagestring ($im255,  "IMAGE ERROR"$text_color);
imagepng ($im);
imagedestroy($im);
}
}

?>


Night09

It can be done standalone without the news section. I had it on a guild site and it was pretty easy to implement. That would save butchering the news maybe?

Heres a similar example of how it was done http://www.pontikis.net/tip/?id=25 

Antechinus

Yes I know it can be done standalone. ;) Implementing the script is easy. I'm just somewhat wary of free decade-old scripts from obscure corners of the web, and would like an opinion on the quality of its coding, and whether it is advisable to make something a lot better instead of using this one.

Night09

The simpler the better, less to fix if it goes wrong and it worked ok on 2.0.x for us. ;)

Antechinus

Hmm. That one looks simple alright. Could do with a few tweaks.

Ideally what I would want to do is have a custom textarea on the admin>theme settings page (which is easy to make) and allow the admin to enter a comma separated list of image names (banner1.jpg, banner2.jpg, etc) then have the PHP implode (or would that be explode?) that in index.template.php to create the array (no big deal) and then spit that out as inline style on the header anchor, so that the image gets loaded as a background image for better performance, with the surrounding anchor size being predefined in index.css (responsive, of course).

ETA: I just RTFM. I would want explode on the list of image names. :D Would probably also want to include a limit on the amount of exploding, just in case some nutter uploaded 574 images to the banners folder.

I'm gonna do a bit more reading.

margarett

Actually if you're providing the images yourself, you don't need such a fancy script...

You just really need to establish a base folder, append it the image name (which you do by randomizing the array key, most likely), do a file_exist to check the image is there (probably repeat if not?) and, if so, spit out a common "img".
That script needs more stuff because it is making an image callable through a PHP file: to use that script you would use <img src="http://www.yabadaba.com/script.php">, but in your case you can just spit the image itself <img src="http://www.yabadaba.com/image_a.png">

Of course this exposes the image's filename (and path) but, if that's not a problem, then I would say it's far more easy to do...
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Antechinus

TBH I can't see that exposing the path is any big deal, since "View page source" will do that for any inline image anyway, and "Inspect element" will do it for CSS background images.

Night09

As long as theres an index.php in the folder its just like any other then.

Antechinus

Quote from: margarett on January 14, 2015, 03:36:27 PMActually if you're providing the images yourself...

Well I'd provide at least one initially, but let people have the option of adding their own, or deleting the default from the list. It'd still require FTP or cPanel or similar to get the images into the folder, but anyone who can't handle that won't be running a forum for long.

margarett

Quote from: Antechinus on January 14, 2015, 03:48:53 PM
TBH I can't see that exposing the path is any big deal, since "View page source" will do that for any inline image anyway, and "Inspect element" will do it for CSS background images.
Not really. On a regular SMF instance where you upload your avatar (not here as it was modified) you can't see its full path, because it's called through a php script (see attached)
So you need to really consider all angles ;)

Quote from: Antechinus on January 14, 2015, 03:59:13 PM
It'd still require FTP or cPanel or similar to get the images into the folder, but anyone who can't handle that won't be running a forum for long.
:P True
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Antechinus

Quote from: margarett on January 14, 2015, 04:05:50 PM
Quote from: Antechinus on January 14, 2015, 03:48:53 PM
TBH I can't see that exposing the path is any big deal, since "View page source" will do that for any inline image anyway, and "Inspect element" will do it for CSS background images.
Not really. On a regular SMF instance where you upload your avatar (not here as it was modified) you can't see its full path, because it's called through a php script (see attached)
So you need to really consider all angles ;)

Good point. So is exposing the path likely to present any problems in the case of banners?

Night09

QuoteGood point. So is exposing the path likely to present any problems in the case of banners?

Its unlikely as most sites you can view the background or banner anyway including here.

Illori

if you have the exact link visible, the images can be cached.

i had posted this in one of the charter boards.

Quote from: Illori
put this code in a file named rotator.php in your Sources folder

<?php

function getRandomFromArray($ar) {

    
mt_srand( (double)microtime() * 1000000 );

    
$num array_rand($ar);

    return 
$ar[$num];

}

 

function 
getImagesFromDir($path) {

    
$images = array();

    if ( 
$img_dir = @opendir($path) ) {

        while ( 
false !== ($img_file readdir($img_dir)) ) {

            
// checks for gif, jpg, png

            
if ( preg_match("/(\.gif|\.jpg|\.png)$/"$img_file) ) {

                
$images[] = $img_file;

            }

        }

        
closedir($img_dir);

    }

    return 
$images;

}

 

$root '';

// If images not in sub directory of current directory specify root 

//$root = $_SERVER['DOCUMENT_ROOT'];

 

global $settings$context;
$path $settings['default_theme_dir'] .  '/rotator/';
 
// Obtain list of images from directory 
$imgList = array();
if ( 
$img_dir = @opendir($path) )
{
while ( false !== ($img_file readdir($img_dir)) )
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/"$img_file) )
$imgList[] = $img_file;

closedir($img_dir);

$num array_rand($imgList);
$context['img_logo'] = $settings['theme_url'] .  '/rotator/' $imgList[$num];
}
else
{
$context['img_logo'] = $context['header_logo_url_html_safe'];


?>


put the images in a folder named rotator in your your default themes folder

from the default theme find
function template_body_above()
{
global $context, $settings, $options, $scripturl, $txt, $modSettings;

add after
require_once($sourcedir.'/rotator.php');

in this case we were replacing the logo so
<a href="', $scripturl, '">', empty($context['header_logo_url_html_safe']) ? $context['forum_name'] : '<img src="' . $context['header_logo_url_html_safe'] . '" alt="' . $context['forum_name'] . '" />', '</a>
is replaced with

<a href="', $scripturl, '">', empty($context['header_logo_url_html_safe']) ? $context['forum_name'] : '<img src="' . $context['img_logo'] . '" alt="' . $context['forum_name'] . '" />', '</a>

some of the above code came from members of the dev/customization teams

you would just need to edit it to fit where you want the image to show up in your theme

Antechinus

Ok, that would need some simple changing of paths for what I want, but that's a no-brainer. One question: what does the "mt_srand( (double)microtime() * 1000000 );" code do? Is that for rotating on a set time interval?

Arantor

Well, the rest of the code is about getting a random number, but the random number generator sometimes needs... priming to actually be basically random. That's what that code does.


Antechinus

Oh yeah, so I'm assuming that wouldn't require any admin input for image names, and would just grab whatever was in the banners folder. If correct, that's probably going to be better for the average mug.

Night09

Yeah should need no admin help as its set to grab images from the folder so all they need to do is upload them there.

Antechinus

That sounds like the way to go. I can just have a help blurb in admin telling them what to do, and a checkbox to enable rotation. Easy.

Night09

The only advice I would give is an image size range to suit so it doesnt kill the template if someone uploads a massive HD image or something daft like that.

Advertisement: