Topic: Randomize gallery, but with some fixed.

I realise this is not currently offered, but I'm wondering if there is some way to achieve it.

I want to randomize a gallery (select the option in Customise/Gerneral) BUT be able to select specific images so that they remain fixed in their positions according to their placement in the Images tab in Juicebox.

Seems like a lot of people might want to do this.  Currently, it's all or nothing.  Random or fixed position.

Re: Randomize gallery, but with some fixed.

As you are aware, there is currently no way to do this in Juicebox-Pro.
It might be quite a difficult thing to achieve.

One approach might be to create a regular Juicebox-Pro gallery with a static XML file but instead of using this XML file directly, use a configUrl to point towards a PHP (or other server-side scripting language) file which would generate a new XML file dynamically.
The PHP file could read the static XML file and load all the <image> tags into an array and you could then rearrange the elements in the array using PHP functions such as shuffle) before generating a new XML file on-the-fly.
For example, you could load only certain images into a new array to shuffle before writing them out around other images in fixed positions.
It would involve quite a bit of work (and knowledge of PHP) but it would be possible.
These three forum posts might be useful:
http://stackoverflow.com/questions/2671 … f-an-array
http://stackoverflow.com/questions/1775 … -it-in-php
http://stackoverflow.com/questions/2827 … e-elements

Re: Randomize gallery, but with some fixed.

Thanks for the Reply Steven.

The php solution is way beyond my understandings and ability.

I do think it is something that would be interesting for many people.  We all have images which we feel are stronger than others and would like to be sure that those will be seen.  Equally, we want to avoid repeat visitors always seeing the same initial images and consequently, not progressing any further through the galleries.  It is a conundrum.

I'll add it to the suggestions.

Re: Randomize gallery, but with some fixed.

I'll add it to the suggestions.

That's a good idea. (It's certainly the best place for all the suggestions for future versions. It keeps them all together and ensures that they are not overlooked by the developers.)

Here's a PHP script you can use to fix the position of the first few images in your gallery and then shuffle the rest.
(1) Create a regular gallery with JuiceboxBuilder-Pro.
(2) Save the code below in a file named 'config.php'.
(3) Place the 'config.php' file in your gallery folder.
(4) Use a configUrl in your gallery's embedding code to point towards the 'config.php' file:

configUrl: "config.php",

(5) Change the $fixed variable's value (at the top of the 'config.php' file) to be the number of fixed images at the start of the gallery.

Just be sure to not set randomizeImages="TRUE" (in JuiceboxBuilder-Pro's 'Customize -> General' section) as this will randomize the images after the script sets the image order.

I hope this helps.

<?php
header('Content-Type: application/xml');

$filename = 'config.xml';
$fixed = 2;

if (is_file($filename)) {

    $dom_doc = new DOMDocument('1.0', 'UTF-8');
    $dom_doc->load($filename);

    $settings_tags = $dom_doc->getElementsByTagName('juiceboxgallery');
    $settings_tag = $settings_tags->item(0);

    if (!is_null($settings_tag)) {

        $new_dom_doc = new DOMDocument('1.0', 'UTF-8');
        $new_dom_doc->formatOutput = true;

        $new_settings_tag = $new_dom_doc->createElement('juiceboxgallery');

        $values = array();

        foreach ($settings_tag->attributes as $attribute) {
            $name = $attribute->nodeName;
            $value = $attribute->nodeValue;
            $values[$name] = $value;
        }

        foreach ($values as $key=>$value) {
            $new_settings_tag->setAttribute($key, $value);
        }

        $images = $settings_tag->getElementsByTagName('image');

        $all = array();

        foreach ($images as $image) {
            $all[] = $image;
        }

        $integer = intval($fixed);
        $clean = $integer < 0 || $integer > $images->length ? 0 : $integer;

        $remainder = array_slice($all, $clean);

        shuffle($remainder);

        for ($i = 0; $i < $clean; $i++) {
            $image = $all[$i];
            $node = $new_dom_doc->importNode($image, true);
            $new_settings_tag->appendChild($node);
        }

        foreach ($remainder as $image) {
            $node = $new_dom_doc->importNode($image, true);
            $new_settings_tag->appendChild($node);
        }

        $new_dom_doc->appendChild($new_settings_tag);

        echo $new_dom_doc->saveXML();

    }

}
?>

Re: Randomize gallery, but with some fixed.

Thanks Steven.  Kind of you to provide that.  I'll try and implement on a test site.

Re: Randomize gallery, but with some fixed.

You're welcome.

I've now modified the code to accept individual images.
You can identify images to be in fixed positions in one of two ways:
(1) You can use a comma-separated list of images to be in fixed positions via the $fixed array at the top of the script.
(2) You can mark images to be in fixed positions within JuiceboxBuilder-Pro by adding the following code anywhere within an image title.

<span class="fixed"></span>

The script just looks for the term 'fixed' and the code added to the image title will not be visible in the gallery itself.

You can use either of these methods (or a combination of both at the same time).

<?php
header('Content-Type: application/xml');

$filename = 'config.xml';
$fixed = array(1, 2, 3, 8, 11, 12);

if (is_file($filename)) {

    $dom_doc = new DOMDocument('1.0', 'UTF-8');
    $dom_doc->load($filename);

    $settings_tags = $dom_doc->getElementsByTagName('juiceboxgallery');
    $settings_tag = $settings_tags->item(0);

    if (!is_null($settings_tag)) {

        $new_dom_doc = new DOMDocument('1.0', 'UTF-8');
        $new_dom_doc->formatOutput = true;

        $new_settings_tag = $new_dom_doc->createElement('juiceboxgallery');

        $values = array();

        foreach ($settings_tag->attributes as $attribute) {
            $name = $attribute->nodeName;
            $value = $attribute->nodeValue;
            $values[$name] = $value;
        }

        foreach ($values as $key=>$value) {
            $new_settings_tag->setAttribute($key, $value);
        }

        $images = $settings_tag->getElementsByTagName('image');

        $marked = array();
        $all = array();

        for ($i = 0; $i < $images->length; $i++) {
            $image = $images->item($i);
            $title = $image->getElementsByTagName('title');
            $text = $title->item(0);
            if (!is_null($text)) {
                if (stripos($text->nodeValue, 'fixed') !== false) {
                    $marked[] = $i + 1;
                }
            }
            $all[] = $image;
        }

        $remainder = $all;

        $clean = array_values(array_unique(array_merge($fixed, $marked)));

        for ($i = 0; $i < count($clean); $i++) {
            $key = intval($clean[$i]) - 1;
            unset($remainder[$key]);
        }

        shuffle($remainder);

        $counter = 0;

        for ($i = 0; $i < count($all); $i++) {
            $image = in_array($i + 1, $clean) ? $all[$i] : $remainder[$counter++];
            $node = $new_dom_doc->importNode($image, true);
            $new_settings_tag->appendChild($node);
        }

        $new_dom_doc->appendChild($new_settings_tag);

        echo $new_dom_doc->saveXML();

    }

}
?>

Re: Randomize gallery, but with some fixed.

Wow!  Very cool Steven.

I'm shooting this week, but will implement as soon as I can and let you know how it works out.

Re: Randomize gallery, but with some fixed.

Hi Steven,

first off ... a very Happy New Year to you and many thanks for all the help you provided last year. :)

The code works brilliantly and I really think it is a feature that should be included in a future version of JBx Pro.

However ... using the

<span class="fixed"></span>

isn't working for me, but perhaps I'm not implementing it correctly?

I'm adding it to the Title in JBx.  I don't use the titles in my galleries normally, only Captions ... but either way, with the titles on or off, it's not affecting the gallery display at all ... although it *is* being ranomized.

http://www.grantsymon.com/Grabs/JBx-fixedImage.jpg

Re: Randomize gallery, but with some fixed.

I'm adding it to the Title in JBx.

That's where it should go.
Adding <span class="fixed"></span> to an image title simply marks the image to be in a fixed position (the position it is displayed in the JuiceboxBuilder-Pro 'Images' tab). This marker will not be displayed as the image title in the gallery. (It is still possible to use the image title if you want to. Just add your normal image title text before or after this marker.)
In your screenshot, the second image in your gallery should remain in position #2 whilst the other images will be displayed in a random order around it.

The script above (the most recent one) allows you to fix images with two methods (simultaneously, if you like):
(1) via the $fixed array in the code and ...
(2) by marking images with <span class="fixed"></span> in the image titles.

If you choose to use the image title method only, then change:

$fixed = array(1, 2, 3, 8, 11, 12);

... to:

$fixed = array();

... at the top of the code.
(The numbers were there just as an example.)