MandrivaUsers.org : PHP script for random pics - MandrivaUsers.org

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

PHP script for random pics

#1 User is offline   javaguy 

  • Awesome
  • Group: Members
  • Posts: 366
  • Joined: 23-January 04

Posted 25 March 2007 - 11:13 PM

Greg2 requested this in another thread, so here's one way to make a script that displays a random picture. In this case I have installed the Coppermine photo gallery (a nifty little piece of PHP freeware), so the names of all the photo files are stored in a database table called cpg135_pictures. You don't have to have all that set up, though, just some kind of database from which you can select a random file name. In fact, you don't really even need a database--you could use the PHP directory functions to list all the files in the image directory and then pick one at random.

Anyway, here it is. Put the following text on your web server in a file called randompic.php or something like that, make sure it has execute permission and that your web server has PHP installed, and when you navigate to http://mydomain.com/randompic.php you'll get a random picture.

<?php
header("Content-type: image/png");


//Select random thumbnail file.
$link = mysql_connect('localhost', 'dbusername', 'myawesomepassword')
	or die('Could not connect: ' . mysql_error());
mysql_select_db('benjamin_album') or die('Could not select database');

// Performing SQL query
$query = "SELECT filepath, filename FROM cpg135_pictures ORDER BY rand() limit 1";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$line = mysql_fetch_array($result, MYSQL_ASSOC);
$filepath = 'album/albums/' . $line['filepath'];
$filename = $line['filename'];
mysql_free_result($result);
mysql_close($link);

$thumbnail_img = imagecreatefromjpeg($filepath . '/' . $filename);

imagejpeg($thumbnail_img);

imagedestroy($thumbnail_img);

?>



[moved from Software by spinynorman]
0

#2 User is offline   Greg2 

  • 45% Platinum
  • Group: Global Moderator
  • Posts: 2,412
  • Joined: 06-April 04

Posted 25 March 2007 - 11:24 PM

Thanks! I have a new toy to play with. :D
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users