Verified Commit fa4cdaf3 authored by Dave Long's avatar Dave Long
Browse files

Issue #2350849 by mondrake, fietserwin, ankithashetty, jhedstrom: Deprecate image_filter_keyword()

parent 1853daaf
Loading
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -56,4 +56,29 @@ public static function scaleDimensions(array &$dimensions, $width = NULL, $heigh
    return TRUE;
  }

  /**
   * 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 {
    return match ($anchor) {
      'bottom', 'right' => $current_size - $new_size,
      'center' => (int) round($current_size / 2 - $new_size / 2),
      'top', 'left' => 0,
      default => throw new \InvalidArgumentException("Invalid anchor '{$anchor}' provided to getKeywordOffset()"),
    };
  }

}
+6 −0
Original line number Diff line number Diff line
@@ -336,8 +336,14 @@ function template_preprocess_image_style(&$variables) {
 * @return int|string
 *   The offset from the anchor, in pixels, or the anchor itself, if its value
 *   isn't one of the accepted values.
 *
 * @deprecated in drupal:11.1.0 and is removed from drupal:12.0.0. Use
 *   \Drupal\Component\Utility\Image::getKeywordOffset() instead.
 *
 * @see https://www.drupal.org/node/3268441
 */
function image_filter_keyword($anchor, $current_size, $new_size) {
  @trigger_error('image_filter_keyword() is deprecated in drupal:11.1.0 and is removed from drupal:12.0.0. Use \Drupal\Component\Utility\Image::getKeywordOffset() instead. See https://www.drupal.org/node/3268441', E_USER_DEPRECATED);
  switch ($anchor) {
    case 'top':
    case 'left':
+3 −2
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\image\Plugin\ImageEffect;

use Drupal\Component\Utility\Image;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
@@ -22,8 +23,8 @@ class CropImageEffect extends ResizeImageEffect {
   */
  public function applyEffect(ImageInterface $image) {
    [$x, $y] = explode('-', $this->configuration['anchor']);
    $x = image_filter_keyword($x, $image->getWidth(), $this->configuration['width']);
    $y = image_filter_keyword($y, $image->getHeight(), $this->configuration['height']);
    $x = Image::getKeywordOffset($x, $image->getWidth(), (int) $this->configuration['width']);
    $y = Image::getKeywordOffset($y, $image->getHeight(), (int) $this->configuration['height']);
    if (!$image->crop($x, $y, $this->configuration['width'], $this->configuration['height'])) {
      $this->logger->error('Image crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
      return FALSE;
+5 −4
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\image\Plugin\ImageEffect;

use Drupal\Component\Utility\Image;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\image\Attribute\ImageEffect;
@@ -20,13 +21,13 @@ class ScaleAndCropImageEffect extends CropImageEffect {
   * {@inheritdoc}
   */
  public function applyEffect(ImageInterface $image) {
    $width = $this->configuration['width'];
    $height = $this->configuration['height'];
    $width = (int) $this->configuration['width'];
    $height = (int) $this->configuration['height'];
    $scale = max($width / $image->getWidth(), $height / $image->getHeight());

    [$x, $y] = explode('-', $this->configuration['anchor']);
    $x = image_filter_keyword($x, $image->getWidth() * $scale, $width);
    $y = image_filter_keyword($y, $image->getHeight() * $scale, $height);
    $x = Image::getKeywordOffset($x, (int) round($image->getWidth() * $scale), $width);
    $y = Image::getKeywordOffset($y, (int) round($image->getHeight() * $scale), $height);

    if (!$image->apply('scale_and_crop', ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height])) {
      $this->logger->error('Image scale and crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType(), '%dimensions' => $image->getWidth() . 'x' . $image->getHeight()]);
+0 −1
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@
 * - image.module:
 *   image_style_options()
 *   \Drupal\image\ImageStyleInterface::flush()
 *   image_filter_keyword()
 */

/**
Loading