You are here

function image_effects_text_get_text in ImageCache Actions 7

Same name and namespace in other branches
  1. 8 image_effects_text/image_effects_text.inc \image_effects_text_get_text()

Get the text to use for this image.

Parameters

stdClass $image: The image the current effect is to be applied to.

array $data: An array containing the effect data.

Return value

string Plain text to be placed on the image.

1 call to image_effects_text_get_text()
image_effects_text_effect_inc in image_effects_text/image_effects_text.inc
Image effect callback for the text effect.

File

image_effects_text/image_effects_text.inc, line 683

Code

function image_effects_text_get_text(stdClass $image, array $data) {

  // Get context about the image.
  $image_context = imagecache_actions_get_image_context($image, $data);
  if ($data['text_source'] === 'text') {

    // Replace \n with a newline character, except when preceded by a \.
    $text = preg_replace('/([^\\\\])\\\\n/', "\$1\n", $data['text']);

    // Replace \\n by \n.
    $text = preg_replace('/\\\\\\\\n/', '\\n', $text);

    // Replace tokens.
    $token_data = array();
    foreach ($image_context['referring_entities'] as $field_referring_entities) {
      foreach ($field_referring_entities as $entity_type => $entities) {

        // We can pass only 1 entity per given type to token_replace(), we take
        // the first.
        $token_data[$entity_type] = reset($entities);
      }
    }
    if ($image_context['managed_file']) {
      $token_data['file'] = $image_context['managed_file'];
    }

    // We should not sanitize the text as it will not be rendered in the browser
    // but is rendered on the image canvas on the server.
    $text = token_replace($text, $token_data, array(
      'clear' => TRUE,
      'sanitize' => FALSE,
    ));
  }
  else {
    if ($data['text_source'] === 'alt' || $data['text_source'] === 'title') {
      $text = '';

      // We have 2 possible sources for the alt or title text:
      // - Image field.
      // - Media module (7.x-2.x) with file_entity: the alt and title come as
      // fields of the file entity, stored in 'managed_file'. The names of the
      // fields are field_file_image_alt_text resp. field_file_image_title_text.
      // BTW: these fields are also available in the 'image_field' entry, but as
      // a managed file may be existing without any image field referring to it,
      // we do the lookup in the managed_file entry.
      if (!empty($image_context['image_field'][$data['text_source']])) {
        $text = $image_context['image_field'][$data['text_source']];
      }
      else {
        if (!empty($image_context['managed_file'])) {
          $field = field_get_items('file', $image_context['managed_file'], "field_file_image_{$data['text_source']}_text");
          if ($field) {
            $text = $field[0]['value'];
          }
        }
      }
    }
    else {

      // $data['text_source'] === 'php'
      // Process the php using php_eval (rather than eval), but with GLOBAL
      // variables, so they can be passed successfully.
      $GLOBALS['image_context'] = $image_context;
      $GLOBALS['image'] = $image;

      // Get (non-alterable) context about the image style and image effect.
      $execution_info = imagecache_actions_get_image_effect_context();
      $GLOBALS['image_style'] = $execution_info['image_style'];
      $GLOBALS['image_effect_id'] = $execution_info['image_effect_id'];

      // We don't need to check_plain() the resulting text, as the text is not
      // rendered in a browser but processed on the server.
      $text = module_exists('php') ? php_eval('<' . '?php global $image, $image_context; ' . $data['php'] . ' ?' . '>') : '';
      unset($GLOBALS['image_effect_id']);
      unset($GLOBALS['image_style']);
      unset($GLOBALS['image']);
      unset($GLOBALS['image_context']);
    }
  }

  // Convert case.
  $text = image_effect_text_case_transform($text, isset($data['text_case']) ? $data['text_case'] : 'none');
  return $text;
}