You are here

function video_filter_process in Video Filter 5.2

Same name and namespace in other branches
  1. 5 video_filter.module \video_filter_process()
  2. 6.3 video_filter.module \video_filter_process()
  3. 6 video_filter.module \video_filter_process()
  4. 6.2 video_filter.module \video_filter_process()
1 call to video_filter_process()
video_filter_filter in ./video_filter.module
Implementation of hook_filter().

File

./video_filter.module, line 62

Code

function video_filter_process($text, $format = -1) {
  if (preg_match_all('/\\[video(\\:(.+))?( .+)?\\]/isU', $text, $matches_code)) {
    foreach ($matches_code[0] as $ci => $code) {
      $video = array(
        'source' => $matches_code[2][$ci],
        'width' => variable_get('video_filter_width_' . $format, 400),
        'height' => variable_get('video_filter_height_' . $format, 400),
        'autoplay' => variable_get('video_filter_autoplay_' . $format, 0),
        'related' => variable_get('video_filter_related_' . $format, 1),
      );

      // Insert default values to this settings array so we don't get any "Undefined index" warnings from PHP.
      $video += array(
        'ratio' => 0,
        'control_bar_height' => 0,
        'align' => NULL,
      );

      // Pick random out of multiple sources separated by ','
      if (strstr($video['source'], ',')) {
        $sources = explode(',', $video['source']);
        $random = array_rand($sources, 1);
        $video['source'] = $sources[$random];
      }

      // Load all codecs
      $codecs = module_invoke_all('codec_info');

      // Find codec
      foreach ($codecs as $codec_name => $codec) {
        if (!is_array($codec['regexp'])) {
          $codec['regexp'] = array(
            $codec['regexp'],
          );
        }

        // Try different regular expressions
        foreach ($codec['regexp'] as $delta => $regexp) {
          if (preg_match($regexp, $video['source'], $matches)) {
            $video['codec'] = $codec;
            $video['codec']['delta'] = $delta;
            $video['codec']['matches'] = $matches;
            $video['codec']['codec_name'] = $codec_name;

            // used in theme function
            $video['codec']['control_bar_height'] = 0;

            // default
            break 2;
          }
        }
      }

      // Codec found
      if ($video['codec']) {

        // Override default attributes
        if ($matches_code[3][$ci] && preg_match_all('/\\s+([a-zA-Z_]+)\\:(\\s+)?([0-9\\/]+)/i', $matches_code[3][$ci], $matches_attributes)) {
          foreach ($matches_attributes[0] as $ai => $attribute) {
            $video[$matches_attributes[1][$ai]] = $matches_attributes[2][$ai];
          }
        }

        // Use configured ratio if present, use that from the codec otherwise
        $ratio = 0;
        if ($video['ratio'] && preg_match('/(\\d+)\\/(\\d+)/', $video['ratio'], $tratio)) {

          //validate given ratio parameter
          $ratio = $tratio[1] / $tratio[2];
        }
        else {
          $ratio = $video['codec']['ratio'];
        }

        // Default value for control bar height
        $control_bar_height = 0;
        if ($video['control_bar_height']) {

          // respect control_bar_height option if present
          $control_bar_height = $video['control_bar_height'];
        }
        elseif ($video['codec']['control_bar_height']) {

          // respect setting provided by codec otherwise
          $control_bar_height = $video['codec']['control_bar_height'];
        }

        // Resize to fit within width and height repecting aspect ratio
        if ($ratio) {
          $scale_factor = min(array(
            $video['height'] - $control_bar_height,
            $video['width'] / $ratio,
          ));
          $video['height'] = round($scale_factor + $control_bar_height);
          $video['width'] = round($scale_factor * $ratio);
        }
        $video['autoplay'] = (bool) $video['autoplay'];
        $video['align'] = in_array($video['align'], array(
          'left',
          'right',
          'center',
        )) ? $video['align'] : NULL;
        $replacement = $video['codec']['callback']($video);

        // Invalid format
      }
      else {
        $replacement = '<!-- VIDEO FILTER - INVALID CODEC IN: ' . $code . ' -->';
      }
      $text = str_replace($code, $replacement, $text);
    }
  }
  return $text;
}