diff --git a/includes/media.filter.inc b/includes/media.filter.inc index b8f8065..6593c06 100644 --- a/includes/media.filter.inc +++ b/includes/media.filter.inc @@ -176,6 +176,7 @@ function media_token_to_markup($match, $wysiwyg = FALSE) { $element['#attributes']['data-file_info'] = urlencode($data); $element['#attributes']['class'][] = 'media-element'; } + // Markup is being rendered for the page else { // Display the field elements. $element = array(); @@ -191,20 +192,16 @@ function media_token_to_markup($match, $wysiwyg = FALSE) { field_attach_prepare_view('file', array($file->fid => $file), $tag_info['view_mode']); entity_prepare_view('file', array($file->fid => $file)); $element['content'] += field_attach_view('file', $file, $tag_info['view_mode']); - if (count(element_children($element['content'])) > 1) { - // Add surrounding divs to group them together. - // We dont want divs when there are no additional fields to allow files - // to display inline with text, without breaking p tags. - $attributes = array( - 'class' => array( - 'media', - 'media-element-container', - 'media-' . $element['content']['file']['#view_mode'], - ), - ); - $element['content']['#prefix'] = '
'; - $element['content']['#suffix'] = '
'; - } + $attributes = array( + 'class' => array( + 'media', + 'media-element-container', + 'media-' . $element['content']['file']['#view_mode'], + ), + ); + $element['content']['#prefix'] = '
'; + $element['content']['#suffix'] = '
'; + $element['content']['file']['#theme'] = 'media_wysiwyg_image'; } drupal_alter('media_token_to_markup', $element, $tag_info, $settings); return drupal_render($element); diff --git a/includes/media.theme.inc b/includes/media.theme.inc index b5c82dd..448a6ec 100644 --- a/includes/media.theme.inc +++ b/includes/media.theme.inc @@ -134,3 +134,57 @@ function theme_media_formatter_large_icon($variables) { } return theme('image', $variables); } + + +/** + * Implements a custom version of theme_image_style + * + * This code is largely cribbed from the image module. It generates a new image + * based on parameters passed by wysiwyg. + * + * @TODO sanity checking + * @TODO garbage collection of resized files + * + * @param type $variables + * @return type + */ +function theme_media_wysiwyg_image($variables) { + module_load_include('inc', 'image', 'image.effects'); + + $style = image_style_load('media_wysiwyg'); + $file = $variables['element']['#file']; + // Define the style with dimensions to ensure nothing gets deleted + $derivative_uri = image_style_path($style['name'] . '.' . $file->override['attributes']['width'] . 'x' . $file->override['attributes']['height'], $file->uri); + + $dimensions = array( + 'width' => $file->override['attributes']['width'], + 'height' => $file->override['attributes']['height'], + ); + + // Force the dimensions that are going to be used based on what the wysiwyg passed + $style['effects'][0]['data'] = $dimensions; + + // Don't start generating the image if the derivative already exists or if + // generation is in progress in another thread. + $lock_name = 'image_style_deliver:' . $style['name'] . ':' . drupal_hash_base64($file->uri); + + $lock_acquired = lock_acquire($lock_name); + if (!$lock_acquired) { + return; + } + + if (! file_exists($derivative_uri)) { + // Try to generate the image + image_style_create_derivative($style, $file->uri, $derivative_uri); + } + + if (!empty($lock_acquired)) { + lock_release($lock_name); + } + + $image = array( + 'path' => $derivative_uri, + 'attributes' => $variables['element']['#file']->override['attributes'], + ); + return theme('image', $image); +} \ No newline at end of file diff --git a/media.module b/media.module index 12d6996..accda95 100644 --- a/media.module +++ b/media.module @@ -294,6 +294,12 @@ function media_theme() { 'variables' => array('file' => NULL, 'attributes' => array()), 'file' => 'includes/media.theme.inc', ), + + // Rendered image from a wysiwyg + 'media_wysiwyg_image' => array( + 'render element' => 'element', + 'file' => 'includes/media.theme.inc', + ), ); } @@ -311,6 +317,16 @@ function media_image_default_styles() { ), ) ); + $styles['media_wysiwyg'] = array( + 'effects' => array( + array( + 'name' => 'image_scale_and_crop', + 'data' => array('width' => 320, 'height' => 240), + 'weight' => 0, + ), + ) + ); + return $styles; }