You are here

function canvasactions_roundedcorners_image in ImageCache Actions 5.2

Same name and namespace in other branches
  1. 5.3 canvasactions.inc \canvasactions_roundedcorners_image()
  2. 6.2 canvasactions/rounded_corners.inc \canvasactions_roundedcorners_image()
  3. 6 canvasactions.inc \canvasactions_roundedcorners_image()

Create a rounded corner mask and alpha-merge it with the image.

Implementation of hook_image()

Note, this is not image toolkit-agnostic yet! It just assumes GD. We can abstract it out once we have something else to abstract to. In the meantime just don't.

'independant' rounded corners logic contributed by canaryMason 2009-03

Parameters

$image:

$action:

File

./canvasactions.inc, line 516

Code

function canvasactions_roundedcorners_image(&$image, $action = array()) {
  if ($image->toolkit != 'imageapi_gd') {
    drupal_set_message("Unable to create rounded corners with {$image->toolkit}");

    // Pretend we did anyway, just return the untrimmed version.
    return TRUE;
  }
  $width = $image->info['width'];
  $height = $image->info['height'];
  $radius = $action['radius'];
  $independent_corners = $action['independent_corners_set']['independent_corners'];
  $radii = $action['independent_corners_set']['radii'];
  $diameter = array();
  if ($action['antialias']) {
    $width = $width * 3;
    $height = $height * 3;
    $radius = $radius * 3;
    foreach ($radii as $corner => $corner_radius) {
      $radii[$corner] = $radii[$corner] * 3;
    }
  }
  foreach ($radii as $corner => $corner_radius) {
    if (!$independent_corners) {

      // Use the unique maths for independant corners,
      // even if they are all the same.
      $radii[$corner] = $radius;
    }
    $diameter[$corner] = $radii[$corner] * 2;
  }

  // Create a mask with rounded corners
  $mask = imagecreatetruecolor($width, $height);

  // Is using the toolkit really worth the bother?
  // Just doing it in an attempt to be consistent
  $mask_image = (object) array(
    'res' => &$mask,
    'info' => array(
      'width' => $width,
      'height' => $height,
      'extension' => 'png',
    ),
    'toolkit' => $image->toolkit,
    'resource' => &$mask,
  );

  // Start with a blank slate
  $background = imagecolorallocatealpha($mask, 255, 255, 255, 127);
  imagesavealpha($mask, TRUE);
  imagealphablending($mask, false);
  imagefilledrectangle($mask, 0, 0, $width, $height, $background);

  // Place solid lumps on it
  $foreground = imagecolorallocatealpha($mask, 0, 0, 0, 0);

  // Place blobs in the corners
  ImageFilledEllipse($mask, $radii['tl'], $radii['tl'], $diameter['tl'], $diameter['tl'], $foreground);
  ImageFilledEllipse($mask, $width - $radii['tr'], $radii['tr'], $diameter['tr'], $diameter['tr'], $foreground);
  ImageFilledEllipse($mask, $radii['bl'], $height - $radii['bl'], $diameter['bl'], $diameter['bl'], $foreground);
  ImageFilledEllipse($mask, $width - $radii['br'], $height - $radii['br'], $diameter['br'], $diameter['br'], $foreground);

  // Block out the middle
  ImageFilledRectangle($mask, $radii['tl'], 0, $width * 0.5, $height * 0.5, $foreground);
  ImageFilledRectangle($mask, 0, $radii['tl'], $width * 0.5, $height * 0.5, $foreground);
  ImageFilledRectangle($mask, $width * 0.5, 0, $width - $radii['tr'], $height * 0.5, $foreground);
  ImageFilledRectangle($mask, $width * 0.5, $radii['tr'], $width, $height * 0.5, $foreground);
  ImageFilledRectangle($mask, 0, $height * 0.5, $width * 0.5, $height - $radii['bl'], $foreground);
  ImageFilledRectangle($mask, $radii['bl'], $height * 0.5, $width * 0.5, $height, $foreground);
  ImageFilledRectangle($mask, $width * 0.5, $height * 0.5, $width, $height - $radii['br'], $foreground);
  ImageFilledRectangle($mask, $width * 0.5, $height * 0.5, $width - $radii['br'], $height, $foreground);
  if ($action['antialias']) {

    // Use toolkit so scale down again.
    imageapi_image_scale($mask_image, $width / 3, $height / 3);
    $mask = $mask_image->resource;
  }

  // Now we have a mask. Merge to get a result
  canvasactions_mask($image, $mask_image);
  return TRUE;
}