Embedded Video


This is a first try at an action / plugin embedding a video.

For now, the action embeds youtube and vimeo videos as easy as: {{video url="http://www.youtube.com/watch?v=-dnL00TdmLY"}}

Other parameters other than url are type and id (replaces url) and width and height (defaults at 440 / 330).

Save the following file as video.php under plugins/actions folder:

<?php
/**
 * Display a video.
 *
 * @package     Actions
 * @version     $Id$
 * @license     http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @filesource
 *
 * @uses    Wakka::htmlspecialchars_ent()
 * @uses    Wakka::cleanUrl()
 *
 * @input   string  $url    optional: URL of video to be embedded (either this or both type and id must be set)
 * @input   string  $type   optional: type of video (youtube, vimeo)
 * @input   string  $id     optional: id of video resource
 * @input   int     $height optional: height of the video
 * @input   int     $width  optional: width of the video
 * @output  string  object element
 */


// defaults
$url = '';
$type = 'youtube';
$id = '';
$width = 440;
$height = 330;
$errors = '';

// params
if (is_array($vars))
{
    foreach ($vars as $param => $value)
    {
        if ($param == 'src' && $vars['url'] == '') {$vars['url']=$value;}
        if ($param == 'type') {$type = $this->htmlspecialchars_ent($vars['type']);}
        if ($param == 'id') {$id = $this->htmlspecialchars_ent($vars['id']);}
        if ($param == 'width' && (int)$vars['width'] > 0) {$width = (int)$vars['width'];}
        if ($param == 'height' && (int)$vars['height'] > 0) {$height = (int)$vars['height'];}
    }
}
if(isset($vars['url'])) $url = $this->cleanUrl(trim($vars['url']));

if($id == '' && $url != '') {
    if ( preg_match('/^.*www.(.*?)\..*?\/(.*)$/', $url, $matches) )  {
        // $matches[1] is domain; youtube or vimeo etc. - $matches[2] is everything after the first slash.
        $type = $matches[1];
        if ( $type == 'youtube' ) {
            if ( preg_match('/watch\?v=(.*?)(\&.*)?$/', $matches[2], $id_and_flags) ) {
                $id = $id_and_flags[1];
            } else {
                $errors .= "<ins>bad format on youtube arguments:</ins> $matches[2]";
            }
        } else if ( $type == 'vimeo' ) {
            $id = $matches[2];
        } else {
            $errors .= "<ins>type: <$type> not known or bad id: <$id> in url.</ins>";
        }
    } else {
        $errors .= '<ins>no id and error in url:</ins> ' . $url . "\n";
    }
}

// building output
if ( $id != '' ) {
    if ($type == 'youtube' && $id != '') {
        $output  = '<object width="' . $width . '" height="' . $height . '">' . "\n";
        $output .= '<param name="movie" value="http://www.youtube.com/v/';
        $output .= $id . '&hl=en&fs=1"></param>' . "\n";
        $output .= '<param name="allowFullScreen" value="true"></param>' . "\n";
        $output .= '<param name="allowscriptaccess" value="always"></param>' . "\n";
        $output .= '<embed src="http://www.youtube.com/v/';
        $output .= $id . '&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' . $width . '" height="' . $height . '">';
        $output .= '</embed>' . "\n" . '</object>' . "\n";
    } else if ($type == 'vimeo') {
        $output = '<object width="' . $width . '" height="' . $height . '">' . "\n";
        $output .= '<param name="allowfullscreen" value="true" />' . "\n";
        $output .= '<param name="allowscriptaccess" value="always" />' . "\n";
        $output .= '<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=';
        $output .= $id . '&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1" />' . "\n";
        $output .= '<embed src="http://vimeo.com/moogaloop.swf?clip_id=';
        $output .= $id . '&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="' . $width . '" height="' . $height . '">';
        $output .= '</embed>' . "\n" . '</object>' . "\n";
    }
} else { $output = $errors; }

echo $output;
?>



CategoryDevelopmentActions CategoryUserContributions
Comments
Comment by AliOli
2010-12-07 18:40:18
Hi Tormod! I tried this action and got it to work easily. But unfortunately it did not produce valid XHTML. So I have adapted it to use Vimeo videos in an iframe. Added my modification as VimeoAction and have attributed you. Thanks for getting me started!
Comment by TormodHaugen
2010-12-08 03:56:32
Glad you could make something out of it. :D
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki