Commit 4546653c authored by mschudders's avatar mschudders Committed by mondrake
Browse files

Issue #3268087 by Mschudders: PHP 8.1 deprecation warning - float to int in imagecopy setCanvas

parent fa6a379a
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -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()");
  }

}
+3 −2
Original line number Diff line number Diff line
@@ -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;
@@ -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'],
+2 −2
Original line number Diff line number Diff line
@@ -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());
+3 −10
Original line number Diff line number Diff line
@@ -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.
@@ -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.
+2 −2
Original line number Diff line number Diff line
@@ -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