You are here

function _epsacrop_crop in EPSA Crop - Image Cropping 6

Same name and namespace in other branches
  1. 6.2 epsacrop.module \_epsacrop_crop()

Copy of imagecache function

2 calls to _epsacrop_crop()
epsacrop_crop in ./epsacrop.module
callback for handling public files imagecache requests.
epsacrop_crop_private in ./epsacrop.module
callback for handling private files imagecache requests

File

./epsacrop.module, line 467
The main file of module

Code

function _epsacrop_crop($presetname, $path) {
  if (!($preset = imagecache_preset_by_name($presetname))) {

    // Send a 404 if we don't know of a preset.
    header("HTTP/1.0 404 Not Found");
    exit;
  }

  // umm yeah deliver it early if it is there. especially useful
  // to prevent lock files from being created when delivering private files.
  $dst = imagecache_create_path($preset['presetname'], $path);
  if (is_file($dst)) {
    imagecache_transfer($dst);
  }

  // preserve path for watchdog.
  $src = $path;

  // Check if the path to the file exists.
  if (!is_file($src) && !is_file($src = file_create_path($src))) {
    watchdog('imagecache', '404: Unable to find %image ', array(
      '%image' => $src,
    ), WATCHDOG_ERROR);
    header("HTTP/1.0 404 Not Found");
    exit;
  }

  // Bail if the requested file isn't an image you can't request .php files
  // etc...
  if (!getimagesize($src)) {
    watchdog('imagecache', '403: File is not an image %image ', array(
      '%image' => $src,
    ), WATCHDOG_ERROR);
    header('HTTP/1.0 403 Forbidden');
    exit;
  }
  $lockfile = file_directory_temp() . '/' . $preset['presetname'] . basename($src);
  if (file_exists($lockfile)) {
    watchdog('imagecache', 'ImageCache already generating: %dst, Lock file: %tmp.', array(
      '%dst' => $dst,
      '%tmp' => $lockfile,
    ), WATCHDOG_NOTICE);

    // 307 Temporary Redirect, to myself. Lets hope the image is done next time around.
    header('Location: ' . request_uri(), TRUE, 307);
    exit;
  }
  touch($lockfile);

  // register the shtdown function to clean up lock files. by the time shutdown
  // functions are being called the cwd has changed from document root, to
  // server root so absolute paths must be used for files in shutdown functions.
  register_shutdown_function('file_delete', realpath($lockfile));

  // Apply our actions for the crop
  $data = _epsacrop_get_crop_action($preset);
  $_preset = 'epsacrop-' . $presetname;
  $fid = _epsacrop_get_fid_from_path($src);
  $coords = _epsacrop_get_coords_from_fid($fid);
  if (!empty($coords[$_preset])) {
    $saved_actions = $preset['actions'];
    $epsacrop_crop = array(
      'actionid' => -1,
      'presetid' => -1,
      'weight' => -99,
      'module' => 'imagecache',
      'action' => 'imagecache_crop',
      'data' => array(
        'width' => $coords[$_preset]['w'],
        'height' => $coords[$_preset]['h'],
        'xoffset' => $coords[$_preset]['x'],
        'yoffset' => $coords[$_preset]['y'],
      ),
    );
    $epsacrop_resize = array(
      'actionid' => -1,
      'presetid' => -1,
      'weight' => -98,
      'module' => 'imagecache',
      'action' => 'imagecache_resize',
      'data' => array(
        'width' => $data['width'],
        'height' => $data['height'],
      ),
    );

    // Remove crop action on actual actions
    foreach ($saved_actions as $key => $action) {
      if (preg_match('/_crop$/', $action['action'])) {
        unset($saved_actions[$key]);
      }
    }
    $preset['actions'] = array_merge(array(
      $epsacrop_crop,
      $epsacrop_resize,
    ), $saved_actions);
  }

  // check if deriv exists... (file was created between apaches request handler and reaching this code)
  // otherwise try to create the derivative.
  if (file_exists($dst) || imagecache_build_derivative($preset['actions'], $src, $dst)) {
    imagecache_transfer($dst);
  }

  // Generate an error if image could not generate.
  watchdog('imagecache', 'Failed generating an image from %image using imagecache preset %preset.', array(
    '%image' => $path,
    '%preset' => $preset['presetname'],
  ), WATCHDOG_ERROR);
  header("HTTP/1.0 500 Internal Server Error");
  exit;
}