You are here

function textimage_url_deliver in Textimage 7.3

Deliver directly a Textimage from the URL request.

1 string reference to 'textimage_url_deliver'
textimage_menu in ./textimage.module
Implements hook_menu().

File

./textimage.module, line 913
Textimage - Provides text to image manipulations.

Code

function textimage_url_deliver() {

  // Get the real directory of the public wrapper.
  $public_path = file_stream_wrapper_get_instance_by_scheme('public')
    ->getDirectoryPath();

  // Full path to public textimage files.
  $public_textimage_path = base_path() . $public_path . '/textimage/';
  $pattern = '/' . str_replace('/', '\\/', $public_textimage_path) . '/';

  // The request in format [style]/[Text_0]/[Text_1]/.../[Text_n].[extension]
  // is in the trailing part of the URL.
  $trail = urldecode(preg_replace($pattern, '', request_uri()));
  $args = explode('/', $trail);

  // Manages the [style].
  $style_name = array_shift($args);
  $style = TextimageStyles::get($style_name);
  if (!$style) {
    drupal_not_found();
  }
  if ($style['textimage']['uri_scheme'] != 'public') {
    drupal_access_denied();
  }

  // [Text_0]/[Text_1]/.../[Text_n] to the $text array.
  $text = $args;

  // Manage the [extension].
  $last_text = array_pop($text);
  $offset = strrpos($last_text, '.');
  if ($offset && strlen($last_text) - $offset <= 5) {
    $extension = substr($last_text, $offset + 1);
    $text[] = substr($last_text, 0, $offset);
  }
  else {
    $extension = 'png';
    $text[] = $last_text;
  }

  // Get the image and transfer to client.
  $uri = TextimageImager::processImageRequest($style, NULL, $text, $extension);
  $image_info = image_get_info($uri);
  file_transfer($uri, array(
    'Content-Type' => $image_info['mime_type'],
    'Content-Length' => $image_info['file_size'],
  ));
}