You are here

function canvasactions_canvas2file_effect in ImageCache Actions 8

Same name and namespace in other branches
  1. 7 canvasactions/canvasactions.inc \canvasactions_canvas2file_effect()

Image effect callback for the underlay (background) effect.

Parameters

stdClass $image:

array $data:

Return value

boolean true on success, false otherwise.

1 string reference to 'canvasactions_canvas2file_effect'
imagecache_canvasactions_image_effect_info in canvasactions/imagecache_canvasactions.module
Implements hook_image_effect_info().

File

canvasactions/canvasactions.inc, line 544

Code

function canvasactions_canvas2file_effect(stdClass $image, array $data) {
  $underlay = imagecache_actions_image_load($data['path'], $image->toolkit);
  if ($underlay) {

    // 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($data['dimensions'])) {
      $data['dimensions'] = 'original';
    }
    switch ($data['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.
    // Crop both before processing to avoid unwanted processing.
    image_crop_effect($underlay, $crop_rules);

    // @todo: 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 (image_overlay($image, $underlay, $data['xpos'], $data['ypos'], $data['alpha'], TRUE)) {

      //$image->resource = $underlay->resource;
      $image = $underlay;

      //@todo: this is a no-op.
      return TRUE;
    }
  }
  return FALSE;
}