Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Random image from url parameter?

    I'm generating a random image using an array e.g.

    PHP Code:
    $pics = array(
        
    'image1',
        
    'image2',
        
    'image3'
    );

    $randompic $pics[array_rand($pics)];

    echo 
    "<img src=\"images/".$randompic.".jpg\" />"
    On the page that displays the image (lets call it index.php) I echo different content based on the parameter in the URL e.g.
    index.php?cat=group1

    I now need to do the same for the random image generation.
    I can use $_GET in randomimage.php to select a random image based on the group e.g.

    PHP Code:
    $category $_GET['cat'];

    $group1 = array(
        
    'image1',
        
    'image2',
        
    'image3'
    );

    $group2 = array(
        
    'image4',
        
    'image5',
        
    'image6'
    );

    if (
    $category=='group1'){
    $randompic $group1[array_rand($group1)];}
    elseif (
    $category=='group2'){
    $randompic $group2[array_rand($group2)];}

    echo 
    "<img src=\"images/".$randompic."\" />"

    However, I dont know how to do the echo on index.php rather than randomimage.php.

    I tried

    echo "<img src=\"randompic.php?cat=group1\" />";

    but looking at the html, it shows the above as a literal string.

    I'm guessing I need to set the header on randomimage.php to header("Content-type: image/JPEG"); to just display the image with no html?

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    have you tryed including the image so that it acts as the real image even tho its not.

    example:
    PHP Code:
    $pics = array(
               
    'image1',
               
    'image2',
               
    'image3'
               
    );
    $randompic $pics[array_rand($pics)];
    include(
    $randompic.'.jpg'); 
    you will need to use the full path such as ../images/$randompic.jpg
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I'll give that a go and post back shortly

  • #4
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    function. create a file, say "includes.php". include this page on every page.

    PHP Code:
    <?php
    function randomImage($category) {

        
    $safe= array('group1''group2''group3''group4'); /*add each "gallery here */
        
        
    if(in_array($category,$safe)) {        
        
    /*if requested image is in the array */
            
    $group1 = array(
                
    'image1',
                
    'image2',
                
    'image3'
            
    );
        
            
    $group2 = array(
                
    'image4',
                
    'image5',
                
    'image6'
            
    );
        
            if (
    $category=='group1'){
                
    $randompic $group1[array_rand($group1)];
            }
            elseif (
    $category=='group2'){
                
    $randompic $group2[array_rand($group2)];
            }
        
            return 
    "<img src=\"images/".$randompic."\" />";  
            
        } else {
            return 
    'Invalid URL specified.'// default content if the $category doesn't exist
        
    }
    }


    /* use like this */
    //url=index.php?category=group1
    echo randomImage($_GET['category']);
    //returns <img src="images/image4" /> from $group1
    don't forget you need a file extension.
    Last edited by met; 03-25-2010 at 07:54 PM.

  • Users who have thanked met for this post:

    htcilt (03-25-2010)

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks Met, that approach works perfectly

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    For anyone that wants to read any directory and display random images ....

    PHP Code:
    <?php

    // directory name where they are stored.
    $dir_name="photos";

    // put these types of images into an array.
    $images array_merge(
    glob("$dir_name/*.jpg"),
    glob("$dir_name/*.png"),
    glob("$dir_name/*.gif")
    );

    // Randomize the array.
    shuffle($images);

    // Display the first 5 random images.
    echo"
    <img src='$images[0]' /><br />
    <img src='$images[1]' /><br />
    <img src='$images[2]' /><br />
    <img src='$images[3]' /><br />
    <img src='$images[4]' /><br />
    "
    ;

    // If you would rather display all of them.
    //foreach($images as &$img){
    //echo "<img src='$img' /><br />";
    //}

  • #7
    Regular Coder
    Join Date
    Sep 2007
    Posts
    238
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Also a nice solution. Thank you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •