Announcement

Collapse
No announcement yet.

Random PHP image

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Random PHP image

    Hi there,

    I want to be able to call am image using a php script (so, <img src="random.php">).. but as you see, it has to be random. I dont have access to GD (its a windows box, plus all the GD dlls ive tried just gave me errors).

    Basically I just need it to read from a file, a random image name and use it. This is what I've come up with so far:

    Code:
    <?
    header("Content-type:  image/gif"); 
    passthru("type //gota be random here//"); 
    ?>
    hehe. Ive tried doing some stuff, but the headers always get sent before the image one.. so it doesnt work can you help?

    thanks ^_^

  • #2
    If you are using a mysql database here is something to play with.

    PHP Code:
    <?
    $user
    ="usename to login";
    $password="password to login";
    $database "your database";
    $table "your table";

      
    mysql_connect("localhost","$user","$password")or die ("no mysql connection");
      
    mysql_select_db("$database") or die ("Could not get database<p>");
     
    $result mysql_query("select * from $table order by rand() limit 1") or die ("error:".mysql_error());
      while (
    $row=mysql_fetch_array($result))
      {
            
    $image=($row['image']);
            
    $image "img_dir/$image";
            print 
    "$image";
      }
    ?>

    Comment


    • #3
      untested Quikie Code:
      PHP Code:
      <?php

      header
      ("Content-type:  image/gif");

      // list all gifs
      $dir = `dir /b *.gif`;
      // split it into an array
      $dir explode("\n"$dir);

      // seed the rand()
      srand((double)microtime()*1000000);
      // pull a random image
      $i rand(0count($dir)-1);

      // print it
      passthru("type " $dir[$i]);

      ?>

      Comment


      • #4
        Thanks both of you, both of them are great for what im doing

        But one other question (actually two)... how can I grab the contents of a text file and overlay it over a transparent gif(or png)?

        I may need GD for it, so do you know where I can get a good windows GD dll?

        Comment


        • #5
          You will need some kind of GD library. Maybe ImageMagik (I've never played with it).

          I have no idea where to get this stuff for Windows.

          Comment


          • #6
            well, I grabed ImageMagik.. but it seems the only program it lets me use in there is mogrify, and It doesnt even seem to do the operations when I tell it to >_<

            Comment


            • #7
              if you downloaded php with exstentions for win32 box, you will be able to run GD. it has all the files you need.

              Comment


              • #8
                Dont you have to compile the 4mb version though? Im not sure how to do that

                Comment


                • #9
                  no you download the one for windows which is in zip format I think. don't have to compile anything.

                  Comment


                  • #10
                    Thanks very much. I did it and got it all running. I even got the GD image I wanted to work! Im going to start playing with all these new extentions

                    Thanks again

                    Comment


                    • #11
                      you're quite welcome

                      Comment

                      widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                      Working...
                      X