Page 1 of 1

Creating a random image and background

Posted: Thu Jan 18, 2007 1:46 pm
by Mystical
I've made the script to get a random image using Curl because my host has opendir() disabled but I've not been able to come up with a method for creating a random image and have a background that goes with it.

like
background1.gif
image1.gif

or
background10.gif
image10.gif

any ideas would be nice.

thanks,
mystical

Code: Select all

<?php
      function random_logo( $logos )
      {
      $total_logos = count($logos );
      $current_logo = mt_rand(0, $total_logos-1);
      return $logos[$current_logo];
      }

      $this_logo = array(
            "IMG GOES HERE",
            "IMG GOES HERE",
            "IMG GOES HERE"
            );

$image = random_logo($this_logo);
$file_info = pathinfo($image);
$ch = curl_init();
$timeout = 20;
curl_setopt ($ch, CURLOPT_URL, $image);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
$content_type = 'Content-type: ' .$image_info['extension'];
header ($content_type);
echo $file_contents;
?>

Posted: Thu Jan 18, 2007 4:29 pm
by farinspace
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


here is the concept assuming you have all your images defined, why not create an array of your images

Code: Select all

$bg= array
(
    "background1.gif"
    , "background2.gif"
    , "background3.gif"
);

$img= array
(
    "image1.gif"
    , "image2.gif"
    , "image3.gif"
);

// get images as such
$n= rand(0,count($bg));
$bgSelected= $bg[$n];
$imgSelected= $img[$n];

// read it in and output it to page ...

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Jan 18, 2007 5:17 pm
by Mystical
well that won't work because I'm using the php files as an index.php then i have the folders as background.png and forground.png

Posted: Fri Jan 19, 2007 12:26 am
by farinspace
then i may have misunderstood, if you can explain, maybe i can try again ...

Posted: Fri Jan 19, 2007 1:57 pm
by Mystical
well I ended up just using somehting like, using this code as index.php placed in a folder named bg.gif

Code: Select all

<?php
$now = date('i');
function odd($number) { return($number & 1); }
if ( odd( $now ) )
{$image = "http://www.the-hacks.com/layouts/thsbg.png"; }
else
{  $image = "http://img95.imageshack.us/img95/651/tsbgms8.png"; }

$file_info = pathinfo($image);
$ch = curl_init();
$timeout = 20;
curl_setopt ($ch, CURLOPT_URL, $image);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
$content_type = 'Content-type: ' .$image_info['extension'];
header ($content_type);
echo $file_contents;
?>
and this code as index.php in a folder named main.gif

Code: Select all

<?php
$now = date('i');
function odd($number) { return($number & 1); }
if ( odd( $now ) )
{$image = "http://www.the-hacks.com/layouts/ths.png"; }
else
{  $image = "http://img248.imageshack.us/img248/5908/thldw2.png"; }

$file_info = pathinfo($image);
$ch = curl_init();
$timeout = 20;
curl_setopt ($ch, CURLOPT_URL, $image);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
$content_type = 'Content-type: ' .$image_info['extension'];
header ($content_type);
echo $file_contents;
?>