You are here

function canvasactions_canvas2file_image in ImageCache Actions 6

Same name and namespace in other branches
  1. 5.3 canvasactions.inc \canvasactions_canvas2file_image()
  2. 5.2 canvasactions.inc \canvasactions_canvas2file_image()
  3. 6.2 canvasactions/canvasactions.inc \canvasactions_canvas2file_image()

Place the source image on the current background

Implementation of hook_image()

Note - this is currently incompatable with imagemagick, due to the way it addresses $image->resource directly - a gd only thing.

Parameters

$image:

$action:

File

./canvasactions.inc, line 395
Helper functions for the text2canvas action for imagecache

Code

function canvasactions_canvas2file_image(&$image, $action = array()) {

  // Search for full (siteroot) paths, then file dir paths, then relative to the current theme
  $filepath = $action['path'];
  if (!file_exists($filepath)) {
    $filepath = file_create_path($action['path']);
  }
  if (!file_exists($filepath)) {
    trigger_error("Failed to load underlay image {$filepath}.", E_USER_ERROR);
    return FALSE;
  }
  $underlay = imageapi_image_open($filepath);

  // To handle odd sizes, we will resize/crop the background image to the desired dimensions before
  // starting the merge. The built-in imagecopymerge, and the watermark library both do not
  // allow overlays to be bigger than the target.
  // Adjust size
  $crop_rules = array(
    'xoffset' => 0,
    'yoffset' => 0,
  );
  if (empty($action['dimensions'])) {
    $action['dimensions'] = 'original';
  }
  switch ($action['dimensions']) {
    case 'original':

      // If the underlay is smaller than the target size,
      // then when preparing the underlay by cropping it,
      // the offsets may need to be negative
      // which will produce a 'cropped' image larger than the original
      // In this case, we need to calculate the position of the bg image
      // in relation to the space it will occupy under the top layer

      #$crop_rules['xoffset'] = $underlay->info['width'] - $image->info['width'] ;
      $crop_rules['width'] = $image->info['width'];
      $crop_rules['height'] = $image->info['height'];
      break;
    case 'background':
      $crop_rules['width'] = $underlay->info['width'];
      $crop_rules['height'] = $underlay->info['height'];
      break;
    case 'minimum':
      $crop_rules['width'] = min($underlay->info['width'], $image->info['width']);
      $crop_rules['height'] = min($underlay->info['height'], $image->info['height']);
      break;
    case 'maximum':
      $crop_rules['width'] = max($underlay->info['width'], $image->info['width']);
      $crop_rules['height'] = max($underlay->info['height'], $image->info['height']);
      break;
  }

  // imageapi crop assumes upsize is legal.
  imagecache_include_standard_actions();

  // ensure the library is loaded.
  // Crop both before processing to avoid unwanted processing.
  imagecache_crop_image($underlay, $crop_rules);

  # BUG - this doesn't position either

  // Actually this fails because imagecache_crop fills it with solid color when 'cropping' to a larger size.

  #imagecache_crop_image($image, $crop_rules);

  #dpm(get_defined_vars());

  // This func modifies the underlay image by ref, placing the current canvas on it
  if (imageapi_image_overlay($underlay, $image, $action['xpos'], $action['ypos'], $action['alpha'], TRUE)) {
    $image->resource = $underlay->resource;
    return TRUE;
  }
}