You are here

function video_filter_process in Video Filter 5

Same name and namespace in other branches
  1. 5.2 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

File

./video_filter.module, line 79

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),
      );

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

      // Find codec
      foreach ($codecs as $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;
            break 2;
          }
        }
      }

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

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

        // 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];
        }

        // Resize within set width and height to given ratio
        if ($video['codec']['ratio']) {
          if ($video['width'] * $video['codec']['ratio'] > $video['height']) {
            $video['width'] = round($video['height'] * $video['codec']['ratio']);
            $video['height'] = round($video['width'] / $video['codec']['ratio']);
          }
          else {
            $video['height'] = round($video['width'] / $video['codec']['ratio']);
            $video['width'] = round($video['height'] * $video['codec']['ratio']);
          }
        }
        $video['autoplay'] = (bool) $video['autoplay'];
        $video['align'] = in_array($video['align'], array(
          'left',
          'right',
        )) ? $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;
}