Forum Moderators: coopster

Message Too Old, No Replies

random pic of the day...

I need help with "upgrading" the feature.

         

Snickers

9:17 am on Jun 19, 2005 (gmt 0)

10+ Year Member



Here is some piece of code. It will show random image from a folder, regardless the name of the files:

<?php
function rndImage(){
$folder = "images"; /// Change this to your img folder
$imgFiles = Array();
$dirHandler = opendir($folder) or die("cannot opendir");
while($myImages = readdir($dirHandler)){
if($myImages == "." ¦¦ $myImages == ".."){continue;}
array_push($imgFiles,$myImages);
}
$imgNo = rand(0, count($imgFiles));
$imgName = $folder."/".$imgFiles[$imgNo];
echo $imgName;
}
?>

when you wan to put random img:

<IMG SRC="<?php rndImage()?>">

What I want to do is:

Lets say I have a blog looking site, and on the right side on the top there is this box.

I would like to show the random picture there, and here comes the part where I need help.

Is that anyway to make sure that it gets"rezized" tempoary when showed in that box?

Lets say max: 145px times 177px and THEN when you click on it, it will lead you to the full image...

HELP?

Blackie

11:21 am on Jun 19, 2005 (gmt 0)

10+ Year Member



The easiest way is to simply change this line:
<IMG SRC="<?php rndImage()?>">
to
<IMG SRC="<?php rndImage()?>" width=145 target=_blank>

This will ensure the image resizing as well as opening it in a new window fullsized.

Snickers

1:04 pm on Jun 19, 2005 (gmt 0)

10+ Year Member



Okay, I'm really not good at this..

I made a test by creating a new file called: testing.php

and I wrote this:

<?php
function rndImage(){
$folder = "images"; /// Change this to your img folder
$imgFiles = Array();
$dirHandler = opendir($folder) or die("cannot opendir");
while($myImages = readdir($dirHandler)){
if($myImages == "." ¦¦ $myImages == ".."){continue;}
array_push($imgFiles,$myImages);
}
$imgNo = rand(0, count($imgFiles));
$imgName = $folder."/".$imgFiles[$imgNo];
echo $imgName;
}

<IMG SRC='<?php rndImage()?>' width=145 target=_blank>

?>

it says error in line 7?

what did I do wrong?

jatar_k

5:38 pm on Jun 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what is the actual error that it gives you?

Snickers

5:51 pm on Jun 19, 2005 (gmt 0)

10+ Year Member



Parse error: parse error, unexpected T_STRING in /home2/clickspa/public_html/testing.php on line 7

jatar_k

6:08 pm on Jun 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is this line 7?

if($myImages == "." ¦¦ $myImages == ".."){continue;}

no white space or anything that could throw off my count?

I can't see it myself

Snickers

8:29 am on Jun 20, 2005 (gmt 0)

10+ Year Member



can some one help?

anshul

8:52 am on Jun 20, 2005 (gmt 0)

10+ Year Member



I hope you are maintaining a list of images ( urls ) in db or an array. And you generated a random number to choose a random image. And you are maintaining two versions of your all images, one is constant less dimensions and other a full-size image.

So just the problem is to trap 'width' and 'height' of full images ( different images are of different dimensions ). so can either store that in db or if you can get it from filesystem image file ( using gd library perhaps? ) I can't try code for you :)

MattHock

7:42 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Is that text you posted exactly how the file is? If so, you don't escape from the PHP before outputting the HTML - you need to move the?> before the img tag.

Snickers

5:55 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



<?php
function rndImage(){
$folder = "images"; /// Change this to your img folder
$imgFiles = Array();
$dirHandler = opendir($folder) or die("cannot opendir");
while($myImages = readdir($dirHandler)){
if($myImages == "." ¦¦ $myImages == ".."){continue;}
array_push($imgFiles,$myImages);
}
$imgNo = rand(0, count($imgFiles));
$imgName = $folder."/".$imgFiles[$imgNo];
echo $imgName;
}
?>
</head>
</body>
</IMG SRC="<?php rndImage()?>" width=145 target=_blank>
</head>
</body>

This is how I have written it, and it still says:

Parse error: parse error, unexpected T_STRING in /home2/clickspa/public_html/admin.php on line 7

MattHock

6:10 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



Try changing
if($myImages == "." ¦¦ $myImages == ".."){continue;}
to
if(($myImages == ".") or ($myImages == "..")){continue;}

bcolflesh

3:37 pm on Jun 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also - change:

$imgNo = rand(0, count($imgFiles));

to

$imgNo = rand(0, count($imgFiles)-1);