Difference between revisions of "Swf.php"

From Organic Design wiki
m
m
 
(155 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{legacy}}
 +
<source lang="php">
 
<?
 
<?
 
# SWF Compiler for XmlWiki Environment
 
# SWF Compiler for XmlWiki Environment
 +
 +
$server = $_SERVER['HTTP_HOST'];
 +
$htdocs = $_SERVER['DOCUMENT_ROOT'];
 +
if ( !ereg( "\\/$", $htdocs ) ) $htdocs .= '/';
 +
 +
xwSetProperty( $properties, 'language', 'raw' );
  
 
# Read in relevent properties
 
# Read in relevent properties
# - also constructs link which requests the raw compiled SWF
+
# - also supplies defaults
$src = "$script?title=$title";
+
# - and constructs link which requests the raw compiled SWF
 +
# - also adds each property as a constant to the script
 +
$t = ereg_replace( '\\.as$', '.swf', $title );
 +
$tag = ereg( '(\\w+)$', $path, $m ) ? $m[1] : 'swf';
 +
$src = "$script?title=$title&xpath:/properties:language=raw&xpath:/properties:data=$tTitle&xpath:/properties:$tag";
 
foreach ( array(
 
foreach ( array(
  
Line 12: Line 24:
 
'background' => 'ffffff',
 
'background' => 'ffffff',
 
'quality' => 'medium',
 
'quality' => 'medium',
'params' => '',
+
'flashvars' => '',
 
'format' => 'embed',
 
'format' => 'embed',
'url' => ''
+
'version' => 6,
 
+
'url' => "www/$t"
) ) {
 
  
if ( !xwGetProperty( $properties, "$path/$key", $$key ) ) $$key = $value;
+
) as $key => $value ) {
$src .= "&xpath:$path/$key=" . ( $key == 'format' ) ? 'raw' : $value;
+
if ( !xwGetProperty( $properties, "xpath:$path/$key", $$key ) ) $$key = $value;
 +
$value = $$key;
 +
$src .= "&xpath:$path:$key=".( ( $key == 'format' ) ? 'raw' : $value );
 +
$article = "$key='$value';\n$article";
 +
}
  
}
+
# Have to add imports to src url
 +
foreach ( xwGetListByTagname( $properties, 'import', $import ) as $image )
 +
$src .= "&xpath:$path:import=$image";
  
 
# if format is embed, just convert the content to an object-tag referring to raw-compiled-swf
 
# if format is embed, just convert the content to an object-tag referring to raw-compiled-swf
 
if ( $format == 'embed' ) {
 
if ( $format == 'embed' ) {
 
$article = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"
 
$article = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"
codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\"
+
codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=$version,0,0,0\"
 
width=$width height=$height id=\"$src\" align=\"\"
 
width=$width height=$height id=\"$src\" align=\"\"
 
type=\"application/x-shockwave-flash\" data=\"$src\">
 
type=\"application/x-shockwave-flash\" data=\"$src\">
<param name=flashvars value=\"$param\">
+
<param name=flashvars value=\"$flashvars\">
 
<param name=movie value=\"$src\">
 
<param name=movie value=\"$src\">
 
<param name=quality value=\"$quality\">
 
<param name=quality value=\"$quality\">
<param name=bgcolor value=\"$bg\">
+
<param name=bgcolor value=\"$background\">
<embed src=\"$src\" quality=\"$quality\" bgcolor=\"$bg\"
+
<embed src=\"$src\" quality=\"$quality\" bgcolor=\"$background\"
width=$width height=$height align="" flashvars=\"$param\"
+
width=$width height=$height align=\"\" flashvars=\"$flashvars\"
 
name=\"$src\" type=\"application/x-shockwave-flash\"
 
name=\"$src\" type=\"application/x-shockwave-flash\"
 
pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />
 
pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />
Line 40: Line 57:
 
}
 
}
  
 +
else {
  
 +
# Compile the script into a binary SWF
 +
# - also builds hard wired common vector shapes (later converted to Ming from SVG)
  
 +
# Check weather required extentions are available (GD2 and Ming)
 +
$ext = get_loaded_extensions();
 +
if ( !in_array( 'ming', $ext ) ) xwMessage( 'Ming extention not loaded! (at least version 0.3 required)', 'red' );
  
# Hard wired common vector shapes (later converted to Ming from SVG)
+
ming_useswfversion( $version );
 +
global $lib, $swf;
 +
$lib = array();
 +
$swf = new SWFMovie();
 +
$swf->setDimension( $width, $height );
 +
eregi( '^#?([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$' , $background, $m );
 +
list(,$r,$g,$b) = $m;
 +
$swf->setBackground( hexdec($r), hexdec($g), hexdec($b) );
 +
$swf->setRate( $fps );
 +
xwMessage( "SWF movie created from \"$title\": $width"." x $height @ $fps".'fps' );
 +
 
 +
# Don't redeclare (happens if more than one compile on same page)
 +
if ( !isset( $GLOBALS[$tTitle] ) ) {
  
# Black block (for spacers and fills)
+
$GLOBALS[$tTitle] = true;
$shape = new SWFShape();
 
$shape->setRightFill( 0, 0, 0 );
 
$shape->movePenTo( 100, 100 );
 
$shape->drawLineTo( -100, 100 );
 
$shape->drawLineTo( -100, -100 );
 
$shape->drawLineTo( 100, -100 );
 
$shape->drawLineTo( 100, 100 );
 
$sym = new SWFSprite();
 
$sym->add( $shape );
 
$sym->nextFrame();
 
$swf->addExport( $sym, 'black' );
 
  
# White block (for spacers and fills)
+
# Make symbol library available at runtime in library array
$shape = new SWFShape();
+
function libAdd( $item, $name ) {
$shape->setRightFill( 255, 255, 255 );
+
global $lib, $swf;
$shape->movePenTo( 0, 0 );
+
$sym = new SWFSprite();
$shape->drawLineTo( 0, 767 );
+
$sym->add( $item );
$shape->drawLineTo( 1023, 767 );
+
$sym->nextFrame();
$shape->drawLineTo( 1023, 0 );
+
$swf->addExport( $sym, $lib[] = $name );
$shape->drawLineTo( 0, 0 );
+
}
$sym = new SWFSprite();
 
$sym->add($shape);
 
$sym->nextFrame();
 
$swf->addExport( $sym, 'white' );
 
  
# Selection area
+
}
$shape = new SWFShape();
 
$shape->setLine( 0, 255, 0, 0 );
 
$shape->movePenTo( 100, 100 );
 
$shape->drawLineTo( -100, 100 );
 
$shape->drawLineTo( -100, -100 );
 
$shape->drawLineTo( 100, -100 );
 
$shape->drawLineTo( 100, 100 );
 
$sym = new SWFSprite();
 
$sym->add( $shape );
 
$sym->nextFrame();
 
$swf->addExport( $sym, 'select' );
 
  
# Triangle
+
# Import images
$shape = new SWFShape();
+
foreach ( xwGetListByTagname( $properties, 'import', $import ) as $image ) {
$shape->setRightFill( 0, 0, 0 );
+
$ref = strtolower( trim( ereg_replace( '^.*/', '', $image ) ) );
$shape->movePenTo( 100, -100 );
+
$ref = ereg_replace( '\\..+$', '', $ref );
$shape->drawLineTo( -100, -100 );
+
$w = $htdocs.'wiki';
$shape->drawLineTo( -100, 100 );
+
shell_exec( "$w/png2dbl $htdocs$image $w/$ref.dbl" );
$shape->drawLineTo( 100, -100 );
+
# investigate creating a unix level pipe and reading a la /dev
$triangle = new SWFSprite();
+
#$img = new SWFBitmap( popen( "$w/png2dbl $w$image $w/stream.dbl", 'rb' ) );
$triangle->add( $shape );
+
$img = new SWFBitmap( fopen( "$w/$ref.dbl", 'rb' ) );
$triangle->nextFrame();
+
libAdd( $img, $ref );
$swf->addExport( $triangle, 'triangle' );
+
}
  
# Linear Gradient
 
$shape=new SWFShape();
 
 
$grad = new SWFGradient();
 
$grad = new SWFGradient();
$grad->addEntry( 0.0, 0, 0, 0, 0xff );
+
for ( $n = 0; $n <= 10; $n++ ) {
$grad->addEntry( 1.0, 0, 0, 0, 0 );
+
$i = ( 2 - log( $n + 1 ) * 0.95 ) * 125;
$fill = $shape->addFill( $grad, SWFFILL_LINEAR_GRADIENT );
+
if ( $i > 255 ) $i = 255; else if ( $i < 0 ) $i = 0;
$fill->scaleTo( 0.097 );
+
$grad->addEntry( $n / 10, 255, 255, 255, $i );
$fill->moveTo( -18, 0 );
+
}
$shape->setRightFill( $fill );
+
$shape = new SWFShape();
$shape->movePenTo( 100, -100 );
 
$shape->drawLineTo( -100, -100 );
 
$shape->drawLineTo( -100, 100 );
 
$shape->drawLineTo( 100, 100 );
 
$shape->drawLineTo( 100, -100 );
 
$sym = new SWFSprite();
 
$sym->add( $shape );
 
$sym->nextFrame();
 
$swf->addExport( $sym, 'linear' );
 
 
 
# Radial Gradient
 
 
$fill = $shape->addFill( $grad, SWFFILL_RADIAL_GRADIENT );
 
$fill = $shape->addFill( $grad, SWFFILL_RADIAL_GRADIENT );
$fill->scaleTo( 0.2 );
+
$fill->scaleTo( 0.17 );
$fill->moveTo( -100, 100 );
 
 
$shape->setRightFill( $fill );
 
$shape->setRightFill( $fill );
$shape->movePenTo( 100, -100 );
+
$shape->drawCircle( 100 );
$shape->drawLineTo( -100, -100 );
+
libAdd( $shape, 'star' );
$shape->drawLineTo( -100, 100 );
 
$shape->drawLineTo( 100, 100 );
 
$shape->drawLineTo( 100, -100 );
 
$sym = new SWFSprite();
 
$sym->add( $shape );
 
$sym->nextFrame();
 
$swf->addExport( $sym, 'radial' );
 
 
 
# Ball
 
$sym = new SWFSprite();
 
$g = new SWFGradient();
 
$g->addEntry( 0.0, 0xff, 0xff, 0xff );
 
$g->addEntry( 1.0, 0, 0, 0 );
 
$ball = new SWFShape();
 
$f = $ball->addFill( $g, SWFFILL_RADIAL_GRADIENT );
 
$f->scaleTo( 0.2 );
 
$f->moveTo( 40, -60 );
 
$ball->setRightFill( $f );
 
$ball->drawCircle( 100 );
 
$sym->add( $ball );
 
$sym->nextFrame();
 
$swf->addExport( $sym, 'ball' );
 
  
 +
# Add compile.as common code
 +
$article = xwArticleContent( 'compile.as' ).$article;
  
 +
# Write exports and make all available at runtime in library array
 +
$swf->writeExports();
 +
$article = 'library=array("'.join( '","', $lib )."\");\n$article";
 +
if ( count( $lib ) ) xwMessage( 'Pre-compiled symbols: "'.join( '", "', $lib ).'"' );
  
 +
# Flashvars aren't working just compile assignments into script for now
 +
foreach( split( '&', $flashvars ) as $var )
 +
if ( ereg( '^(.+)=(.+)$', $var, $m ) )
 +
$article = "$m[1]='$m[2]';\n$article";
  
# Check weather required extentions are available (GD2 and Ming)
+
# Replace #Include statements with content
global $xwDebug;
+
$includes = array();
$ext = get_loaded_extensions();
+
$more = true;
if (!in_array('gd', $ext)) xwMessage("Warning: GD extention not loaded! (image processing unavailable)",'red');
+
while ( $more ) {
else {
+
$more = count( $includes );
$gdi = gd_info();
+
$article = preg_replace(
ereg('([0-9])([0-9.]+)', $gdi['GD Version'], $m);
+
'/^\\s*#include\\s+[\'"](.+?)[\'"].*?$/emi',
if ($xwDebug) xwMessage("GD Version is $m[1]$m[2]");
+
'xwArticleContent($includes["$1"]="$1",true);',
if ($m[1] < 2) xwMessage("Warning: At least GD version 2 required (image processing unavailable)",'red');
+
$article );
 +
$more = count( $includes ) > $more;
 
}
 
}
if (!in_array('ming', $ext)) xwMessage("Ming extention not loaded! (at least version 0.3 required)",'red');
+
if ( count( $includes ) ) xwMessage( 'Included articles: "'.join( '", "', array_keys( $includes ) ).'"' );
elseif ($xwDebug) xwMessage("Ming extention ok.");
 
  
# Override <language> in properties object to 'raw' so no wiki-parsing
+
# Fix the ^= operator which doesn't work in Ming but does in Flash
$node = $properties->create_element('language');
+
$article = preg_replace( '/([a-z_]+[a-z_0-9.]+)\\s*\\^=/i', '$1=$1^', $article );
$node->set_content('raw');
 
$root = $properties->document_element();
 
$lang = xwXPathQuery($properties, "/properties/language");
 
if (is_array($lang) && count($lang)) $lang[0]->replace_node($node);
 
else $root->append_child($node);
 
  
# Get compile parameters from properties object
+
# Add actionscript in frames 1-3
$output = count($results = xwXPathQuery($properties, "$path/output")) ? $results[0]->get_content() : 'html';
+
$swf->add( new SWFAction( $article ) );
$width = count($results = xwXPathQuery($properties, "$path/width")) ? $results[0]->get_content() : 200;
+
$swf->nextFrame();
$height = count($results = xwXPathQuery($properties, "$path/height")) ? $results[0]->get_content() : 150;
+
$swf->add( new SWFAction( 'internal_reduce();' ) );
$frameRate = count($results = xwXPathQuery($properties, "$path/frameRate")) ? $results[0]->get_content() : 25;
+
$swf->nextFrame();
$bg = count($results = xwXPathQuery($properties, "$path/background")) ? $results[0]->get_content() : '#cccccc';
+
$swf->add( new SWFAction( 'gotoAndPlay(2);' ) );
eregi('^#([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$',$bg,$m);
+
$swf->nextFrame();
list(,$r,$g,$b) = $m;
 
  
# Create new SWF
+
if ( $format == 'raw' ) {
ming_useswfversion(6);
+
# Send the raw SWF back to the client and die
$swf = new SWFMovie();
+
ob_end_clean();
$swf->setDimension($width, $height);
+
header( 'Content-Type: application/x-shockwave-flash' );
$swf->setBackground(hexdec($r), hexdec($g), hexdec($b));
+
$swf->output(9);
$swf->setRate($frameRate);
+
die();
if ($xwDebug) xwMessage("SWF movie created ($width"."x$height@$frameRate"."fps)");
+
}
 
+
else {
eval($article);
+
# Write the SWF to $url and render a link to it
ob_end_clean();
+
header('Content-Type: application/x-shockwave-flash');
+
$swf->save( "$htdocs$url" ,9 );
$swf->output(9);
+
$article = "\n[[$title]] successfully compiled to [http://$server/$url $server/$url]\n";
die();
+
}
  
 +
}
 
?>
 
?>
 +
</source>

Latest revision as of 13:18, 6 August 2024

Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, now this page is for historic record only.
<?
# SWF Compiler for XmlWiki Environment

$server = $_SERVER['HTTP_HOST'];
$htdocs = $_SERVER['DOCUMENT_ROOT'];
if ( !ereg( "\\/$", $htdocs ) ) $htdocs .= '/';

xwSetProperty( $properties, 'language', 'raw' );

# Read in relevent properties
# - also supplies defaults
# - and constructs link which requests the raw compiled SWF
# - also adds each property as a constant to the script
$t = ereg_replace( '\\.as$', '.swf', $title );
$tag = ereg( '(\\w+)$', $path, $m ) ? $m[1] : 'swf';
$src = "$script?title=$title&xpath:/properties:language=raw&xpath:/properties:data=$tTitle&xpath:/properties:$tag";
foreach ( array(

	'width' => 550,
	'height' => 400,
	'fps' => 12,
	'background' => 'ffffff',
	'quality' => 'medium',
	'flashvars' => '',
	'format' => 'embed',
	'version' => 6,
	'url' => "www/$t"

	) as $key => $value ) {
		if ( !xwGetProperty( $properties, "xpath:$path/$key", $$key ) ) $$key = $value;
		$value = $$key;
		$src .= "&xpath:$path:$key=".( ( $key == 'format' ) ? 'raw' : $value );
		$article = "$key='$value';\n$article";
		}

# Have to add imports to src url
foreach ( xwGetListByTagname( $properties, 'import', $import ) as $image )
	$src .= "&xpath:$path:import=$image";

# if format is embed, just convert the content to an object-tag referring to raw-compiled-swf
if ( $format == 'embed' ) {
	$article = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"
codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=$version,0,0,0\"
width=$width height=$height id=\"$src\" align=\"\"
type=\"application/x-shockwave-flash\" data=\"$src\">
<param name=flashvars value=\"$flashvars\">
<param name=movie value=\"$src\">
<param name=quality value=\"$quality\">
<param name=bgcolor value=\"$background\">
<embed src=\"$src\" quality=\"$quality\" bgcolor=\"$background\"
width=$width height=$height align=\"\" flashvars=\"$flashvars\"
name=\"$src\" type=\"application/x-shockwave-flash\"
pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />
</object>";
	}

else {

# Compile the script into a binary SWF
# - also builds hard wired common vector shapes (later converted to Ming from SVG)

# Check weather required extentions are available (GD2 and Ming)
$ext = get_loaded_extensions();
if ( !in_array( 'ming', $ext ) ) xwMessage( 'Ming extention not loaded! (at least version 0.3 required)', 'red' );

ming_useswfversion( $version );
global $lib, $swf;
$lib = array();
$swf = new SWFMovie();
$swf->setDimension( $width, $height );
eregi( '^#?([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$' , $background, $m );
list(,$r,$g,$b) = $m;
$swf->setBackground( hexdec($r), hexdec($g), hexdec($b) );
$swf->setRate( $fps );
xwMessage( "SWF movie created from \"$title\": $width"." x $height @ $fps".'fps' );

# Don't redeclare (happens if more than one compile on same page)
if ( !isset( $GLOBALS[$tTitle] ) ) {

	$GLOBALS[$tTitle] = true;

	# Make symbol library available at runtime in library array
	function libAdd( $item, $name ) {
		global $lib, $swf;
		$sym = new SWFSprite();
		$sym->add( $item );
		$sym->nextFrame();
		$swf->addExport( $sym, $lib[] = $name );
		}

	}

# Import images
foreach ( xwGetListByTagname( $properties, 'import', $import ) as $image ) {
	$ref = strtolower( trim( ereg_replace( '^.*/', '', $image ) ) );
	$ref = ereg_replace( '\\..+$', '', $ref );
	$w = $htdocs.'wiki';
	shell_exec( "$w/png2dbl $htdocs$image $w/$ref.dbl" );
	# investigate creating a unix level pipe and reading a la /dev 
	#$img = new SWFBitmap( popen( "$w/png2dbl $w$image $w/stream.dbl", 'rb' ) );
	$img = new SWFBitmap( fopen( "$w/$ref.dbl", 'rb' ) );
	libAdd( $img, $ref );
	}

$grad = new SWFGradient();
for ( $n = 0; $n <= 10; $n++ ) {
	$i = ( 2 - log( $n + 1 ) * 0.95 ) * 125;
	if ( $i > 255 ) $i = 255; else if ( $i < 0 ) $i = 0;
	$grad->addEntry( $n / 10, 255, 255, 255, $i );
	}
$shape = new SWFShape();
$fill = $shape->addFill( $grad, SWFFILL_RADIAL_GRADIENT );
$fill->scaleTo( 0.17 );
$shape->setRightFill( $fill );
$shape->drawCircle( 100 );
libAdd( $shape, 'star' );

# Add compile.as common code
$article = xwArticleContent( 'compile.as' ).$article;

# Write exports and make all available at runtime in library array
$swf->writeExports();
$article = 'library=array("'.join( '","', $lib )."\");\n$article";
if ( count( $lib ) ) xwMessage( 'Pre-compiled symbols: "'.join( '", "', $lib ).'"' );

# Flashvars aren't working just compile assignments into script for now
foreach( split( '&', $flashvars ) as $var )
	if ( ereg( '^(.+)=(.+)$', $var, $m ) )
		$article = "$m[1]='$m[2]';\n$article";

# Replace #Include statements with content
$includes = array();
$more = true;
while ( $more ) {
	$more = count( $includes );
	$article = preg_replace(
		'/^\\s*#include\\s+[\'"](.+?)[\'"].*?$/emi',
		'xwArticleContent($includes["$1"]="$1",true);',
		$article );
	$more = count( $includes ) > $more;
	}
if ( count( $includes ) ) xwMessage( 'Included articles: "'.join( '", "', array_keys( $includes ) ).'"' );

# Fix the ^= operator which doesn't work in Ming but does in Flash
$article = preg_replace( '/([a-z_]+[a-z_0-9.]+)\\s*\\^=/i', '$1=$1^', $article );

# Add actionscript in frames 1-3
$swf->add( new SWFAction( $article ) );
$swf->nextFrame();
$swf->add( new SWFAction( 'internal_reduce();' ) );
$swf->nextFrame();
$swf->add( new SWFAction( 'gotoAndPlay(2);' ) );
$swf->nextFrame();

if ( $format == 'raw' ) {
	# Send the raw SWF back to the client and die
	ob_end_clean();
	header( 'Content-Type: application/x-shockwave-flash' );
	$swf->output(9);
	die();
	}
else {
	# Write the SWF to $url and render a link to it
	
	$swf->save( "$htdocs$url" ,9 );
	$article = "\n[[$title]] successfully compiled to [http://$server/$url $server/$url]\n";
	}

}
?>