You are here

function image_imagemagick_perspective in ImageCache Actions 7

Imagemagick toolkit specific implementation of the image Perspective effect.

Parameters

stdClass $image: Image object containing the image to operate on.

array $data: The current configuration for this image effect, contains the keys distortion, vanish, symmetry and opposite_distortion options.

Return value

bool True on success, false otherwise.

File

canvasactions/canvasactions.inc, line 1599

Code

function image_imagemagick_perspective(stdClass $image, $data) {
  $width = $image->info['width'];
  $height = $image->info['height'];
  $distortion = $data['distortion'];
  $opposite_distortion = $data['symmetry'] === 'symmetrical' ? $distortion : $data['opposite_distortion'];
  switch ($data['vanish']) {
    case 'top':
      $left = round($width * $distortion / 100);
      $right = round($width * (100 - $opposite_distortion) / 100);
      $perspective_arg = "0,0,{$left},0 0,{$height},0,{$height} {$width},0,{$right},0 {$width},{$height},{$width},{$height}";
      break;
    case 'right':
    default:

      // Prevents warning about possibly undefined variable.
      $top = round($height * $distortion / 100);
      $bottom = round($height * (100 - $opposite_distortion) / 100);
      $perspective_arg = "0,0,0,0 0,{$height},0,{$height} {$width},0,{$width},{$top} {$width},{$height},{$width},{$bottom}";
      break;
    case 'bottom':
      $left = round($width * $distortion / 100);
      $right = round($width * (100 - $opposite_distortion) / 100);
      $perspective_arg = "0,0,0,0 0,{$height},{$left},{$height} {$width},0,{$width},0 {$width},{$height},{$right},{$height}";
      break;
    case 'left':
      $top = round($height * $distortion / 100);
      $bottom = round($height * (100 - $opposite_distortion) / 100);
      $perspective_arg = "0,0,0,{$top} 0,{$height},0,{$bottom} {$width},0,{$width},0 {$width},{$height},{$width},{$height}";
      break;
  }
  $transparent_white = escapeshellarg('#ffffffff');
  $perspective = escapeshellarg($perspective_arg);
  $image->ops[] = "-background {$transparent_white} -virtual-pixel background -distort Perspective {$perspective}";
  return TRUE;
}