22, November 2008

What is this code and this operator? - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: What is this code and this operator?
« previous next »
Pages: [1] 2 Print

Author Topic: What is this code and this operator?  (Read 1408 times)
Bill Gates is my home boy
*****
Gender: Female
Posts: 634
3957 credits
Members referred : 2



« on: Aug 29, 2006, 09:15:41 PM »

Hi All,

Found this code and it does exactly what I want, but I need to execute multiple times on the same page. The idea is to have a group of image files in a directory and use this code to randomly display one. Since I use it up to three times on the same page and it appears to use the time as the seed for the calculation, without modification it shows the same image 3 times.

I added something and it is working now, but would like to understand it better. My solution was to change a line of code and save the file with a new name. I have 3 files and vary which one is called.

Here's the file:

Quote
<?php

    $folder = '.';

    $extList = array();
    $extList['gif'] = 'image/gif';
    $extList['jpg'] = 'image/jpeg';
    $extList['jpeg'] = 'image/jpeg';
    $extList['png'] = 'image/png';

$img = null;

if (substr($folder,-1) != '/') {
    $folder = $folder.'/';
}

if (isset($_GET['img'])) {
    $imageInfo = pathinfo($_GET['img']);
    if (
        isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
        file_exists( $folder.$imageInfo['basename'] )
) {
    $img = $folder.$imageInfo['basename'];
}
} else {
    $fileList = array();
    $handle = opendir($folder);
    while ( false !== ( $file = readdir($handle) ) ) {
        $file_info = pathinfo($file);
        if (
            isset( $extList[ strtolower( $file_info['extension'] ) ] )
) {
            $fileList[] = $file;
        }
    }
    closedir($handle);

    if (count($fileList) > 0) {
        $imageNumber = time() % count($fileList);
        $img = $folder.$fileList[$imageNumber];
    }
}
if ($img!=null) {
    $imageInfo = pathinfo($img);
    $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
    header ($contentType);
    readfile($img);
} else {
    if ( function_exists('imagecreate') ) {
        header ("Content-type: image/png");
        $im = @imagecreate (100, 100)
            or die ("Cannot initialize new GD image stream");
        $background_color = imagecolorallocate ($im, 255, 255, 255);
        $text_color = imagecolorallocate ($im, 0,0,0);
        imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
        imagepng ($im);
        imagedestroy($im);
    }
}
?>

I modifiied this line:

Quote
$imageNumber = time() % count($fileList) + 0.5;

using 0.5 for one file and 1 for another. When I tried 2 it didn't work - which makes me think this sets $imageNumber to a decimal less than 1 but more than 0?

My question is what does the % operator do?

If I understood this code better, maybe there would be a way to only need one file instead of 3.

Thanks.

www.yourmessageconsultant.com Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Hot Glue Failures and Other Turkeys
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6439
39458 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Aug 29, 2006, 09:49:36 PM »

Code:
$imageNumber = time() % count($fileList);

check this from the php manual:
http://nl2.php.net/manual/en/language.operators.arithmetic.php Visit through proxy

EDIT: the code looks like some random image/banner generator...
« Last Edit: Aug 29, 2006, 09:51:35 PM by olaf »


Last blog : Just a better Internet portal provided by Google
Bill Gates is my home boy
*****
Gender: Female
Posts: 634
3957 credits
Members referred : 2



« Reply #2 on: Aug 29, 2006, 09:54:30 PM »

Thanks for the link. That seems vaguely familiar from another language I've worked with.

Any idea on how to modify the script so I don't need 3 different copies to generate 3 different images on the same page?

www.yourmessageconsultant.com Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Hot Glue Failures and Other Turkeys
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6439
39458 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Aug 29, 2006, 09:58:30 PM »


Any idea on how to modify the script so I don't need 3 different copies to generate 3 different images on the same page?

as I can see this script is written to use with different images:
this variable must holder the image name: $_GET['img']


Last blog : Just a better Internet portal provided by Google
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #4 on: Aug 29, 2006, 10:02:20 PM »

here's some code I'm using to display random images:

Code:
<?php 
$DIRNAME 
"./images/randoms/";   // Directory that contains the images

function DisplayRandomImage() 
{
global $DIRNAME;
$msg ""; // IMG tag to be returned
$y 0; // counter for the images
$images = array();       // array to hold the list of images to display randomly
$file_handle "";


if (is_dir($DIRNAME)) 
{
$file_handle=opendir($DIRNAME); 
$y 0;
while ($file readdir($file_handle))  // while there's files to read....

     if (!is_dir($DIRNAME $file)) 
{
  $pieces explode ("."$file);
if ($pieces[0] && $pieces[1])  // filename.ext
{
if (($pieces[1]=="gif"))  // add only .gif files
{
     $images[$y] = $DIRNAME $file
     $y += 1;
}
}
}
}
closedir($file_handle); 
if (count($images) > 0
{
        
srand((double)microtime()*1000000);
$y rand(0,(count($images) - 1));
$msg "<IMG SRC=\"" $images[$y] . "\" width=\"30\" height=\"80\" alt=\"\">";

else 
{
$msg "No image found in \"" $DIRNAME "\"\n";
}

else 
{
$msg "\"" $DIRNAME "\" is not a valid directory!\n";
}
return $msg;

?>

This code is in the top of my page, after that HTML is used to create the page and where I want to display a random image I use
Code:
<?=DisplayRandomImage();?>


Last blog : Are You Stumbling Yet?
Bill Gates is my home boy
*****
Gender: Female
Posts: 634
3957 credits
Members referred : 2



« Reply #5 on: Aug 29, 2006, 10:19:18 PM »

Does that code require building a file list? Many of the others I looked at do.

I can call the routine I use multiple times, unfortunately it is displaying the same image - I would guess the time only changes by seconds and therefore the resulting value is the same.

Do you use this with more than one image on the page?

www.yourmessageconsultant.com Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Hot Glue Failures and Other Turkeys
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #6 on: Aug 29, 2006, 10:46:56 PM »

Code:
$DIRNAME = "./images/randoms/";
refers to the directory the script looks at (in this case mydomain.com/images/randoms/) The scrip retrieves all the .gif images and displays a random one. If you have other images besides .gif you can change:
Code:
if (($pieces[1]=="gif"))
to:
Code:
if (($pieces[1]=="gif") OR ($pieces[1]=="jpg"))
for instance...
hope this helps!

edit: yes I use it with more then one image here: http://www.letsworkonline.com/ Visit through proxy (its for the 2 images on the left next to the google ads) it can sometimes display the same image twice, but I guess that chance lowers when you add more images to your random image folder...
« Last Edit: Aug 29, 2006, 10:55:14 PM by Mind_nl »


Last blog : Are You Stumbling Yet?
Bill Gates is my home boy
*****
Gender: Female
Posts: 634
3957 credits
Members referred : 2



« Reply #7 on: Aug 29, 2006, 11:26:58 PM »

Thank you, I'll have to play with that some.

I noticed when testing that the incidence of the same image coming up goes down as the number of images in the directory goes up.

www.yourmessageconsultant.com Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Hot Glue Failures and Other Turkeys
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #8 on: Aug 29, 2006, 11:29:18 PM »

I like Mind_nl's snippet, it can do what you want, and actually it can be optimized a little.

If you are about to have only the images in the $DIRNAME directory then you can replace this :

Code:
<?php
$pieces 
explode ("."$file);
if (
$pieces[0] && $pieces[1])  // filename.ext
{
if (($pieces[1]=="gif"))  // add only .gif files
{
     
$images[$y] = $DIRNAME $file
    
$y += 1;
}
}

with this :

Code:
<?php
if ( is_file $file ) )
{
     
$images[$y] = $DIRNAME $file
    
$y += 1;
}

This way it will work with any type of file too Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Bill Gates is my home boy
*****
Gender: Female
Posts: 634
3957 credits
Members referred : 2



« Reply #9 on: Aug 29, 2006, 11:59:59 PM »

Cool mod Nik, in my case the images are in a directory by themself.

www.yourmessageconsultant.com Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Hot Glue Failures and Other Turkeys
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #10 on: Aug 30, 2006, 12:04:32 AM »

I just took a closer look and realized that this script is showing one random image.

You want one for as many as you want right?

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #11 on: Aug 30, 2006, 12:12:09 AM »

No I think this is exactly what he wants (from what I read in the first post) one random image displayed, but the script called more then one time so he gets random images in different places on his page, thats just how I use this snippet
Code:
<?=DisplayRandomImage();?>
in different places in my HTML

edit: I like your optimization, but I often keep my photoshop files in the same directory (on my PC) and they sometimes get uploaded to the site too, I don't think displaying a .PSD file will work this way, so I'll leave in the part you optimized out  Wink
« Last Edit: Aug 30, 2006, 12:15:39 AM by Mind_nl »


Last blog : Are You Stumbling Yet?
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #12 on: Aug 30, 2006, 12:17:17 AM »

No I think this is exactly what he wants (from what I read in the first post) one random image displayed, but the script called more then one time so he gets random images in different places on his page, thats just how I use this snippet
Code:
<?=DisplayRandomImage();?>
in different places in my HTML

Correct, except than the he Smiley

BTW I think this is a nice idea for the code gallery. I will make a snippet for random images tomorrow and post it there Smiley

The only bad thing about this snippet is that when you use it for more than one images, it can show the same image twice or more times....

But I will fix that Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #13 on: Aug 30, 2006, 12:49:54 AM »

Yes, like I said before: it sometimes shows the same image more than once. I'd be very interested if you can fix that!


Last blog : Are You Stumbling Yet?
Bill Gates is my home boy
*****
Gender: Female
Posts: 634
3957 credits
Members referred : 2



« Reply #14 on: Aug 30, 2006, 01:23:48 AM »

<Spoken in her deepest manliest voice...  Kiss >

Yes, Nik, I would love it if you could fix that too.

So, glad I came here and asked about this.  Smiley

www.yourmessageconsultant.com Visit through proxy, providing online content and printed marketing materials.
www.helpforwebbeginners.com Visit through proxy, Tutorials and how to's for new  webmasters.
www.CraftyTips.com Visit through proxy, a unique Arts & Crafts Directory
www.nocans.com Visit through proxy - Pet Food Recipe Site
www.petsiteguides.com Visit through proxy - A New Pet Directory

Last blog : Hot Glue Failures and Other Turkeys
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #15 on: Aug 30, 2006, 11:24:37 AM »

Ok, I will mark this thread as unread, and later on I will post you the code fragment you need Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #16 on: Aug 31, 2006, 01:45:54 PM »

Ok the snippet is done, with some extra feautures Smiley

http://www.webdigity.com/index.php?action=tutorial;code=32 Visit through proxy

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #17 on: Aug 31, 2006, 02:08:27 PM »

Good work Nick! I'll be replacing my current random image script with this version.
One thing though: the code for displaying one random image is the same as for displaying more then one, looks like the second code line is missing something. Should that be:
Code:
<?php 
     
echo RandomImage(3);
to display 3 random images?


Last blog : Are You Stumbling Yet?
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8249
42481 credits
Members referred : 3



« Reply #18 on: Aug 31, 2006, 02:14:07 PM »

lol, that happens when you copy/paste Tongue

I have fixed that...

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy or twitter Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Bill Gates is my home boy
*****
Gender: Female
Posts: 634
3957 credits
Members referred : 2



« Reply #19 on: Sep 01, 2006, 03:40:50 AM »

Nice job Nik.

I do have a couple of questions.

I would use the code multiple times in the same page, but not with t