probably pretty easy solved php issue ...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
perik78
Forum Newbie
Posts: 8
Joined: Mon Dec 28, 2009 5:20 pm

probably pretty easy solved php issue ...

Post by perik78 »

s
Last edited by perik78 on Sun Apr 25, 2010 4:51 pm, edited 1 time in total.
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Re: probably pretty easy solved php issue ...

Post by Technocrat »

How about something like:

Code: Select all

<?php
function getImage() {  //Added
/* The default folder with images */
$settings['img_folder'] = 'bildermini/';

/* File types (extensions) to display */
$settings['img_ext'] = array('.jpg','.gif','.png');

/*
   How to display the images?
   0 = print just the image path (for includes), like: images/test.jpg
   1 = redirect to the image, when using: <img src="randim.php" />
*/
$settings['display_type'] = 1;

/* Allow on-the-fly settings override? 0 = NO, 1 = YES */
$settings['allow_otf'] = 1;

/*******************************************************************************

/* Override type? */
if ($settings['allow_otf'] && isset($_GET['type']))
{
	$type = intval($_GET['type']);
}
else
{
	$type = $settings['display_type'];
}

/* Override images folder? */
if ($settings['allow_otf'] && isset($_GET['folder']))
{
	$folder = htmlspecialchars(trim($_GET['folder']));
    if (!is_dir($folder))
    {
    	$folder = $settings['img_folder'];
    }
}
else
{
	$folder = $settings['img_folder'];
}

/* Make sure images fodler ends with an '/' */
if (substr($folder,-1) != '/')
{
	$folder.='/';
}

/* Get a list of all the image files */
$flist = array();
foreach($settings['img_ext'] as $ext)
{
    $tmp = glob($folder.'*'.$ext);
    if (is_array($tmp))
    {
    	$flist = array_merge($flist,$tmp);
    }
}

/* If we have any images choose a random one, otherwise select the "noimg.gif" image */
if (count($flist))
{
	$src = $flist[array_rand($flist)];
}
else
{
	$src = 'noimg.gif';
}

/* Output the image according to the selected type */
if ($type)
{
	header('Location:'.$src);
    exit();
}
else
{
	return $src; //Changed
}

} //Added
?>

Code: Select all

    
<?php include 'randim.php'; ?>
<ol>
    click for bigger image:
    <li>
<?php $_GET['type']=0; $_GET['folder']='bilderstora'; $img = getImage();  ?>
    <a class="fancy" rel="fancy" href="<?php echo $img; ?>"><img border='0' src="<?php echo $img; ?>"></a></p>
    </li>

    <li>
<?php $_GET['type']=0; $_GET['folder']='bilderstora'; $img = getImage(); ?>
    <a class="fancy" rel="fancy" href="<?php echo $img; ?>"><img border='0' src="<?php echo $img; ?>"></a></p>
    </li>

    <li>
<?php $_GET['type']=0; $_GET['folder']='bilderstora'; $img = getImage(); ?>
    <a class="fancy" rel="fancy" href="<?php echo $img; ?>"><img border='0' src="<?php echo $img; ?>"></a></p>
    </li>
    </ol>
Post Reply