<?
 // Let's build an Hello World SWF in a few bytes :)
 // (c)2008 by Paul TOTH <tothpaul -at- free.fr> 
 
 // based on : Sphing (Swf with PHp without mING)
 // Project homepage: http://sphing.sourceforge.net/
 // Copyright 2002 Igor Clukas <igor -at- clukas -dot- de>

 
 define('SWFEndTag',"\x00\x00");
 define('acEndAction',  "\x00");
 define('acGetVariable',"\x1C");
 define('acSetMember',  "\x4F");
 define('acCallMethod', "\x52");
   
// Hello World !   
 $swf=SWFFile(
       9,       // Flash Version 9
       320,200, // Width, Height
       32,      // Frame rate
       1,       // FrameCount
       SWFFileAttributes(0)      // no flags
      .SWFBackground(128,128,255)  // background RGB color
      .SWFDoAction(              // initial script
        acPushData(200,320,0,0,0,'field1',6 ,'_root') // push all we need ( 6 = createTextField args count)
       .acGetVariable                                 // get('_root')
       .acPushData('createTextField')
       .acCallMethod                                  // <stack>=_root.createTextFied('field1',depth,x,y,width,height);
       .acPushData('text','Hello World!')
       .acSetMember                                   // <stack>.text='Hello World!'
       .acEndAction
      )
      //.SWFShowFrame() // not needed
      .SWFEndTag
     );
      
 $f=fopen('hello.swf','w');
 fwrite($f,$swf);
 
 Header('Content-Type: application/x-shockwave-flash');     
 die($swf); 
 
function SWFFile( $version, $width, $height, $rate, $frames, $content) {
 $content=SWFRect(0,0,20*$width,20*$height) // frame size in twips
         ."\x00".chr($rate) // frame rate 8.8 fixed
         .SWFshort($frames) // frame count
         .$content; // actuel content         
 return 'CWS'.chr($version)          // header
       .SWFlong(strlen($content)+8)  // content length+header length
       .gzcompress($content); 
}

function SWFRect($x1,$y1,$x2,$y2) {
 $bits=max(bits($x1),bits($y1),bits($x2),bits($y2));
 return SWFbin(
         sprintf('%05b',$bits)
        .SWFbits($x1,$bits)
        .SWFbits($x2,$bits)
        .SWFbits($y1,$bits)
        .SWFbits($y2,$bits)
       ); 
}

function SWFFileAttributes($flags) {
 return SWFshort((69<<6)+4).SWFlong($flags);
}

function SWFBackground($r,$g,$b) {
 return SWFshort((9<<6)+3).chr($r).chr($g).chr($b);
}

function SWFShowFrame() {
 return SWFshort(1<<6);
}

function SWFDoAction($actions) {
 return SWFshort((12<<6)+0x3F).SWFlong(strlen($actions)).$actions;
}

function acPushData() {
 $data='';
 $args=func_get_args();
 foreach ($args as $arg) {
  if (is_int($arg) ) $data.=chr(7).SWFlong($arg); else
  if (is_bool($arg)) $data.=chr(5).($arg?chr(1):chr(0)); else
  if (is_string($arg)) $data.=chr(0).$arg.chr(0); else die('type error');
 }
 return "\x96".SWFshort(strlen($data)).$data;
 
}

function bits($value) {
 return strlen(decbin(abs($value)))+1;
}

function SWFbits($value,$bits) {
 return substr(sprintf("%0${bits}b", $value), -$bits);
}

function SWFbin($bin) {
 $result='';
 while (strlen($bin) % 8 != 0) $bin .= '0';
 for ($i=0; $i < strlen($bin) / 8; $i++) {
  $result.=chr(bindec(substr($bin, $i*8, 8)));
 }	
 return $result;
}

function SWFshort($value) {
 return chr($value & 0xff) . chr(($value >> 8) & 0xff);
}

function SWFlong($value) {
 return pack('V', $value);
}

?>