This repository has been archived by the owner on Jan 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #2748507 by mondrake: Add "Opacity" effect
- Loading branch information
mondrake
authored and
mondrake
committed
Jan 30, 2017
1 parent
ece7a2f
commit 7e79819
Showing
10 changed files
with
363 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Drupal\image_effects\Plugin\ImageEffect; | ||
|
||
use Drupal\Core\Image\ImageInterface; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\image\ConfigurableImageEffectBase; | ||
|
||
/** | ||
* Adjust image transparency. | ||
* | ||
* @ImageEffect( | ||
* id = "image_effects_opacity", | ||
* label = @Translation("Opacity"), | ||
* description = @Translation("Change overall image transparency level. Applies only to image formats that support Alpha channel, like PNG.") | ||
* ) | ||
*/ | ||
class OpacityImageEffect extends ConfigurableImageEffectBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultConfiguration() { | ||
return [ | ||
'opacity' => 50, | ||
] + parent::defaultConfiguration(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSummary() { | ||
return [ | ||
'#theme' => 'image_effects_opacity_summary', | ||
'#data' => $this->configuration, | ||
] + parent::getSummary(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | ||
$form['opacity'] = [ | ||
'#type' => 'number', | ||
'#title' => $this->t('Opacity'), | ||
'#field_suffix' => '%', | ||
'#description' => $this->t('Opacity: 0 - 100'), | ||
'#default_value' => $this->configuration['opacity'], | ||
'#min' => 0, | ||
'#max' => 100, | ||
'#maxlength' => 3, | ||
'#size' => 3 | ||
]; | ||
return $form; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | ||
parent::submitConfigurationForm($form, $form_state); | ||
$this->configuration['opacity'] = $form_state->getValue('opacity'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function applyEffect(ImageInterface $image) { | ||
return $image->apply('opacity', ['opacity' => $this->configuration['opacity']]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Drupal\image_effects\Plugin\ImageToolkit\Operation; | ||
|
||
/** | ||
* Base trait for image_effects Opacity operations. | ||
*/ | ||
trait OpacityTrait { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function arguments() { | ||
return [ | ||
'opacity' => [ | ||
'description' => 'Opacity.', | ||
'required' => FALSE, | ||
'default' => 100, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function validateArguments(array $arguments) { | ||
// Ensure opacity is in the range 0-100. | ||
if (!is_numeric($arguments['opacity']) || $arguments['opacity'] > 100 || $arguments['opacity'] < 0) { | ||
throw new \InvalidArgumentException("Invalid opacity ('{$arguments['opacity']}') specified for the image 'opacity' operation"); | ||
} | ||
return $arguments; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Drupal\image_effects\Plugin\ImageToolkit\Operation\gd; | ||
|
||
use Drupal\system\Plugin\ImageToolkit\Operation\gd\GDImageToolkitOperationBase; | ||
use Drupal\image_effects\Plugin\ImageToolkit\Operation\gd\GDOperationTrait; | ||
use Drupal\image_effects\Plugin\ImageToolkit\Operation\OpacityTrait; | ||
|
||
/** | ||
* Defines GD Opacity operation. | ||
* | ||
* @ImageToolkitOperation( | ||
* id = "image_effects_gd_opacity", | ||
* toolkit = "gd", | ||
* operation = "opacity", | ||
* label = @Translation("Opacity"), | ||
* description = @Translation("Adjust image transparency.") | ||
* ) | ||
*/ | ||
class Opacity extends GDImageToolkitOperationBase { | ||
|
||
use GDOperationTrait; | ||
use OpacityTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(array $arguments) { | ||
if ($arguments['opacity'] < 100) { | ||
return $this->filterOpacity($this->getToolkit()->getResource(), $arguments['opacity']); | ||
} | ||
return TRUE; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Drupal\image_effects\Plugin\ImageToolkit\Operation\imagemagick; | ||
|
||
use Drupal\imagemagick\Plugin\ImageToolkit\Operation\imagemagick\ImagemagickImageToolkitOperationBase; | ||
use Drupal\image_effects\Plugin\ImageToolkit\Operation\OpacityTrait; | ||
|
||
/** | ||
* Defines ImageMagick Opacity operation. | ||
* | ||
* @ImageToolkitOperation( | ||
* id = "image_effects_imagemagick_opacity", | ||
* toolkit = "imagemagick", | ||
* operation = "opacity", | ||
* label = @Translation("Opacity"), | ||
* description = @Translation("Adjust image transparency.") | ||
* ) | ||
*/ | ||
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((float) (100 / $arguments['opacity']), 4, '.', ','); | ||
$this->getToolkit()->addArgument("-alpha set -channel Alpha -evaluate Divide {$divide}"); | ||
break; | ||
|
||
} | ||
|
||
return TRUE; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Drupal\image_effects\Tests; | ||
|
||
/** | ||
* Opacity effect test. | ||
* | ||
* @group Image Effects | ||
*/ | ||
class ImageEffectsOpacityTest extends ImageEffectsTestBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp() { | ||
parent::setUp(); | ||
// @todo This effect does not work on GraphicsMagick. | ||
$this->imagemagickPackages['graphicsmagick'] = FALSE; | ||
} | ||
|
||
/** | ||
* Opacity effect test. | ||
*/ | ||
public function testOpacityEffect() { | ||
// Test operations on toolkits. | ||
$this->executeTestOnToolkits([$this, 'doTestOpacityOperations']); | ||
} | ||
|
||
/** | ||
* Opacity operations test. | ||
*/ | ||
public function doTestOpacityOperations() { | ||
// Test on the PNG test image. | ||
$original_uri = $this->getTestImageCopyUri('/files/image-test.png', 'simpletest'); | ||
|
||
// Test data. | ||
$test_data = [ | ||
// No transparency change. | ||
'100' => [$this->red, $this->green, $this->transparent, $this->blue], | ||
// 50% transparency. | ||
'50' => [[255, 0, 0, 63], [0, 255, 0, 63], $this->transparent, [0, 0, 255, 63]], | ||
// 100% transparency. | ||
'0' => [$this->transparent, $this->transparent, $this->transparent, $this->transparent], | ||
]; | ||
|
||
foreach ($test_data as $opacity => $colors) { | ||
// Add Opacity effect to the test image style. | ||
$effect = [ | ||
'id' => 'image_effects_opacity', | ||
'data' => [ | ||
'opacity' => $opacity, | ||
], | ||
]; | ||
$uuid = $this->addEffectToTestStyle($effect); | ||
|
||
// Check that ::applyEffect generates image with expected opacity. | ||
$derivative_uri = $this->testImageStyle->buildUri($original_uri); | ||
$this->testImageStyle->createDerivative($original_uri, $derivative_uri); | ||
$image = $this->imageFactory->get($derivative_uri, 'gd'); | ||
$this->assertTrue($this->colorsAreEqual($colors[0], $this->getPixelColor($image, 0, 0))); | ||
$this->assertTrue($this->colorsAreEqual($colors[1], $this->getPixelColor($image, 39, 0))); | ||
$this->assertTrue($this->colorsAreEqual($colors[2], $this->getPixelColor($image, 0, 19))); | ||
$this->assertTrue($this->colorsAreEqual($colors[3], $this->getPixelColor($image, 39, 19))); | ||
|
||
// Remove effect. | ||
$uuid = $this->removeEffectFromTestStyle($uuid); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{# | ||
/** | ||
* @file | ||
* Default theme implementation for a summary of an image opacity effect. | ||
* | ||
* Available variables: | ||
* - data: The current configuration for this opacity effect, including: | ||
* - opacity: The opacity of the image. | ||
* - effect: The effect information, including: | ||
* - id: The effect identifier. | ||
* - label: The effect name. | ||
* - description: The effect description. | ||
* | ||
* @ingroup themeable | ||
*/ | ||
#} | ||
{% spaceless %} | ||
{{ data.opacity|e }}% | ||
{% endspaceless %} |