kelvinluck.com

a stroke of luck

Embedding flash in TXP

Update:

This is a very old page imported from my previous blog. If there is missing content below or anything that doesn’t make sense then please check the page on my old blog.

Not only does kml_flashembed make it nice and easy to embed Flash files in your textpattern pages – it also produces validating markup and deals nicely with detecting the Flash Player and displaying a message to the user if it isn’t installed. This is thanks to Geoff Stern’s JavaScript which it uses to embed the Flash files.
But… There was a couple of little problems with the plugin. I have error_reporting set to E_ALL on my system and the plugin was throwing a couple of notices due to unset variables.
So I made a few little changes to the code – here it is in it’s new state:

/**
 * Original code from Michael Bester:
 * http://www.kimili.com/journal/32/textpattern-plugin-kimili-flash-embed
 *
 * Slightly adjusted to work when error_reporting is set to E_ALL
 */

function kml_flashembed($atts) {

    if (is_array($atts)) extract($atts);
    $out = array();
    if (!empty($movie) && !empty($height) && !empty($width)) {
        $fversion     = (!empty($fversion)) ? $fversion : 6;
        $target       = (!empty($target)) ? '"'.$target.'"' : '' ;
        $fvars        = isset($fvars) ? explode(";", $fvars) : array();
        $height       = ($height{strlen($height) - 1} == "%") ? '"'.$height.'"' : $height;
        $width        = ($width{strlen($width) - 1} == "%") ? '"'.$width.'"' : $width;
        $id           = isset($id) ? $id : "fm_".$movie; // if no id is provided then default to the name of the swf being embedded which is hopefully unique
        # Convert any quasi-HTML in alttext back into tags
        $alttext      = preg_replace("/{(.*?)}/i", "< >", isset($alttext) ? $alttext : "");

        $out[] = '';
        $out[] = '<script type="text/javascript">';
        $out[] = '    // < ![CDATA[';
        $out[] = '';
        $out[] = '    var flashObject = new FlashObject("'.$movie.'", "'.$id.'", '.$width.', '.$height.', '.$fversion.', "'.(isset($bgcolor) ? $bgcolor : "").'");';
        if (!empty($alttext))        $out[] = '    flashObject.altTxt = "'.$alttext.'"';
        if (!empty($play))           $out[] = '    flashObject.addParam("play", "'.$play.'");';
        if (!empty($loop))           $out[] = '    flashObject.addParam("loop", "'.$loop.'");';
        if (!empty($menu))           $out[] = '    flashObject.addParam("menu", "'.$menu.'");';
        if (!empty($scale))          $out[] = '    flashObject.addParam("scale", "'.$scale.'");';
        if (!empty($quality))        $out[] = '    flashObject.addParam("quality", "'.$quality.'");';
        if (!empty($wmode))          $out[] = '    flashObject.addParam("wmode", "'.$wmode.'");';
        if (!empty($align))          $out[] = '    flashObject.addParam("align", "'.$align.'");';
        if (!empty($salign))         $out[] = '    flashObject.addParam("salign", "'.$salign.'");';
        // Loop through and add any name/value pairs in the  attribute
        for ($i = 0; $i < count($fvars); $i++) {
            $thispair    = trim($fvars[$i]);
            $nvpair      = explode("=",$thispair);
            $name        = trim($nvpair[0]);
            $value       = trim($nvpair[1]);
            $out[]       = '    flashObject.addVariable("'.$name.'", "'.$value.'");';
        }
        $out[] = '    flashObject.write('.$target.');';
        $out[] = '';
        $out[] = '    // ]]>';
        $out[] = '</script>';
        // Add NoScript content
        if (!empty($noscript)) {
            $out[] = '<noscript>';
            $out[] = '    ' . $noscript;
            $out[] = '</noscript>';
        }
        $out[] = '';
    }
    return join("\n",$out);
}

As you can see, the changes are very simple – just making sure that $fvars, $id, $alttext and $bgcolor are defined before they are used.
Here it is in action – embedding a little Flash movie I wrote which connects to the Flickr API to download the 6 most recent photos uploaded to Flickr. Refresh to see more photos – they are uploaded at an alarming rate!

Once again – props to Geoff Stern for his work on this cool and valid way to embed Flash in a page and to Michael Bester for bringing it to TXP in the form of an easy to use plugin.

2 Comments, Comment or Ping

Reply to “Embedding flash in TXP”