You are here

function image_upload_element_thumb in Upload element 6

Generates a temp image for image preview.

This uses core Drupal image processing.

1 string reference to 'image_upload_element_thumb'
upload_element_menu in ./upload_element.module
Implementation of hook_menu().

File

./upload_element.pages.inc, line 13
Handles image previews from both temperary and perminant file directories.

Code

function image_upload_element_thumb() {
  $GLOBALS['devel_shutdown'] = FALSE;
  list($form_build_id, $name, $fid) = func_get_args();

  // Load the element and pull out the defaults.
  $form_state = array(
    'submitted' => FALSE,
  );
  if ($form = form_get_cache($form_build_id, $form_state)) {
    $form += array(
      '#post' => array(),
      '#programmed' => TRUE,
    );

    // calling form_builder would trigger the session to be cleared
    $element = locate_upload_element($form, $name);
    if ($element && ($image = image_upload_element_preview_path($form_build_id, $name, $element['#image_preview_default_image']))) {
      $info = image_get_info($image->filepath);
      if ($info) {

        // create temp file, minimise conflicts by instant touch
        $tmp_file = file_destination(file_directory_temp() . '/upload_element_' . $fid . '.' . $info['extension'], FILE_EXISTS_RENAME);
        $width = 100;
        $height = 100;
        if ($element['#image_preview_size'] && preg_match('/^\\d+x\\d+$/', $element['#image_preview_size'])) {
          list($width, $height) = explode('x', $element['#image_preview_size'], 2);
        }
        if (!image_scale($image->filepath, $tmp_file, $width, $height)) {
          $tmp_file = $image->filepath;
        }
        $tmp_info = image_get_info($tmp_file);
        if ($tmp_info) {
          $headers = array(
            'Content-type: ' . $tmp_info['mime_type'],
            'Content-Length: ' . $tmp_info['file_size'],
            'Cache-Control: max-age=0, no-cache',
            'Expires: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT',
            'Last-Modified: ' . time(),
          );
          ob_end_clean();
          foreach ($headers as $header) {
            $header = preg_replace('/\\r?\\n(?!\\t| )/', '', $header);
            drupal_set_header($header);
          }

          // Transfer file in 1024 byte chunks to save memory usage.
          if ($fd = fopen($tmp_file, 'rb')) {
            while (!feof($fd)) {
              print fread($fd, 1024);
            }
            fclose($fd);
          }
          else {
            watchdog('Upload element', 'Error generating the image thumbnail.', array(), WATCHDOG_ERROR);
          }
        }
        else {
          watchdog('Upload element', 'Error generating the image thumbnail.', array(), WATCHDOG_ERROR);
        }

        // We're using the original if it is too small - do not delete!
        if ($image->filepath != $tmp_file) {
          @unlink($tmp_file);
        }
      }
      else {
        watchdog('Upload element', 'No image information could be parsed from the upload.', array(), WATCHDOG_ERROR);
      }
    }
    else {
      watchdog('Upload element', 'No preview path was created.', array(), WATCHDOG_ERROR);
    }
  }
  else {
    watchdog('Upload element', 'Form element was not found. This form may not be compatible with this module.', array(), WATCHDOG_ERROR);
  }
  if (!headers_sent()) {
    drupal_set_header('HTTP/1.1 404 Not Found');
  }
  exit;
}