20.8. How to display images in PHP-Nuke blocks

Blocks offer a quick way to incorporate content in PHP-Nuke. They are happily used to display images, since they can be placed anywhere in the three layout columns - and their position can be comfortably changed from the administration panel. A very common use of blocks in PHP-Nuke is to display dynamic images, i.e. images that change depending on some parameter of dynamic nature, most offen time: a daily quote, a daily weather forecast or a daily comic.

Most of the time you will not be the original author of the quote, forecast or comic. You will want to fetch this content from some popular source on the Internet.

ImportantBefore you proceed: think on copyright!
 

We will describe the tecnology that allows you to use somebody else's work to make your site more beautiful, attractive or informative. This almost certainly touches that person's copyright. Please take the time to read the admonition on copyright in Section 8.2.3!

PHP enables you to read a remote URL, store its whole content in one (!) variable, then search the contents of the variable for any regular expression (for regular expressions, see Section 25.3). We will use this technique to find out the filename of the User Friendly daily comic strip, in order to display it in a PHP-Nuke block. This UserFriendly block reads the User Friendly daily cartoon page line by line, searches for the pattern

whatever<IMG ALT="Latest Strip"whateverSRC="imagefile">whatever

extracts the location of the image file (imagefile) from it and displays it in the block. The PHP code of the User Friendly block is a classic example of how you can do this “scrapping” in PHP (-Nuke):

<?php
if (eregi("block-User_Friendly.php",$PHP_SELF)) { (1)
    Header("Location: index.php");
    die();
}
$ufurl =  "http://www.userfriendly.org/static/"; (2)
$content = "<i>ufurl:</i> $ufurl<br>";
$imagefile = ""; (3)
if (!($textfile = fopen( "$ufurl",  "r"))) { (4)
    $content .= "$ufurl<br> could not be opened<br>"; (5)
    die();
}
while (!feof($textfile)) { (6)
    $line = fgets($textfile, 1500);          (7)
    if (eregi( ".*<IMG ALT=\"Latest Strip\".*SRC=\"(.*)\">.*", $line, $out)) { (8)
        $imagefile = $out[1]; (9)
        break;
    }
}
fclose($textfile);
$content = "<p align=center>";
$content .= "<A HREF=\"http://www.userfriendly.org\" target=\"_blank\">
<IMG SRC=\"$imagefile\" alt=\"User Friendly by Illiad\"></A>"; (10)
?>
(1)
This is the standard check at the start of every PHP-Nuke block: if the name of the script, as it was called by the user, contains the full name of the block, then the user must have called the block directly. In this case we redirect him to the home page. As with modules, a direct call of a block not only will not work, it will also bypass all security measures of PHP-Nuke (see for example Section 23.4.3).
(2)
The URL of the remote page containing the dynamic image.
(3)
Initialization of the image filename.
(4)
We open the remote page for reading ("r"). This operation returns a "file handle", a unique identifier for the opened file, which is stored in $textfile.
(5)
If the opening operation failed, we output an error message in the content variable of the block and quit.
(6)
If the opening operation succeeded, we start reading the content of the remote page in a loop, line-by-line, until the end-of-file (eof) character is encountered.
(7)
We only read 1500 characters at a time. This is more than enough for our purposes.
(8)
This is where all the magic takes place! We search the line we just read for occurences of a regular expression. The expression itself means
whatever<IMG ALT="Latest Strip"whateverSRC="imagefile">whatever

where "whatever" may be any content, including space. The regular expression contains a subexpression in the place of the image filename. This results in the file name being stored in the $out array, as its first element.

(9)
We copy the first element the $out array to the $imagefile variable. By construction of the regular expression above, $imagefile now contains the image filename of the daily comic strip. We only have to display it!
(10)
We echo an image anchor using as source the filename we just "scrapped" from our source page. This displays the daily comic strip without fetching the image to our web server (hot linking!).

A similar technique is used in news aggregators, like the MyHeadlines module (see Section 8.3.9), that “scrap” news sources for pieces of dynamic information that are not otherwise available (i.e. not available as RSS/RDF news feeds), see Template Based Scraping. For each source page, a regular expression is stored in a table. Its subexpressions will automatically store the dynamic information pieces that are of interest to us in the elements of some $out array. We would then only have to display those elements for each source.

TipQuod medicina aliis, aliis est acre venenum[1]
 

If you are an artist whose images are being hot-linked with the above methods without your permission, you are certainly not excited. But don't despair! You can defend your work with a simple technique that requires only the Apache mod_rewrite module and a few lines in your .htaccess file. See Section 25.6.

20.8.1. How to display random images in PHP-Nuke blocks

We now move to a more advanced example of a PHP-Nuke block: a block that displays random images. Suppose you have a collection of images in a directory of your webserver and would like to display a random image each time in a PHP-Nuke block. Using PHP-Nuke's own positioning capabilities for blocks (in the administration panel, from the “Blocks” link, see Section 7.1), you can achieve an almost arbitrary positioning of random images in your page - a functionality that could be used to display whatever visual content you like, ranging from a “daily babe” to a random skyscraper banner.

To create a random image block for PHP-Nuke, proceed as follows (see Random Picture Block):

Make a script called block-RandomPics.php and put it in the blocks folder. The code for block-RandomPics.php is:

<?php
$content="";
if (eregi("block-RandomPics.php",$PHP_SELF)) {
  Header("Location: index.php");
  die();
}
// $whereimgs has to be in the Domain (like localhost area) 
// or put ../ in front of folder name
$whereimgs = "../pictures";
mt_srand((double)microtime()*1000000);
$imgs = dir($whereimgs);
while ($file = $imgs->read()) {
  if (eregi("gif", $file) || eregi("jpg", $file)) {
    $imglist .= "$file ";
  }
}
closedir($imgs->handle);
$imglist = explode(" ", $imglist);
$a = sizeof($imglist)-2;
$random = mt_rand(0, $a);
$image = $imglist[$random];
$asin = explode(".", $image);
$content = "";
$content .= "<center><a href=\"PicShow.php?show=".$whereimgs."/$image\">";
$content .= "<img src=\"".$whereimgs."/$image\" border=\"0\" 
              width=\"120\" alt=\"\"><br><br>";
$content .= "Click for full picture.</a><BR></center>";
?>

The block-RandomPics.php block reads all .gif's and .jpg's from the $whereimgs directory into an array ($imglist), computes a random index between 0 and the maximum array index ($a) and uses the random index to select an image name from the array. It then writes some HTML code to display a thumbnail of 120 pixels width with a ling to the original picture. The link itself passes the show parameters on the URL as the concatenation of the $whereimgs directory and the random image name ($image) to a script we still have to write, PicShow.php.

TipThe right images path
 

Taking into account that block-RandomPics.php has to be located in the blocks folder, then, if your images folder is the folder “pictures” under the “PHP-Nuke root” directory (i.e. under the same directory where mainfile.php, index.php, config.php etc. are located), then you have to prepend a “../” to “pictures”, as we did in the code, assuming that you don't want to give an absolute, fully qualified URL there.

For PicShow.php, which is the script that will show the full image, we have to write code that behaves just like a standard PHP-Nuke module (but without any administration functions), so you can see it as an example of a minimal module:

<?php
if (!isset($mainfile)) { include("mainfile.php"); }
$index = 0; 
include("header.php");
OpenTable(); 
$sl = $show;
echo " 
<center><h3><a href=\"javascript:history.back()\">Go back!<br><br>
<img src=$sl border=\"2\"></a></h3> 
</center>"; 
closetable();
include("footer.php");
?> 

Notes

[1]

One person's medicine is another's foul poison.