Loading src/Component/ImageUtility.php +34 −0 Original line number Diff line number Diff line Loading @@ -91,4 +91,38 @@ abstract class ImageUtility { return $dimensions; } /** * Returns the offset in pixels from the anchor. * * @param string $anchor * The anchor ('top', 'left', 'bottom', 'right', 'center'). * @param int $current_size * The current size, in pixels. * @param int $new_size * The new size, in pixels. * * @return int * The offset from the anchor, in pixels. * * @throws \InvalidArgumentException * When the $anchor argument is not valid. */ public static function getKeywordOffset(string $anchor, int $current_size, int $new_size): int { switch ($anchor) { case 'bottom': case 'right': return $current_size - $new_size; case 'center': return (int) round($current_size / 2 - $new_size / 2); case 'top': case 'left': return 0; } throw new \InvalidArgumentException("Invalid anchor '{$anchor}' provided to getKeywordOffset()"); } } src/Plugin/ImageEffect/BackgroundImageEffect.php +3 −2 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ use Drupal\Core\Image\ImageInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\image\ConfigurableImageEffectBase; use Drupal\image_effects\Component\ImageUtility; use Drupal\image_effects\Plugin\ImageEffectsPluginBaseInterface; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; Loading Loading @@ -186,8 +187,8 @@ class BackgroundImageEffect extends ConfigurableImageEffectBase implements Conta return FALSE; } list($x, $y) = explode('-', $this->configuration['placement']); $x_pos = image_filter_keyword($x, $background_image->getWidth(), $image->getWidth()); $y_pos = image_filter_keyword($y, $background_image->getHeight(), $image->getHeight()); $x_pos = ImageUtility::getKeywordOffset($x, $background_image->getWidth(), $image->getWidth()); $y_pos = ImageUtility::getKeywordOffset($y, $background_image->getHeight(), $image->getHeight()); return $image->apply('background', [ 'x_offset' => $x_pos + $this->configuration['x_offset'], 'y_offset' => $y_pos + $this->configuration['y_offset'], Loading src/Plugin/ImageEffect/MaskImageEffect.php +2 −2 Original line number Diff line number Diff line Loading @@ -209,8 +209,8 @@ class MaskImageEffect extends ConfigurableImageEffectBase implements ContainerFa // Calculate position of mask on source image based on placement option. list($x, $y) = explode('-', $this->configuration['placement']); $x_pos = round(image_filter_keyword($x, $image->getWidth(), $mask_width)); $y_pos = round(image_filter_keyword($y, $image->getHeight(), $mask_height)); $x_pos = ImageUtility::getKeywordOffset($x, $image->getWidth(), $mask_width); $y_pos = ImageUtility::getKeywordOffset($y, $image->getHeight(), $mask_height); // Calculate offset based on px/percentage. $x_offset = (int) ImageUtility::percentFilter($this->configuration['x_offset'], $image->getWidth()); Loading src/Plugin/ImageEffect/RelativeCropImageEffect.php +3 −10 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\image_effects\Plugin\ImageEffect; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Image\ImageInterface; use Drupal\image\Plugin\ImageEffect\CropImageEffect; use Drupal\image_effects\Component\ImageUtility; /** * Provides an image effect that crops images to a ratio. Loading Loading @@ -37,20 +38,12 @@ class RelativeCropImageEffect extends CropImageEffect { // Pick the right anchor depending on whether the image is being cropped in // width or in height. if ($dimensions['width'] !== $original_dimensions['width']) { $x = image_filter_keyword( $this->configuration['anchor']['width'], $original_dimensions['width'], $dimensions['width'] ); $x = ImageUtility::getKeywordOffset($this->configuration['anchor']['width'], $original_dimensions['width'], $dimensions['width']); $y = 0; } elseif ($dimensions['height'] !== $original_dimensions['height']) { $x = 0; $y = image_filter_keyword( $this->configuration['anchor']['height'], $original_dimensions['height'], $dimensions['height'] ); $y = ImageUtility::getKeywordOffset($this->configuration['anchor']['height'], $original_dimensions['height'], $dimensions['height']); } else { // If the image already has the correct dimensions, do not do anything. Loading src/Plugin/ImageEffect/SetCanvasImageEffect.php +2 −2 Original line number Diff line number Diff line Loading @@ -224,8 +224,8 @@ class SetCanvasImageEffect extends ConfigurableImageEffectBase { // Get offset of original image. if ($this->configuration['canvas_size'] === 'exact') { list($x_pos, $y_pos) = explode('-', $this->configuration['exact']['placement']); $data['x_pos'] = image_filter_keyword($x_pos, $data['width'], $image->getWidth()) + $this->configuration['exact']['x_offset']; $data['y_pos'] = image_filter_keyword($y_pos, $data['height'], $image->getHeight()) + $this->configuration['exact']['y_offset']; $data['x_pos'] = ImageUtility::getKeywordOffset($x_pos, $data['width'], $image->getWidth()) + $this->configuration['exact']['x_offset']; $data['y_pos'] = ImageUtility::getKeywordOffset($y_pos, $data['height'], $image->getHeight()) + $this->configuration['exact']['y_offset']; } else { $data['x_pos'] = $this->configuration['relative']['left']; Loading Loading
src/Component/ImageUtility.php +34 −0 Original line number Diff line number Diff line Loading @@ -91,4 +91,38 @@ abstract class ImageUtility { return $dimensions; } /** * Returns the offset in pixels from the anchor. * * @param string $anchor * The anchor ('top', 'left', 'bottom', 'right', 'center'). * @param int $current_size * The current size, in pixels. * @param int $new_size * The new size, in pixels. * * @return int * The offset from the anchor, in pixels. * * @throws \InvalidArgumentException * When the $anchor argument is not valid. */ public static function getKeywordOffset(string $anchor, int $current_size, int $new_size): int { switch ($anchor) { case 'bottom': case 'right': return $current_size - $new_size; case 'center': return (int) round($current_size / 2 - $new_size / 2); case 'top': case 'left': return 0; } throw new \InvalidArgumentException("Invalid anchor '{$anchor}' provided to getKeywordOffset()"); } }
src/Plugin/ImageEffect/BackgroundImageEffect.php +3 −2 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ use Drupal\Core\Image\ImageInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\image\ConfigurableImageEffectBase; use Drupal\image_effects\Component\ImageUtility; use Drupal\image_effects\Plugin\ImageEffectsPluginBaseInterface; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; Loading Loading @@ -186,8 +187,8 @@ class BackgroundImageEffect extends ConfigurableImageEffectBase implements Conta return FALSE; } list($x, $y) = explode('-', $this->configuration['placement']); $x_pos = image_filter_keyword($x, $background_image->getWidth(), $image->getWidth()); $y_pos = image_filter_keyword($y, $background_image->getHeight(), $image->getHeight()); $x_pos = ImageUtility::getKeywordOffset($x, $background_image->getWidth(), $image->getWidth()); $y_pos = ImageUtility::getKeywordOffset($y, $background_image->getHeight(), $image->getHeight()); return $image->apply('background', [ 'x_offset' => $x_pos + $this->configuration['x_offset'], 'y_offset' => $y_pos + $this->configuration['y_offset'], Loading
src/Plugin/ImageEffect/MaskImageEffect.php +2 −2 Original line number Diff line number Diff line Loading @@ -209,8 +209,8 @@ class MaskImageEffect extends ConfigurableImageEffectBase implements ContainerFa // Calculate position of mask on source image based on placement option. list($x, $y) = explode('-', $this->configuration['placement']); $x_pos = round(image_filter_keyword($x, $image->getWidth(), $mask_width)); $y_pos = round(image_filter_keyword($y, $image->getHeight(), $mask_height)); $x_pos = ImageUtility::getKeywordOffset($x, $image->getWidth(), $mask_width); $y_pos = ImageUtility::getKeywordOffset($y, $image->getHeight(), $mask_height); // Calculate offset based on px/percentage. $x_offset = (int) ImageUtility::percentFilter($this->configuration['x_offset'], $image->getWidth()); Loading
src/Plugin/ImageEffect/RelativeCropImageEffect.php +3 −10 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\image_effects\Plugin\ImageEffect; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Image\ImageInterface; use Drupal\image\Plugin\ImageEffect\CropImageEffect; use Drupal\image_effects\Component\ImageUtility; /** * Provides an image effect that crops images to a ratio. Loading Loading @@ -37,20 +38,12 @@ class RelativeCropImageEffect extends CropImageEffect { // Pick the right anchor depending on whether the image is being cropped in // width or in height. if ($dimensions['width'] !== $original_dimensions['width']) { $x = image_filter_keyword( $this->configuration['anchor']['width'], $original_dimensions['width'], $dimensions['width'] ); $x = ImageUtility::getKeywordOffset($this->configuration['anchor']['width'], $original_dimensions['width'], $dimensions['width']); $y = 0; } elseif ($dimensions['height'] !== $original_dimensions['height']) { $x = 0; $y = image_filter_keyword( $this->configuration['anchor']['height'], $original_dimensions['height'], $dimensions['height'] ); $y = ImageUtility::getKeywordOffset($this->configuration['anchor']['height'], $original_dimensions['height'], $dimensions['height']); } else { // If the image already has the correct dimensions, do not do anything. Loading
src/Plugin/ImageEffect/SetCanvasImageEffect.php +2 −2 Original line number Diff line number Diff line Loading @@ -224,8 +224,8 @@ class SetCanvasImageEffect extends ConfigurableImageEffectBase { // Get offset of original image. if ($this->configuration['canvas_size'] === 'exact') { list($x_pos, $y_pos) = explode('-', $this->configuration['exact']['placement']); $data['x_pos'] = image_filter_keyword($x_pos, $data['width'], $image->getWidth()) + $this->configuration['exact']['x_offset']; $data['y_pos'] = image_filter_keyword($y_pos, $data['height'], $image->getHeight()) + $this->configuration['exact']['y_offset']; $data['x_pos'] = ImageUtility::getKeywordOffset($x_pos, $data['width'], $image->getWidth()) + $this->configuration['exact']['x_offset']; $data['y_pos'] = ImageUtility::getKeywordOffset($y_pos, $data['height'], $image->getHeight()) + $this->configuration['exact']['y_offset']; } else { $data['x_pos'] = $this->configuration['relative']['left']; Loading