XF 2.0 Pull Random Image for Registration Notice

kbryant414

Active member
Hello.

For my forum, Post Terminus, I've been trying to set a Notice for visitors that are not logged in, inviting them to register/join. I can get that to work well enough with one image, but since this is for a writing RP forum, I was hoping to have the Notice randomly pick one from a series of character images and quotes I'm preparing (4 at the start, with more to be added later).

I read through this thread (https://xenforo.com/community/resources/display-a-random-banner.375/) and tried to apply it, but it's displaying all four images and quotes where I'm defining the variables, and where it's supposed to pick one randomly, it's instead displaying the Xen code.

Since the thread there is specifically for banners and ads, is it not applicable to images/Notices? Or (perhaps more likely) am I using the code incorrectly? Any help would be appreciated. Thank you.

gl3c4qK.png

HTML:
<!-- Defining variables for character images and quotes -->
    <xen:set var="$regImg.1">
        <a href="register/" onclick="_gaq.push(['_trackEvent', 'Button', 'Click', 'Chibi01']);"><img min-height:180px src="imagelib/register/Chibi01.gif" alt="Come Join Post Terminus!" title="Come Join Post Terminus!"></a>
        </xen:set>
    <xen:set var="$regQuo.1">I'm not lost, just in transit...</xen:set>

    <xen:set var="$regImg.2">
        <a href="register/" onclick="_gaq.push(['_trackEvent', 'Button', 'Click', 'Chibi02']);"><img min-height:180px src="imagelib/register/Chibi02.gif" alt="Come Join Post Terminus!" title="Come Join Post Terminus!"></a>
        </xen:set>
    <xen:set var="$regQuo.2">How could I not care? I never knew them!</xen:set>

    <xen:set var="$regImg.3">
        <a href="register/" onclick="_gaq.push(['_trackEvent', 'Button', 'Click', 'Chibi03']);"><img min-height:180px src="imagelib/register/Chibi03.gif" alt="Come Join Post Terminus!" title="Come Join Post Terminus!"></a>
        </xen:set>
    <xen:set var="$regQuo.3">Life's cheap... Death pays.</xen:set>

    <xen:set var="$regImg.4">
        <a href="register/" onclick="_gaq.push(['_trackEvent', 'Button', 'Click', 'Chibi04']);"><img min-height:180px src="imagelib/register/Chibi04.gif" alt="Come Join Post Terminus!" title="Come Join Post Terminus!"></a>
        </xen:set>
    <xen:set var="$regQuo.4">Apologies... It's just business.</xen:set>

<!-- Selecting image and quote randomly to display in Notice. -->
<table>
    <tr>
        <td>
            <xen:foreach loop="$regImg" value="$curRegImg" i="$i" count="$count">
                <xen:if is="!{$rand}">
                    <xen:set var="$rand">{xen:calc '({$serverTime} % {$count}) + 1'}</xen:set>
                </xen:if>
                <xen:if is="{$i} == {$rand}">
                    {xen:raw '$regImg.{$rand}'}
                </xen:if>
            </xen:foreach>
        </td>
        <td>
            <p class="loginMessage">
            <div class="triangle-border left">
                <xen:foreach loop="$regQuo" value="$curRegQuo" i="$i" count="$count">
                    <xen:if is="!{$rand}">
                        <xen:set var="$rand">{xen:calc '({$serverTime} % {$count}) + 1'}</xen:set>
                    </xen:if>
                    <xen:if is="{$i} == {$rand}">
                        {xen:raw '$regQuo.{$rand}'}
                    </xen:if>
                </xen:foreach>
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <p style="font-size:24px" align="center" class="loginMessage">
                <b><a href="register/" onclick="_gaq.push(['_trackEvent','Button','Click','Link']);" class="loginLink">Click here!</a></b>
        </td>
        <td>
            <p style="font-size:14px" align="center" class="loginMessage">Join us in Post Terminus
        </td>
    </tr>
</table>
 
Okay, so I was able to figure out problems with my approach, and ultimately I'll have to tackle this via PHP. In the hopes that the next person with this issue has an easier time of it, here's the summary:

  • Problem 1: The thread and documentation I was referencing was suggesting <xen:...> codes, but those apply only for XF1. In XF2, you would use <xf:...> code, and there doesn't appear to be available documentation that shows what, if anything, is different beyond that.
  • Problem 2: The referenced thread was also for Banners, not Notices (which I am using). As it turns out, the <xf:...> code doesn't apply in Notices, only basic HTML and {name} or {title} tags.

Thank you to Kier on the XF staff for confirming those details for me.

The following solution is probably plain as day for people who've been at this longer, but in case anyone needs it: save the following PHP code, or something like it, into a file in the same folder as your images, and use it for the img src in the Notice message. It's not immediately ideal for my circumstances, since I will have to adjust the images themselves to include the quotes I wanted. I'd rather deal with code than modify graphics. But hey, it works.

PHP:
<?php

    $folder = '.';

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

$img = null;

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

if (isset($_GET['img'])) {
    $imageInfo = pathinfo($_GET['img']);
    if (
        isset( $extList[ strtolower( $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( $extList[ strtolower( $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 (100, 100)
            or die ("Cannot initialize new GD image stream");
        $background_color = imagecolorallocate ($im, 255, 255, 255);
        $text_color = imagecolorallocate ($im, 0,0,0);
        imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
        imagepng ($im);
        imagedestroy($im);
    }
}

?>
 
Back
Top Bottom