@readfile - random image

In the process of switching servers. I'm 95% there but having an issue with my random image script.

It works perfectly on my production server - I upgraded the production site to 1.4.4.8 before copying everything to the new server. When I went to the new server I installed 1.4.5 not realizing a newer version was released the next day.

Anyway, I haven't switched production to 1.4.5 so I'm not sure if it's a version issue or a server issue - or something else...

This is the script I'm using:
<?php @readfile('http://test.mikemartinelli.com/zp-core/random2.php?num=1&height=150&width=200&album=My-Car-Pics/Capri.alb')
?>

It's built to pull a random image from a dynamic gallery based on tags...

If you past that link in the browser and load it - it loads a random image so I know it's working...but when it's on the site nothing displays...no error just no image.

The random.php script:

<?php

/*******************************************************************************
* random.php: return random image
*******************************************************************************
* URL Parameters:
* num - number of images
* width - width of random image.
* height - height of random image.
* class - css class for random image.
* album - album to get the random image, default is root.
*
*******************************************************************************
*/

define('OFFSET_PATH', true);
require_once("template-functions.php");

isset($_REQUEST['num']) ? $num = $_REQUEST['num'] : $num = 0;
isset($_REQUEST['width']) ? $width = $_REQUEST['width'] : $width = 50;
isset($_REQUEST['height']) ? $height = $_REQUEST['height'] : $height = 50;
isset($_REQUEST['cwidth']) ? $cropw = $_REQUEST['cwidth'] : $cropw = 200;
isset($_REQUEST['cheight']) ? $croph = $_REQUEST['cheight'] : $croph = 150;
isset($_REQUEST['class']) ? $class = $_REQUEST['class'] : $class = '';
isset($_REQUEST['album']) ? $album = $_REQUEST['album'] : $album = '';

header ('Content-Type: text/html; charset=' . getOption('charset'));

while ($num > 0) {
if ($album == '') {
$randomImage = getRandomImages();
} else {
$randomImage = getRandomImagesAlbum($album);
}

$randomImageURL = getURL($randomImage);
echo 'getTitle() . '" class="' . $class . '">' .
'<img border="0" src="' . getMainSiteURL() . $randomImage->getCustomImage(null, $width, $height, $cropw, $croph, null, null, null, null) .
'" width="' . $width . '" height="' . $height . '" alt="'.$randomImage->getTitle().'"';
echo "/>
\n";
$num--;
}

?>

Any ideas?
Thanks,
Mike

Comments

  • acrylian Administrator, Developer
    Besides that the code is not properly escaped so we cannot look at it you should be using the object model (and/or the search class functions) to get a random image from an album.

    There is a random image function which does not support tags though.
  • I don't understand what your saying...that exact code works on 1.4.4.8 on a different server so I'm trying to figure out why it's not working on the new server. Been using the same code for years now w/o issue.

    I got the @readfile code from here:
    http://www.zenphoto.org/news/random-image-for-external-site

    and I got the random.php file from this site too...

    `
    <?php @readfile('http://test.mikemartinelli.com/zp-core/random2.php?num=1&height=150&width=200&album=My-Car-Pics/Capri.alb')
    ?>
    `
    `
    <?php

    /*******************************************************************************
    * random.php: return random image
    *******************************************************************************
    * URL Parameters:
    * num - number of images
    * width - width of random image.
    * height - height of random image.
    * class - css class for random image.
    * album - album to get the random image, default is root.
    *
    *******************************************************************************
    */

    define('OFFSET_PATH', true);
    require_once("template-functions.php");

    isset($_REQUEST['num']) ? $num = $_REQUEST['num'] : $num = 0;
    isset($_REQUEST['width']) ? $width = $_REQUEST['width'] : $width = 50;
    isset($_REQUEST['height']) ? $height = $_REQUEST['height'] : $height = 50;
    isset($_REQUEST['cwidth']) ? $cropw = $_REQUEST['cwidth'] : $cropw = 200;
    isset($_REQUEST['cheight']) ? $croph = $_REQUEST['cheight'] : $croph = 150;
    isset($_REQUEST['class']) ? $class = $_REQUEST['class'] : $class = '';
    isset($_REQUEST['album']) ? $album = $_REQUEST['album'] : $album = '';

    header ('Content-Type: text/html; charset=' . getOption('charset'));

    while ($num > 0) {
    if ($album == '') {
    $randomImage = getRandomImages();
    } else {
    $randomImage = getRandomImagesAlbum($album);
    }

    $randomImageURL = getURL($randomImage);
    echo 'getTitle() . '" class="' . $class . '">' .
    'imagegetCustomImage(null, $width, $height, $cropw, $croph, null, null, null, null) .
    '" width="' . $width . '" height="' . $height . '" alt="'.$randomImage->getTitle().'"';
    echo "/>\n";
    $num--;
    }

    ?>
    `
  • acrylian Administrator, Developer
    Sorry, I don't know this random.php nor do I understand why it is a separate file called via url actually. Why don't you just use a function within your theme's functions.php?
  • I'm no PHP whiz just did some searching years ago and found this...was able to get it to work exactly how I wanted and now it's not. Was just wondering if something changed from 1.4.4.8 to 1.4.5 that would cause it to not display or if it was a server issue...

    I'm open to other options though...

    I mis-spoke earlier...not using tags to display images...the virtual gallery is create based on tags...this random file references the virtual gallery.
  • acrylian Administrator, Developer
    I have no idea why it does not work anymore. It looks overly complicated to me anyway, the url request seems completely unecessary if you know the album to get the image from anyway. That is what getRandomImageAlbum() does.
  • That `@readfile()` may be prohibited on the test site. Often servers will prohibit file I/O on urls. Anyway, this is not a good way to get random images within your own site. It's stated purpose is to fetch random images from an external site, e.g. for you to use it on site A to get images from site B.

    For theme use, your simplest solution is to create a dynamic album from the tags and get random images from that album via the `getRandomImageAlbum()` that acrylian mentions.
  • Yeah perhaps the new server is the issue. I have a ticket in with them too...they had to change some setting around for me to install zenphoto so hopefully it's another setting they can enable.

    In the meantime I've used the getrandomimageAlbum() function to accomplish what I want.

    This pulls a random image from a dynamic gallery (gallery based on tags) and displays the image, with link, sized to 200px wide.

    `
    <?php $randomImage = getRandomImagesAlbum("My-Design-Work/Design.alb"); $randomImageURL = getURL($randomImage); print "<a href='".getURL($randomImage)."' title='".$randomImage->getTitle()."'>imagegetSizedImage('200')."' alt='".$randomImage->getTitle()."' />"; ?>
    `
Sign In or Register to comment.