RandIm - PHP random image script

INDEX

» ACKNOWLEDGEMENT

Please support our free PHP scripts by linking to us or sending a donation. Thank you!

» Installation

Please take 5 minutes to read the installation instructions carefully and thoroughly! Doing so will ensure a proper and easy installation.

 

#1: Quick install

  1. Connect with FTP to the public folder of your server where the rest of your Web site is.

    TIP: The public folder is usually called "public_html", "www", "site" or "htdocs".

    TIP: Learn how to FTP files by reading this FTP and CHMOD tutorial

  2. Create a new folder where you will install this script. Name it anything you like, for example, "randim" or "random_images".
    Example: /public_html/randim
    Corresponding URL: https://www.example.com/randim

  3. Upload all Randim files to your server. PHP files must be transferred in ASCII mode and images in BINARY mode.

    TIP: Most FTP clients will automatically select the proper transfer mode so you needn't worry about it.

 
That's it. To test if Randim works fine on your server, open randim/test.php in your browser, for example:
https://www.example.com/randim/test.php

Your browser should show a sample page with 4 random images displayed. Try refreshing the page to see images change.

 

#2: Using Random Image script

Now that the script is up and running it's time to choose the images that should be randomly displayed. You can do so by uploading all your images to your "randim/images" folder.

Alternatively you can set RandIm to use images from any other folder of your choice by changing the default settings (read more about it further down).

To display a random image on your website using the default settings simply use the URL of randim.php as image SRC (source) address, for example:

<img src="https://example.com/randim/randim.php" />

As simple as that. By default, RandIm will randomly choose from any .jpg, .gif or .png files located in the "randim/images" folder.

 

Find this script useful? Please support our free PHP scripts by linking to us or sending a donation. Thank you!

 

#3: Modifying default settings

To modify default settings open randim.php in a plain text editor (like Notepad or Wordpad on Windows, DO NOT use Microsoft Word!) and scroll down to where it says SETTINGS. Available settings are:

    Variable Description/Setup
    $settings['img_folder'] Name of the folder where RandIm will choose images (files) from by default. You can set it to any folder on your server, use relative or absolute (root) path to the folder you wish.

    For example if your images are located in a folder called "photos" located outside the "randim" folder, you would set it to:
    $settings['img_folder'] = '../photos/';
    $settings['img_ext'] File types (extensions) RandIm will choose from. You can add new extensions by adding ,'.extension' to the list.

    Examples:

    $settings['img_ext'] = array('.jpg');
    Effect: RandIm will choose from JPG files only

    $settings['img_ext'] = array('.jpg','.gif','.png','.tga','.bmp');
    Effect: RandIm will choose from JPG, GIF, PNG, TGA and BMP file types

    $settings['img_ext'] = array('.doc', '.pdf');
    Effect: RandIm will choose DOC and PDF files

    $settings['display_type'] If set to 1 RandIm will redirect to the chosen file. This is useful when you use RandIm from HTML pages as shown before (<img src="randim.php" />). It can also be used to create random file downloads if used in an anchor link, like this:

    <a href="https://example.com/randim/randim.php">Random file</a>


    If set to 0 RandIm will print path to the selected file. This mode should be used if you are using RandIm on a PHP page and want to use RandIm as a PHP include, for example:

    <img src="<?php include 'randim.php'; ?>">

    $settings['allow_otf'] If set to 1 RandIm will allow Changing settings on-the-fly (see below). Set to 0 to only allow hard-coded settings (as set in the randim.php file).

    For security purposes if $settings['allow_otf'] is set to 1 RandIm will only work with local folders and not remote ones (so people can't use your script to for example display images from their folder).

Save changes and upload the edited randim.php file to your server.

 

#4: Changing settings on-the-fly

You can also modify RandIm settings on-the-fly simply by passing proper values to the script using query string (when using RandIm as URL) or variables (when using RandIm as a PHP include).

For changing settings on-the-fly to work variable $settings['allow_otf'] must be set to 1 (see above)!

Operating mode ($settings['display_type']) can be changed using $_GET['type'] variable and images folder using $_GET['folder'] variable. This might sound a bit confusing, but here are a few examples:

If you want to display one random image from folder "images" and one from folder "images2" you could use this code:

<img src="https://example.com/randim/randim.php?folder=images" />
<img src="https://example.com/randim/randim.php?folder=images2" />

The same thing if you are using RandIm as a PHP include:

<img src="<?php $_GET['folder'] = 'images'; include 'randim.php'; ?>">
<img src="<?php $_GET['folder'] = 'images2'; include 'randim.php'; ?>">

If your $settings['display_type'] is set to 1 and default folder set to "images" but you would like to also use randim.php as include and choose a file from folder "../photos" try this:

<img src="<?php $_GET['type'] = 0; $_GET['folder'] = '../photos'; include 'randim.php'; ?>">

And if your display_type is set to 0 by default (using RandIm as a PHP include) you can override the default settings and use randim.php as an URL too:

<img src="https://example.com/randim/randim.php?type=1&folder=../photos" />

The same code is used in the test.php file, have a look if interested.

Are links not your thing? You can attribute by sending a small donation. Any amount will help keep PHPJunkyard website and free scripts alive. Thank you!

Link suggestions

1. Simple text link
Random image

2 "Powered by" simple text link
Powered by Random image

3. Text link with description
Random image - get this and other free PHP scripts from PHPJunkyard.

TIP: You can use one of the PHPJunkyard banners that come with RandIm (check inside images folder) for the link or get more link suggestions on the PHPJunkyard link to us page!

» HELP and Troubleshooting

For up-to-date help and troubleshooting guides, please visit:
PHPJunkyard Support

» Stay updated!

Join our FREE newsletter and we will notify you about new scripts, new versions of the existing scripts, and other important news from PHPJunkyard.
Click here for more info

» Get more useful FREE scripts!

Looking for more PHP scripts? Here is a list of PHPJunkyard FREE scripts:

» CHANGELOG

Changes in 1.2
- don't cache Javascript

 

© Copyright PHP Scripts from PHPJunkyard 2004-2022. All rights reserved.