You are here

class Opacity in Image Effects 8

Same name in this branch
  1. 8 src/Plugin/ImageToolkit/Operation/gd/Opacity.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\Opacity
  2. 8 src/Plugin/ImageToolkit/Operation/imagemagick/Opacity.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\Opacity
Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageToolkit/Operation/imagemagick/Opacity.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\Opacity
  2. 8.2 src/Plugin/ImageToolkit/Operation/imagemagick/Opacity.php \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\Opacity

Defines ImageMagick Opacity operation.

Plugin annotation


@ImageToolkitOperation(
  id = "image_effects_imagemagick_opacity",
  toolkit = "imagemagick",
  operation = "opacity",
  label = @Translation("Opacity"),
  description = @Translation("Adjust image transparency.")
)

Hierarchy

  • class \Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick\Opacity extends \Drupal\imagemagick\Plugin\ImageToolkit\Operation\imagemagick\ImagemagickImageToolkitOperationBase uses OpacityTrait

Expanded class hierarchy of Opacity

4 string references to 'Opacity'
BackgroundImageEffect::buildConfigurationForm in src/Plugin/ImageEffect/BackgroundImageEffect.php
Form constructor.
ImageEffectsColor::processImageEffectsColor in src/Element/ImageEffectsColor.php
Processes a 'image_effects_color' form element.
OpacityImageEffect::buildConfigurationForm in src/Plugin/ImageEffect/OpacityImageEffect.php
Form constructor.
WatermarkImageEffect::buildConfigurationForm in src/Plugin/ImageEffect/WatermarkImageEffect.php
Form constructor.

File

src/Plugin/ImageToolkit/Operation/imagemagick/Opacity.php, line 19

Namespace

Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick
View source
class Opacity extends ImagemagickImageToolkitOperationBase {
  use OpacityTrait;

  /**
   * {@inheritdoc}
   */
  protected function execute(array $arguments) {
    if ($this
      ->getToolkit()
      ->getPackage() === 'graphicsmagick') {

      // GraphicsMagick does not support -alpha argument, return early.
      // @todo implement a GraphicsMagick solution if possible.
      return FALSE;
    }
    switch ($arguments['opacity']) {
      case 100:

        // Fully opaque, leave image as-is.
        break;
      case 0:

        // Fully transparent, set full transparent for all pixels.
        $this
          ->getToolkit()
          ->addArgument("-alpha set -channel Alpha -evaluate Set 0%");
        break;
      default:

        // Divide existing alpha to the opacity needed. This preserves
        // partially transparent images.
        $divide = number_format((double) (100 / $arguments['opacity']), 4, '.', ',');
        $this
          ->getToolkit()
          ->addArgument("-alpha set -channel Alpha -evaluate Divide {$divide}");
        break;
    }
    return TRUE;
  }

}

Members