PEAR::SWF updating… read and modify swf with PHP

I’ve just begun to modify the old PHP class flashHeader. Now using the PEAR::File class for a better file I/O processes.
Basically now, other than returning all the main information from an swf file, such as the bg color, framerate, player version, compression, protection used, movie size.., it allows to modify some of those parameters.
For example it can change the framerate, background, protection… I’m also working in order to allows the class modify some other swf tags in runtime..
Working example

Here the source file

<?php
// include the PEAR package
require “File/File_SWF.php”;
$flash = new SWF(“source.swf”);
if(
$flash->is_valid()){
$stat = $flash->stat(); // this give all the info
// and also..
$fps = $flash->getFrameRate();
$size = $flash->getMovieSize();
$bg = $flash->getBackgroundColor();
$prot = $flash->getProtected();
$compr = $flash->getCompression();
$version = $flash->getVersion();

// they can be changed…
$flash->setFrameRate(60);
$flash->setProtected(1);
$flash->setBackgroundColor(51,51,204);
$flash->setCompression(1);

// and write a new file…
$flash->write(“./new_one.swf”,1);
}
?>

Ho to get bytesTotal from a dynamic generated content in PHP

Think you want to load from Flash some dynamically generated images from PHP (maybe using GD..), or also some else dynamic content..
Think at this simple code:

/**** MovieClip Loader listener ****/
loaderListener = {
	onLoadStart : function(){
		trace('load started!');
		trace(" ----------------------------------- ")
	},
	onLoadProgress : function(target, bLoaded, bTotal){
		trace(bLoaded + "/" + bTotal)
	},
	onLoadComplete : function(target){
		trace(" ----------------------------------- ")
		trace("complete: " + target.getBytesTotal());
		trace(" ----------------------------------- ")
	}
}

// I just add the rand variable to prevent caching...
movie_path = "http://local/ob_start.php?file=C:/DSCF0182.JPG&rand=" + new Date().getTime()
var mcLoader = new MovieClipLoader()
mcLoader.loadClip(movie_path, loader)
mcLoader.addListener( loaderListener )

Now, if your PHP code is written in this way (just a image copy and resize using the GD 2.x library):


<?php
// Get the required image from GET
$image = $_GET['file'];$size = getimagesize($image);
// create an empty image and copy the original
$dst_img = imagecreatetruecolor($size[0]/4, $size[1]/4);
$src_img = imagecreatefromjpeg($image);
// resize image
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $size[0]/4, $size[1]/4, $size[0], $size[1]);
// start output content
header("Content-type: image/jpeg");
// display the image
imagejpeg($dst_img, "", 80);
?>

You will be noticed that the bytesTotal variable in flash is always ‘0’!
This could be a problem if you want to use any kind of graphic preloader for this such of things…
However with some little more code it’s always possible to get in flash (and not only from flash) the exact content bytes lenght.
Try now this:

<?php
// Turn on output buffering
ob_start();
// Get the required image from GET
$image = $_GET['file'];
$size = getimagesize($image);
// create an empty image and copy the original
$dst_img = imagecreatetruecolor($size[0]/4, $size[1]/4);
$src_img = imagecreatefromjpeg($image);
// resize image
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $size[0]/4, $size[1]/4, $size[0], $size[1]);
// start output content
header("Content-type: image/jpeg");
// display the image
imagejpeg($dst_img, "", 80);
// send the content lenght to output
header("Content-Length: ". ob_get_length());
// flush the output
ob_end_flush();
?>

A PHP domxml class in flash style!

Some days ago Daniele, a friend of mine, point my attention to an opensource project hosted on sourceforge that is very interesting for PHP/Flash guys 🙂
This module permit to do an xml parsing, in PHP, using a dom xml in the same way we already do in flash.

Here a little example i made using this class:

<?
// include the php module
include('lib.xml.inc.php');
// load an external xml source
$test = new XML('test.xml');

// make a simple parsing, in AS style ;)
$childs = $test->firstChild->childNodes;
for($a = 0; $a < count($childs); $a++){	
echo "nodeName " . $childs[$a]->nodeName . "<br />";	
echo "nodeValue " . $childs[$a]->firstChild->nodeValue . "<br />";
}
?>

as you can see, we’re already familiar with firstChild, nodeName, nodeValue… etc..
I always hate the PHP SAX parser, and always always prefer a dom xml parser, much more simple to create and to read.