1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Random Image Script not so random

Discussion in 'Programming' started by 313i, Oct 13, 2005.

  1. #1
    Ok, I have a random image script set up on one of my sites. the script works by droping it into the image directory and is called by linking the image location to the random.php script.

    I have 4 of the same script in the directory but 60% of the time the script will return the same image for atleast 2 of the images being called.

    How do I change the below script to always call a different image then the 1st, 2nd and 3rd script?

    <?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( $extList[ strtolower( $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( $extList[ strtolower( $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 (100, 100)
                or die ("Cannot initialize new GD image stream");
            $background_color = imagecolorallocate ($im, 255, 255, 255);
            $text_color = imagecolorallocate ($im, 0,0,0);
            imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
            imagepng ($im);
            imagedestroy($im);
        }
    }
    ?> 
    Code (markup):

    Thanks in advance!
    SEMrush
     
    313i, Oct 13, 2005 IP
    SEMrush
  2. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I can't offer any help on your script but I use the following:
    <?php 
    	
        $Ad[0] = '<a href="http://www.domain.com" TARGET="_blank"><img src="images/xxxx.gif" alt="xxxxx" border="0"></a>';
        $Ad[1] = '<a href="http://www.domain.com" TARGET="_blank"><img src="images/xxxxxx.gif" alt="xxxx" border="0"></a>';
        $Ad[2] = '<a href="http://www.domain.co.uk" TARGET="_blank"><img src="images/xxxx.gif" alt="xxxxxxx" border="0"></a>';    
        $Ad[3] = '<a href="http://www.domain.co.uk" TARGET="_blank"><img src="images/xxxxx.gif" alt="xxxxxx" border="0"></a>';
        $Ad[4] = '<a href="http://www.domain.com" TARGET="_blank"><img src="images/xxxx.gif" alt="xxxxx" border="0"></a>';
        $Ad[5] = '<a href="http://www.domaincom" TARGET="_blank"><img src="images/xxxx.gif" alt="xxxx" border="0"></a>';
        
        $Weight[0]=3;
        $Weight[1]=3;
        $Weight[2]=3;
        $Weight[3]=3;
        $Weight[4]=3;
        $Weight[5]=3;
           
        $sum =0;
        for($i=0;$i<count($Weight);$i++)
        	$sum+=$Weight[$i];
        $ShowAd = rand(0, $sum - 1);
        for($i=0;$i<count($Weight);$i++)
        {
        	if($ShowAd<=$Weight[$i])
        	{
        		$ShowAd=$i;
        		break;
        	}
        	else
        		$ShowAd-=$Weight[$i];
        }
        echo $Ad[$ShowAd];
        ?>
    PHP:
    You can change the weight to make images show up more or less.
     
    dave487, Oct 18, 2005 IP
  3. 313i

    313i hummmmmm. No clue

    Messages:
    1,338
    Likes Received:
    73
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks dave487 I'll try it out, Looks like it should work alittle better
     
    313i, Oct 21, 2005 IP
  4. a3196

    a3196 Well-Known Member

    Messages:
    586
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    108
    #4
    How can I display two or three ads??
     
    a3196, Oct 23, 2005 IP
  5. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #5
    To alter my script for different numbers of adverts you remove both the
    $Ad[5] = '<a...........
    and the
    $Weight[5]=3; lines as appropriate.
     
    dave487, Oct 24, 2005 IP
  6. a3196

    a3196 Well-Known Member

    Messages:
    586
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    108
    #6
    I mean I want to display more than just one advert at a time....

    Like

    <Advert> - <Advert> - <Advert>

    How can I change the number of advert that will be displayed at once?
     
    a3196, Oct 24, 2005 IP
  7. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #7
    The simplest way would be to just run the script twice, you would need to have 2 different lists of adverts to prevent the same one being shown twice though.
     
    dave487, Oct 25, 2005 IP
  8. mrspeed

    mrspeed Guest

    Messages:
    193
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I was looking at your original code
    $imageNumber = time() % count($fileList);

    What happens if you try
    $imageNumber = mt_rand (0,count($fileList));
     
    mrspeed, Oct 25, 2005 IP
  9. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #9
    The code I posted is more complex than it needs to be as I sometimes need to show adverts more than others.

    If you want to show adverts the same number of times (approx.) you can just use:

    
    $adverts=array( "htmlstring1","htmlstring2","htmlstring3","htmlstring4","htmlstring5"  ); 
    srand(time()); 
    shuffle($adverts); 
    for ($i=0;$i<5;++$i) // display five adverts
     echo "$adverts[$i]";
    
    PHP:
    If you only want to show 2 adverts from the list of 5 then you just change the 5 to a 2 etc
     
    dave487, Oct 26, 2005 IP