SWFObject for PHP
A simple PHP class for embedding Flash objects statically into your pages.
Static embedding has several advantages over the JavaScript injection technique:
- Works when JavaScript is unavailable
- Is more robust; unreliance on JavaScript means if an earlier script error halts the interpreter, the SWFObject doesn’t die as well
- Search engines can access statically embedded Flash content (text and links) more effectively
- Static embedding makes Flash possible in RSS feeds
I also like the way this method avoids the mess of printing out client script variables, when feeding in from a server-side data source for example.
As you can see it’s fairly simple, but gives you a lot of control over what parameters and attributes are applied. It generates the same ‘flash satay’ as the SWFObject 2.0 static publishing method.
class FlashObject {
public $attributes = array();
public $params = array();
public $variables = array();
public $path;
public $width;
public $height;
public $id;
public static $default_alt_content = '<a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a>';
function __construct ($path = '#', $width = 100, $height = 100, $alt_content = '', $id = '') {
$this->path = $path;
$this->width = $width;
$this->height = $height;
$this->alt_content = !empty($alt_content) ? $alt_content : $this->default_alt_content;
$this->id = !empty($id) ? ' id="' . $id . '"' : '';
}
function setAltContent ($str) {
$this->alt_content = $str;
}
function setVariables ($arr) {
$this->variables = $arr;
}
function setParams ($arr) {
$this->params = $arr;
}
function setAttributes ($arr) {
$this->attributes = $arr;
}
function setId ($id) {
$this->id = $id;
}
function get () {
$attributes = '';
$params = '';
$variables = array();
foreach ($this->attributes as $key=>$val) {
if ($key !== 'id') { $attributes .= ' ' . $key . '=' . '"' . $val . '"'; }
}
foreach ($this->params as $key=>$val) {
$params .= "<param name=\"{$key}\" value=\"{$val}\" />";
}
foreach ($this->variables as $key=>$val) {
$variables[] = $key . '=' . urlencode($val);
}
if ( count($variables) ) {
$params .= "<param name=\"flashvars\" value=\"" . implode('&', $variables) . "\" />";
}
$str = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\"{$this->width}\" height=\"{$this->height}\"{$this->id}{$attributes}>";
$str .= "<param name=\"movie\" value=\"{$this->path}\" />";
$str .= $params;
$str .= '<!--[if !IE]>-->';
$str .= "<object type=\"application/x-shockwave-flash\" data=\"{$this->path}\" width=\"{$this->width}\" height=\"{$this->height}\"{$attributes}>";
$str .= $params;
$str .= '<!--<![endif]-->';
$str .= $this->alt_content;
$str .= '<!--[if !IE]>-->';
$str .= '</object>';
$str .= '<!--<![endif]-->';
$str .= '</object>';
return $str;
}
function render () {
echo $this->get();
}
}
Example use:
$swiff = new FlashObject('path/to/file.swf', 200, 300, 'Alternative Content here', 'Myid');
$swiff->setVariables(array(
'hello' => 'world',
'myVar' => '200'
));
$swiff->setParams(array(
'wmode' => 'transparent'
));
// Print out directly
$swiff->render();
// Or assign for later use
$mySwiff = $swiff->get();
Tagged with: