xoops forums

BJS3D

Just popping in
Posted on: 2011/5/15 16:31
BJS3D
BJS3D (Show more)
Just popping in
Posts: 7
Since: 2010/2/11
#1

TDMDownloads - Random Image Link Block

I love this module (TDMDownloads 1.6). My site co-conspirator and I wanted to come up with a block that would display a random image from the SHOTS directory which would link to its corresponding download listing.

Now, neither of us are hard-core coders. We're graphic designers by trade, but here's what I came up with. Since xoops.org has helped me immensely over the years, this is but a small contribution in return:

<?php 
// OPENS THE DIRECTORY
if ($handle opendir('uploads/TDMDownloads/images/shots/')) 
// READS THE DIRECTORY
{
    while (
false !== ($file readdir($handle))) 
// RETURNS A LIST OF FILES IN THE DIRECTORY
{
        if (
$file != "." && $file != ".."
// REMOVES THE .JPG EXTENSION FROM FILENAMES LISTED
{
        
$file substr($file, -4);
// CREATES AN ARRAY OF THE LIST WITHOUT EXTENSIONS
        
$narray[$i]=$file;
        
$i++; 
        }
// CHOOSES A RANDOM FILE FROM THE ARRAY
        
$j rand(0$i-1);
// CREATES THE STRING 'FILENAME' 
        
$filename "$narray[$j]";
// CREATES A STRING TO BE USED AS AN EXTENSION
        
$jpg ".jpg";
// ADDS THE EXTENSION BACK TO FILENAME FOR IMAGE
        
$image "$filename$jpg";
// ERROR TRAPPING - IF IMAGE NAME IS BLANK
        
if ($image==".jpg"
// CHANGES IMAGE NAME FROM BLANK TO USEFUL WITHOUT EXTENSION
{
        
$image "cid68lid2";
// RECREATES THE STRING 'FILENAME' FROM EMPTY STRING
        
$filename "$image";
// ADDS THE EXTENSION BACK TO THE IMAGE NAME
        
$image "$image$jpg";
        }
// CONVERTING RAW IMAGE NAME INTO LISTING-SPECIFIC URL
// CHANGES THE 'CID' IN FILENAME TO 'CID='
        
$filename str_replace('cid''cid='$filename);
// CHANGES THE 'LID' IN FILENAME TO '&LID='
        
$filename str_replace('lid''&lid='$filename);
        }
// ADDS STRING 'FILENAME' TO END OF URL FOR DOWNLOAD LINK
// ADDS STRING 'IMAGE' TO END OF URL FOR IMAGE DISPLAY
            
echo "<div align="center"><a target="_parent" title="Click image to view download" href="http://coastalgfx.com/modules/TDMDownloads/singlefile.php?" . $filename . ""><img style="width:160px; height:160px; border:0px solid #212121;" src="http://coastalgfx.com/uploads/TDMDownloads/images/shots/" . $image . "" /></a></div>n";
// CLOSES THE DIRECTORY BEFORE END SCRIPT
    
closedir($handle);
}
?>


This is saved to file.php and called into a block using an iframe like this:

<Iframe src="http://coastalgfx.com/randcgi.php" width="160" height="160" frameborder="0" scrolling="no"></Iframe>


See it in action here

Now, the script often returns a blank random filename and the reason eludes me. I've error-handled this by setting a default filename in case there's a blank return but I'm sure someone can greatly improve upon this.

* It should be noted that, in order for this to work as intended, shot images must be named accordingly as cid#lid#.jpg (change extension string if using other formats) as each shot relates to its corresponding download.

Kudos to TDM for an awesome and highly extensible module.

heyula

Theme Designer
Posted on: 2011/5/15 17:28
heyula
heyula (Show more)
Theme Designer
Posts: 486
Since: 2008/4/24
#2

Re: TDMDownloads - Random Image Link Block

thank you
Xoops Turkey Team !

zyspec

Module Developer
Posted on: 2011/5/16 15:22
zyspec
zyspec (Show more)
Module Developer
Posts: 1077
Since: 2004/9/21
#3

Re: TDMDownloads - Random Image Link Block

BJS3D,

I think I'd use an approach like the one below. It's a little simpler and should eliminate the occassional 'blank' return - unless there's nothing in the directory. It also allows jpg and png files, if desired.

NOTE: This code is untested - so try it before you put it on a 'live' site
<?php
$imageDir 
'uploads/TDMDownloads/images/shots/';
$allowedExtensions = array('jpg''jpeg''png');
// OPENS THE DIRECTORY
if ($handle opendir($imageDir)) {
  
$narray = array();
  
// READS THE DIRECTORY 
  
while (false !== ($file readdir($handle))) {
    
// RETURNS A LIST OF FILES IN THE DIRECTORY 
    
if (is_file($file)) {
      
$path_parts pathinfo($imageDir $file);
      if (
in_array(strtolower($path_parts['extension']), $allowedExtensions)) {
        
// CREATES AN ARRAY OF THE LIST
        
$narray[] = array('filename' => $path_parts['filename'],
                                  
'extension' => $path_parts['extension']);
      }
    }
  }
  
// CHOOSES A RANDOM FILE FROM THE ARRAY 
  
$j rand(0count($narray)-1); 
  
$image "{$narray[$j]['filename']}.{$narray[$j]['extension']}"
  
// CONVERTING RAW IMAGE NAME INTO LISTING-SPECIFIC URL 
  // CHANGES THE 'CID' IN FILENAME TO 'CID=' 
  
$filename str_replace('cid''cid='$narray[$j]['filename']); 
  
// CHANGES THE 'LID' IN FILENAME TO '&LID=' 
  
$filename str_replace('lid''&amp;lid='$filename); 
  
// ADDS STRING 'FILENAME' TO END OF URL FOR DOWNLOAD LINK 
  // DISPLAY THE LINK
  
echo "<div style='text-align: center;'><a target='_parent' title='Click image to view download' href='http://coastalgfx.com/modules/TDMDownloads/singlefile.php?{$filename}'><img style='width:160px; height:160px; border:0px solid #212121;' src='http://coastalgfx.com/uploads/TDMDownloads/images/shots/{$image}' /></a></div>n"
  
// CLOSES THE DIRECTORY BEFORE END SCRIPT 
  
closedir($handle); 
}
?>

BJS3D

Just popping in
Posted on: 2011/5/16 16:43
BJS3D
BJS3D (Show more)
Just popping in
Posts: 7
Since: 2010/2/11
#4

Re: TDMDownloads - Random Image Link Block

zyspec,

Thank you for posting but I was unable to get the code to return anything but a blank string. Zyspec Live Test You did, however, prompt me to sharpen my code a bit since the way a blank string was handled was a bit redundant.

BTW: why it even returns a blank string is a peculiar issue. How is a blink string ending up in the array? Better yet, how could I check for and remove that from the array before choosing a random string?

Here's my update to the original code with error handling done immediately after the creation of string '$filename' instead of after creating '$image$extension':

<?php 
if ($handle opendir('uploads/TDMDownloads/images/shots/')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
        
$file substr($file, -4);
        
$narray[$i]=$file;
        
$i++; 
        }
        
$extension ".jpg";
        
$j rand(0$i-1);
        
$filename "$narray[$j]";
        if (
$filename=="") {
        
$filename "cid68lid2";
        }
        
$image "$filename$extension";
        
$filename str_replace('cid''cid='$filename);
        
$filename str_replace('lid''&lid='$filename);
        }
            echo 
"<div align="center"><a target="_parent" title="Click image to view download" href="http://coastalgfx.com/modules/TDMDownloads/singlefile.php?" . $filename . ""><img style="width:160px; height:160px; border:0px solid #212121;" src="http://coastalgfx.com/uploads/TDMDownloads/images/shots/" . $image . "" /></a></div>n";
    
closedir($handle);
}
?>


CGFX Live Test

Thank you again for posting, zyspec. Your code looks a lot more efficient and effective than mine. I'll probably have a play with it to see if I can get it working. :P

zyspec

Module Developer
Posted on: 2011/5/26 20:08
zyspec
zyspec (Show more)
Module Developer
Posts: 1077
Since: 2004/9/21
#5

Re: TDMDownloads - Random Image Link Block

Sorry for the delayed response. I'll have to look at my code to see what's going on when I get time - I don't have TDMDownloads loaded on my test server right now. With a couple of well placed debug messages on the test server I could debug it pretty quickly.

The main thing you need to change in your code above is to put the ending brace for the 'while' statement immediately after the end of the first 'if' statement. The way the code works now is that it executes a bunch of unnecessary code everytime through the while loop.

Are there 'other' files in the directory (like index.html)? That would explain why you get 'blank' images.

BJS3D

Just popping in
Posted on: 2011/5/26 20:49
BJS3D
BJS3D (Show more)
Just popping in
Posts: 7
Since: 2010/2/11
#6

Re: TDMDownloads - Random Image Link Block

Hi zyspec,

No worries. What I have now is working. Having said that, 'working' doesn't mean 'correctly coded'. By all logic, your code should have functioned and it's beyond my extremely limited php vocabulary as to why it's not. It's not an extremely pressing issue, though, so definitely don't lose any sleep. TDM, though, will definitely benefit from your code as this feature is far more effective than simply displaying a random shots image with no link to the actual entry.

I ended up reworking the problem of the blank string by forcing a default in the event a string returns null. So far, this is working 100% of the time without error. There were no other file formats in the directory, though, which is why the return of a null string is a mystery. I classified the problem as 'coding error' and handled it as best as a CG artist could.

I also fixed a redundancy issue. The error-catching is now done prior to the image and filename conversions.

<?php 
if ($handle opendir('uploads/TDMDownloads/images/shots/')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
        
$file substr($file, -4);
        
$narray[$i]=$file;
        
$i++; 
        }
        
$extension ".jpg";
        
$j rand(0$i-1);
        
$filename "$narray[$j]";
        if (
$filename=="") {
        
$filename "cid68lid2";
        }
        
$image "$filename$extension";
        
$filename str_replace('cid''cid='$filename);
        
$filename str_replace('lid''&lid='$filename);
        }
            echo 
"<div align="center"><a target="_parent" title="Click image to view download" href="http://coastalgfx.com/modules/TDMDownloads/singlefile.php?" . $filename . ""><img style="width:160px; height:160px; border:0px solid #212121;" src="http://coastalgfx.com/uploads/TDMDownloads/images/shots/" . $image . "" /></a></div>n";
    
closedir($handle);
}
?>


Again, here's how the block is setup:

<div align="left" style="verticle-align:top;"><Iframe src="http://coastalgfx.com/randcgi.php" width="172" height="172" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></Iframe></div>
<
div align="center"><Iframe src="http://coastalgfx.com/dl_count.php" width="164" height="27" frameborder="0" scrolling="no"></Iframe></div>


Thanks for the reply and I look forward to your input.

Is this the code change you're suggesting:

<?php 
if ($handle opendir('uploads/TDMDownloads/images/shots/')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
        
$file substr($file, -4);
        
$narray[$i]=$file;
        
$i++; 
        }
        }
        
$extension ".jpg";
        
$j rand(0$i-1);
        
$filename "$narray[$j]";
        if (
$filename=="") {
        
$filename "cid68lid2";
        }
        
$image "$filename$extension";
        
$filename str_replace('cid''cid='$filename);
        
$filename str_replace('lid''&lid='$filename);
            echo 
"<div align="center"><a target="_parent" title="Click image to view download" href="http://coastalgfx.com/modules/TDMDownloads/singlefile.php?" . $filename . ""><img style="width:160px; height:160px; border:0px solid #212121;" src="http://coastalgfx.com/uploads/TDMDownloads/images/shots/" . $image . "" /></a></div>n";
    
closedir($handle);
}
?>

BJS3D

Just popping in
Posted on: 2011/5/26 21:05
BJS3D
BJS3D (Show more)
Just popping in
Posts: 7
Since: 2010/2/11
#7

Re: TDMDownloads - Random Image Link Block

zyspec,

Your code alteration works perfectly. Thank you.

Random TDM Image Link Test

Also moved the extension string down with the rest of the defined strings:

<?php 
if ($handle opendir('uploads/TDMDownloads/images/shots/')) {
    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {
        
$file substr($file, -4);
        
$narray[$i]=$file;
        
$i++; 
        }
        }
        
$j rand(0$i-1);
        
$filename "$narray[$j]";
        if (
$filename=="") {
        
$filename "cid68lid2";
        }
        
$extension ".jpg";
        
$image "$filename$extension";
        
$filename str_replace('cid''cid='$filename);
        
$filename str_replace('lid''&lid='$filename);
            echo 
"<div align="center"><a target="_parent" title="Click image to view download" href="http://coastalgfx.com/modules/TDMDownloads/singlefile.php?" . $filename . ""><img style="width:160px; height:160px; border:0px solid #212121;" src="http://coastalgfx.com/uploads/TDMDownloads/images/shots/" . $image . "" /></a></div>n";
    
closedir($handle);
}
?>