FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Matrix Digital Rain animation in PHP





Mgccl

You need GIFEncoder class to do this Smile
Code:
include('GIFEncoder.class.php');
$msg = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*()-_=+`[]{};:?/.,<>\\|"';
$length = strlen($msg);
$max_x = 400; //image x size
$max_y = 400; //image y size
$font_file = 'cour.ttf';//font, best monospace
$font_size = 10;//font site
$grid = 10;//gridient of the fonts
$measuer = imagettfbbox(10,0,$font_file,'1');
$space = 5;//space between letters
$char_height = $measuer[1] - $measuer[7] + $space;
$char_width = $measuer[2] - $measuer[0] + $space;
$i = 0;

$colorimage = imagecreatetruecolor(1,1);
//this is the part where the colors are made
//you can use other ways to create better looking color scheme
while($i<$grid){
    $color[] = imagecolorallocate($colorimage, 0, 255/($i+1), 0);
    ++$i;
}
//ENOUGH TEXT TO FILL THE PAGE.
$width = ceil($max_x/$char_width);
$height = ceil($max_y/$char_height);

$x = 0;
while($x < $width){
    $y = 0;
    while($y < $height){
        $text[$x][$y] = $msg[rand(0,$length)];
        ++$y;
    }
    $drop[$x] = rand(0, $width);
    ++$x;
}
$i = 0;
while($i<$height+$grid){
    $image[] = imagecreatetruecolor($max_x, $max_y);
    $x = 0;
    while($x < $width){
        $y = 0;
        while($y < $drop[$x]){
            if($drop[$x]-$y<=$grid){
                imagettftext($image[count($image)-1], $font_size, 0, $x*$char_width, ($y+1)*$char_height-$space, $color[$drop[$x]-$y], $font_file, $text[$x][$y]);
            }
            ++$y;
        }
        if($drop[$x]<$height+$grid){
            ++$drop[$x];
        }else{
            $drop[$x] = 0;
        }
        ++$x;
    }
    ++$i;
       
}

ob_start();
$i = 0;
$count = count($image);
while($i<$count){
    imagegif($image[$i]);
    $imagedata[$i] = ob_get_contents();
    imagedestroy($image[$i]);
    ob_clean();
    ++$i;
}
$gif = new GIFEncoder(
                            $imagedata,
                            100,
                            0,
                            2,
                            0, 0, 0,
                            "bin"
        );
Header ( 'Content-type:image/gif' );
echo    $gif->GetAnimation ();

[FuN]goku
sweeeeet Very Happy
ncwdavid
Thats pretty cool. Did you come up with it yourself?
Dougie1
Wow. Mind if I edit your code a bit so that I can get like I want it. That is very cool!
Mgccl
Dougie1 wrote:
Wow. Mind if I edit your code a bit so that I can get like I want it. That is very cool!

no problem Smile everything on my site is GPL Smile
dxhunter
wow very nice Smile
Dougie1
Mgccl wrote:
Dougie1 wrote:
Wow. Mind if I edit your code a bit so that I can get like I want it. That is very cool!

no problem Smile everything on my site is GPL Smile
Thanks! I won't release it as my own or anything like that. I will always leave you feedback.

EDIT: Wow. This script takes a lot of memory!!!
Mgccl
Dougie1 wrote:
Mgccl wrote:
Dougie1 wrote:
Wow. Mind if I edit your code a bit so that I can get like I want it. That is very cool!

no problem Smile everything on my site is GPL Smile
Thanks! I won't release it as my own or anything like that. I will always leave you feedback.

EDIT: Wow. This script takes a lot of memory!!!


Sure it is... it have to create each one of the frames...
but I think I got an idea... it was generating all the image at one time then store each one as gif binary. I just need to make it generate one frame and store as gif binary and destroy the image, then it should use way less memory.

I will do it tomorrow... and I will also make a even better matrix digital rain effect Smile

EDIT:
new version, less memory usage(A LOT LESS)
Code:
ob_start();
$msg = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*()-_=+`[]{};:?/.,<>\\|"';
$length = strlen($msg);
$max_x = 400; //image x size
$max_y = 400; //image y size
$font_file = 'cour.ttf';//font, best monospace
$font_size = 10;//font site
$grid = 10;//gridient of the fonts
$measuer = imagettfbbox(10,0,$font_file,'1');
$space = 5;//space between letters
$char_height = $measuer[1] - $measuer[7] + $space;
$char_width = $measuer[2] - $measuer[0] + $space;
$i = 1;
$colorimage = imagecreatetruecolor(1,1);
$color[1] = imagecolorallocate($colorimage, 255, 255, 255);
while($i<$grid){
   
   $color[] = imagecolorallocate($colorimage, 0, 255-20*$i, 0);
   ++$i;
}
imagedestroy($colorimage);
//ENOUGH TEXT TO FILL THE PAGE.
$width = ceil($max_x/$char_width);
$height = ceil($max_y/$char_height);


$x = 0;
while($x < $width){
   $y = 0;
   while($y < $height){
      $text[$x][$y] = $msg[rand(0,$length)];
      ++$y;
   }
   $drop[$x] = rand(0, $width);
   ++$x;
}
$i = 0;
while($i<$height+$grid){
   $image = imagecreatetruecolor($max_x, $max_y);
   $x = 0;
   while($x < $width){
      $y = 0;
      
      while($y < $drop[$x]){
         if($drop[$x]-$y<=$grid){
            imagettftext($image, $font_size, 0, $x*$char_width, ($y+1)*$char_height-$space, $color[$drop[$x]-$y], $font_file, $text[$x][$y]);
         }
         ++$y;
      }
      if($drop[$x]<$height+$grid){
         ++$drop[$x];
      }else{
         $drop[$x] = 0;
      }
      ++$x;
   }
   imagegif($image);
   $imagedata[$i] = ob_get_contents();
   imagedestroy($image);
   ob_clean();
   ++$i;
}

$gif = new GIFEncoder(
                            $imagedata,
                            100,
                            0,
                            2,
                            0, 0, 0,
                            "bin"
        );
Header ( 'Content-type:image/gif' );
echo    $gif->GetAnimation ();
//echo memory_get_usage();
Related topics
[php scripts ] phpweather&email
Final Fantasy Advent Children
Help ... Scripting PHP
php image exif functions
The Matrix has you...
Help-Digital Camera selection
Need some comments to My WebSite
Brain Or MIND?
1st Annual ZogDog Animation Contest
3D Animation Movies
Flash, PHP and MySQL
[help]fatal error eso dice mi web
integrer une animation flash dans php
Cool Game Making Software !!!
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.