Pixelpost

Authentic Photoblog Flavour


Go Back   Pixelpost Forum > DEVELOPMENT > Hacks and Modifications

Post Reply
 
Thread Tools
  #1  
Old 02-13-2007, 07:11 PM
trebor31 Offline
pp regular
 
Join Date: Jan 2007
Posts: 21
How to display a random image - works on external sites, blogs, etc

Having searched here for a couple of days I have found quite a few people asking how to display a random image, but not found a solid answer... I now have a solution.

Please note I am talking about simply displaying 1 random image - for example on a launch page, your blog etc etc.

This does not provide a clickable link that choses an image for you. (this is covered by Ramin's Add-on: http://www.pixelpost.org/v1/index.ph...ads&details=41)

You need the following php:

PHP Code:
<?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( 
$extListstrtolower$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( 
$extListstrtolower$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 (100100)
            or die (
"Cannot initialize new GD image stream");
        
$background_color imagecolorallocate ($im255255255);
        
$text_color imagecolorallocate ($im0,0,0);
        
imagestring ($im255,  "IMAGE ERROR"$text_color);
        
imagepng ($im);
        
imagedestroy($im);
    }
}

?>
Paste this into a new text file and call it: random.php.
Place this file inside your "images" folder in your PP installation.

On your template (any template, not just image_template) add the following link wherever you want the image to appear:

<img src="images/random.php" alt="" style="height: 1px; width: 1px;" />

(change the width and height attributes to your image size!)

It will simply pick an image (at random!) from your "Images" folder each time the page is loaded.

You can link to the php from an external site, blog etc. (I have tested it on Movable Type, Blogger and from a friends site) and it will display the image.

And it will work exactly the same with your thumbnails folder if you prefer to display a random thumbnail (provided you put random.php in your Thumbnails folder and change the link obviously )

Hope it is useful to someone - it sure helped me!

Rob

A caviat - I cant provided answers to changes to the php code as I havent a clue about coding php!

Last edited by trebor31; 02-15-2007 at 05:46 PM.
Reply With Quote
  #2  
Old 02-15-2007, 08:50 AM
viesturs Offline
forum loafer
 
Join Date: Feb 2007
Posts: 5
thanks!
remark - i assume, you meant line:
<img src="images/random.php" alt="" style="height: 1px; width: 1px;" />
instead of:
<img src="images/rotate.php" alt="" style="height: 1px; width: 1px;" />
?

also - question - now all the images are random, not only the first one. what should be done if i want only the first picture to be a random one and after this random image comes the latest added images!?
can you help me?

thanks!
Reply With Quote
  #3  
Old 02-15-2007, 05:55 PM
trebor31 Offline
pp regular
 
Join Date: Jan 2007
Posts: 21
Quote:
Originally Posted by viesturs View Post
thanks!
remark - i assume, you meant line:
<img src="images/random.php" alt="" style="height: 1px; width: 1px;" />
instead of:
<img src="images/rotate.php" alt="" style="height: 1px; width: 1px;" />
?
You are absolutely right - thanks for spotting it - have changed my original post.

Quote:
also - question - now all the images are random, not only the first one. what should be done if i want only the first picture to be a random one and after this random image comes the latest added images!?
Bear with me - it should be easy...off the top of my head you could either play with the Next link or set up the page showing the random image as in effect a splash page which has the "Next" link leading to the normal index page. I will check it out and post here once I have figured it out.
Reply With Quote
  #4  
Old 02-16-2007, 06:15 AM
viesturs Offline
forum loafer
 
Join Date: Feb 2007
Posts: 5
thanks a lot! looking forward !
Reply With Quote
  #5  
Old 02-17-2007, 01:38 AM
trebor31 Offline
pp regular
 
Join Date: Jan 2007
Posts: 21
Sorry it has taken me a while - I am no expert and have to figure things out by trial and error.

Unfortunately simply playing around with the "Next" Link, I couldnt get it to work. And having read around here even the authors suggest creating a second index or Splash Page as the best way to do this. See this thread:
http://forum.pixelpost.org/showthread.php?t=5714

Put your PixelPost Instalation in a folder.
Leave the "random.php" in the images folder of PixelPost as suggested above.

Create a splash page and put it one server level above this pixel post folder:

- Use "image_template: as the basis for the splash page so everything looks the same, but change any <PIXEL_POST_TAGS> to normal HTML tags.
- Where you want the random image, include the link as above to "random.php" -
<img src="/yourpixelpostfolder/images/random.php" alt="" style="height: 1px; width: 1px;" />
- Where you have your Previous/Next Links, drop the Previous Link, and change the Next Link to <a href="/yourpixelpostfolder/index.php">Next</a>
- Make sure that the CSS link in the head is adjusted to read the CSS of your Pixelpost.

This splash page doesnt have to be a server root level, it just above your pixel post folder.

Hope that helps.
Reply With Quote
  #6  
Old 02-21-2007, 09:35 AM
viesturs Offline
forum loafer
 
Join Date: Feb 2007
Posts: 5
thanks a lot. i have understood the idea. but now the problem is that i'm not sure how to make a splash page. if i use a script for index2.php - what should i do in order to tell pixelpost to open "index2.php" instead of "index.php" by default when opening my webpage?
thanks,
.v
Reply With Quote
  #7  
Old 02-21-2007, 09:46 AM
Dkozikowski's Avatar
Dkozikowski+ Offline
Team Pixelpost
 
Join Date: Oct 2005
Posts: 1,855
Send a message via AIM to Dkozikowski
For what you want to do, the easiest thing would be to place all your pixelpost files in a subfolder. If you do this, make sure you correct any links (if any) and update the options page in the admin interface to point to the new URL.

Then, just make a new index.php file in the root folder containing your splash page. From this page, link to your pixelpost index.php contained in your subfolder.

Just as a caution. Moving your pixelpost install will break any permalinks and bookmarks / etc.
Reply With Quote
  #8  
Old 02-21-2007, 01:20 PM
viesturs Offline
forum loafer
 
Join Date: Feb 2007
Posts: 5
thanks! what do you mean by "just make a new index.php file in the root folder containing your splash page"? do i have to insert html cocde into new index.php?
also - do i have to reinstall pixelpost after dropping it unto a subfolder or everything will work well without reinstall?
Reply With Quote
  #9  
Old 02-21-2007, 04:51 PM
trebor31 Offline
pp regular
 
Join Date: Jan 2007
Posts: 21
Hi
I have attached a jpg which shows how your server should be laid out.



You will see that there is an "index.php" in the folder "Your Server" (the root level). This should be a duplicate of the original index.php from Pixel Post, BUT, change any Pixel Post tags to html. On this one use the "random.php" link at the start of this thread. Then make the "Next" link and/or the image, link to the index.php that is inside the pixel post folder - therefore, using the attached example:- <a href="/pixelpost/index.php">.

Also, make sure that the index.php in "Your server" has the correct link to the CSS file inside your PixelPost templates, so that it all looks the same.

You shouldnt need to re-install your pixel post after dropping it into a folder, but you will have to update the path in the admin section to reflect the new location. (If it does break for whatever reason, just re-install anyway as it is quick and easy - I have had to do it several times after changing my mind about things!)

Hope that helps.
Reply With Quote
  #10  
Old 02-23-2007, 12:20 PM
viesturs Offline
forum loafer
 
Join Date: Feb 2007
Posts: 5
thanks guys for helping me out!
Reply With Quote
Post Reply


Thread Tools




All times are GMT. The time now is 03:17 AM.

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. | Style Design: d3 designs