You are here

function video_filter_process in Video Filter 6.2

Same name and namespace in other branches
  1. 5.2 video_filter.module \video_filter_process()
  2. 5 video_filter.module \video_filter_process()
  3. 6.3 video_filter.module \video_filter_process()
  4. 6 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 63

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],
        'autoplay' => variable_get('video_filter_autoplay_' . $format, 0),
        'related' => variable_get('video_filter_related_' . $format, 1),
      );

      // 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 (isset($video['codec'])) {

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

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

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

        // Sets video width & height after any user input has been parsed.
        // First, check if user has set a width.
        if (isset($video['width']) && !isset($video['height'])) {
          $video['height'] = variable_get('video_filter_height_' . $format, 400);
        }
        elseif (isset($video['height']) && !isset($video['width'])) {
          $video['width'] = $video['height'] * $ratio;
        }
        elseif (isset($video['height']) && isset($video['width'])) {
          $video['width'] = $video['width'];
          $video['height'] = $video['height'];
        }
        elseif (!isset($video['height']) && !isset($video['width'])) {
          $video['width'] = variable_get('video_filter_width_' . $format, 400);
          $video['height'] = variable_get('video_filter_height_' . $format, 400);
        }

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

          // respect control_bar_height option if present
          $control_bar_height = $video['control_bar_height'];
        }
        elseif (isset($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'] = isset($video['align']) && in_array($video['align'], array(
          'left',
          'right',
          'center',
        )) ? $video['align'] : NULL;
        if (is_callable($video['codec']['callback'], FALSE)) {
          $replacement = call_user_func($video['codec']['callback'], $video);
        }
        else {

          // Invalid callback
          $replacement = '<!-- VIDEO FILTER - INVALID CALLBACK IN: ' . $pattern . ' -->';
        }

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