This Snippet is a Youtube video resizer for MODX Evolution. Basically, this Snippet overrides the width and height parameters of a embed Youtube video.
This Snippet can also get the path for the thumbnail from the embed Youtube video.
Snippet developed by: Lorenzo Stanco.
1. Installation
To install Youtube Snippet, go to "Elements > Manage Elements >
Snippets > New Snippet" and fill in the fields as follows:
Snippet Name: youtube
Snippet Description: Resize video and get video thumbnail from Youtube.
Snippet code (php):
<?php
/**
* Snippet Name: youtube
* Short Desc: Resize video and get video thumbnail from Youtube
* Author: Lorenzo Stanco <www.lorenzostanco.com>
* Version: 0.0.3
* Last Edited: 25-09-2011
*/
// ---------------------------------------------------
// Config
// ---------------------------------------------------
// $type: [string] 'video' (for video embedding) or 'url' (for video thumb)
// $tvname: [string] YouTube code
// $width: [int] width dimension in pixel
// $height: [int] height dimension in pixel
//
// [[youtube? &tvname=`[*video01:escapemodx*]` &type=`video` &width=`500` &height=`400`]]
// [[youtube? &tvname=`[*video01:escapemodx*]` &type=`url`]]
//-----------------------------------------------------
$video = str_replace(
array('##w#', '##e#', '##a#'),
array('?' , '=', '&' ),
$tvname
);
if ($type == 'video') {
if (!empty($width) && !empty($height)) {
$pattern_w = '/ width=["]?([0-9]+)["]?/i';
$pattern_h = '/ height=["]?([0-9]+)["]?/i';
$video = preg_replace($pattern_w, ' width="'.intval($width).'"', $video);
$video = preg_replace($pattern_h, ' height="'.intval($height).'"', $video);
}
$video = str_replace('<embed ', '<embed wmode="transparent" ', $video);
$video = preg_replace('/www.youtube.com\/(embed|v)\/([^" \/]+)"/i', 'www.youtube.com/$1/$2?wmode=transparent"', $video);
}
if ($type == 'url') {
$pattern_code_1 = '/\\/v\\/([^"\\/?]+)/i';
$pattern_code_2 = '/\\/embed\\/([^"\\/?]+)/i';
$code = array();
preg_match($pattern_code_1, $video, $code);
if (!empty($code[1])) $code = $code[1];
else { preg_match($pattern_code_2, $video, $code); $code = $code[1]; }
$video = 'http://img.youtube.com/vi/' . $code . '/0.jpg';
}
return $video;
?>
Now we have to install a PHx Extender:
Snippet Name: phx:escapemodx
Snippet Description: Escape special embed characters
Snippet code (php):
<?php
return str_replace(
array('?' , '=', '&' ),
array('##w#', '##e#', '##a#'),
$output
);
?>
2. Parameter &type=`video`
You can use this parameter in this way:
1) Create a Textarea TV and assign it to your Template.
2) Open a Resource and insert the Youtube embedded code in the Textarea TV.
3) Add the Youtube Snippet Call on your Template:
[[youtube? &tvname=`[*video01:escapemodx*]` &type=`video` &width=`500` &height=`400`]]
3. Parameter &type=`url`
You can use this parameter in the same way of the previous one. Just change the &type and remove the width and height parameters:
<img src="[[youtube? &tvname=`[*video01:escapemodx*]` &type=`url`]]" alt="youtube video" />

