You are here

function imagecache_reflect_image in ImageCache Reflect 7

Implements hook_image().

Creates the reflection-like effect.

1 string reference to 'imagecache_reflect_image'
imagecache_reflect_image_effect_info in ./imagecache_reflect.module
Implements hook_image_effect_info().

File

./imagecache_reflect.module, line 145
Adds a reflection action for images.

Code

function imagecache_reflect_image(&$image, $data) {
  $data = array_merge(array(
    'imagecache_reflect_color' => 'white',
    'imagecache_reflect_position' => 'bottom',
    'imagecache_reflect_transparent_source' => FALSE,
    'imagecache_reflect_size' => '50%',
  ), $data);
  $is_vertical = in_array($data['imagecache_reflect_position'], array(
    'top',
    'bottom',
  ));
  if (preg_match('/^\\d{1,3}%$/', $data['imagecache_reflect_size'])) {
    $image_size = $is_vertical ? $image->info['height'] : $image->info['width'];
    $data['imagecache_reflect_size'] = $image_size * (floatval($data['imagecache_reflect_size']) / 100);
  }
  else {
    $data['imagecache_reflect_size'] = intval($data['imagecache_reflect_size']);
  }
  if (!empty($data['imagecache_reflect_color'])) {
    $data['imagecache_reflect_color'] = _imagecache_reflect_color($data['imagecache_reflect_color']);
  }
  $width = $image->info['width'] + ($is_vertical ? 0 : $data['imagecache_reflect_size']);
  $height = $image->info['height'] + ($is_vertical ? $data['imagecache_reflect_size'] : 0);
  $x = $data['imagecache_reflect_position'] == 'left' ? $data['imagecache_reflect_size'] : 0;
  $y = $data['imagecache_reflect_position'] == 'top' ? $data['imagecache_reflect_size'] : 0;
  $background = imagecreatetruecolor($width, $height);

  // If $data['imagecache_reflect_color'] is empty,
  // we're trying for a transparent canvas:
  if (empty($data['imagecache_reflect_color'])) {
    imagesavealpha($background, TRUE);
    imagealphablending($background, FALSE);
  }
  else {
    imagefill($background, 0, 0, $data['imagecache_reflect_color']);
  }
  imagecopy($background, $image->resource, $x, $y, 0, 0, $image->info['width'], $image->info['height']);

  // If the user has said that the source image might contain
  // transparency, use the slower, but working algorithm to merge
  // the images:
  if ($data['imagecache_reflect_transparent_source']) {
    $min_alpha = _imagecache_reflect_alpha_get_min($image->resource);
  }
  else {
    $min_alpha = 0;
  }
  $return = FALSE;
  switch ($data['imagecache_reflect_position']) {
    case 'top':
      $steps = min($data['imagecache_reflect_size'], $image->info['height']);
      for ($i = 0, $opacity = 50; $i < $steps; ++$i, $opacity = ceil(($steps - $i) / $steps * 100 / 2)) {
        _imagecache_reflect_imagecopymerge_alpha($background, $image->resource, 0, $y - $i, 0, $i, $image->info['width'], 1, $opacity, $min_alpha);
      }
      $return = TRUE;
      break;
    case 'bottom':
      $steps = min($data['imagecache_reflect_size'], $image->info['height']);
      for ($i = 0, $opacity = 50; $i < $steps; ++$i, $opacity = ceil(($steps - $i) / $steps * 100 / 2)) {
        _imagecache_reflect_imagecopymerge_alpha($background, $image->resource, 0, $image->info['height'] + $i, 0, $image->info['height'] - $i - 1, $image->info['width'], 1, $opacity, $min_alpha);
      }
      $return = TRUE;
      break;
    case 'left':
      $steps = min($data['imagecache_reflect_size'], $image->info['width']);
      for ($i = 0, $opacity = 50; $i < $steps; ++$i, $opacity = ceil(($steps - $i) / $steps * 100 / 2)) {
        _imagecache_reflect_imagecopymerge_alpha($background, $image->resource, $x - $i, 0, $i, 0, 1, $image->info['height'], $opacity, $min_alpha);
      }
      $return = TRUE;
      break;
    case 'right':
      $steps = min($data['imagecache_reflect_size'], $image->info['height']);
      for ($i = 0, $opacity = 50; $i < $steps; ++$i, $opacity = ceil(($steps - $i) / $steps * 100 / 2)) {
        _imagecache_reflect_imagecopymerge_alpha($background, $image->resource, $image->info['width'] + $i, 0, $image->info['width'] - $i - 1, 0, 1, $image->info['height'], $opacity, $min_alpha);
      }
      $return = TRUE;
      break;
  }
  if ($return) {
    imagedestroy($image->resource);
    $image->resource = $background;
    $image->info['width'] = $width;
    $image->info['height'] = $height;
  }
  else {
    imagedestroy($background);
  }
  return $return;
}