Verified Commit 3b44eb23 authored by Dave Long's avatar Dave Long
Browse files

Issue #3265953 by andypost, quietone, ankithashetty, _utsavsharma, mondrake,...

Issue #3265953 by andypost, quietone, ankithashetty, _utsavsharma, mondrake, smustgrave, xjm, alexpott, longwave: Deprecate resource mentions in GDToolkit method names
parent 387c2e72
Loading
Loading
Loading
Loading
+106 −33
Original line number Diff line number Diff line
@@ -28,11 +28,9 @@
class GDToolkit extends ImageToolkitBase {

  /**
   * A GD image resource.
   *
   * @var \GdImage|null
   * A GD image.
   */
  protected $resource = NULL;
  protected ?\GdImage $image = NULL;

  /**
   * Image type represented by a PHP IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG).
@@ -42,16 +40,16 @@ class GDToolkit extends ImageToolkitBase {
  protected $type;

  /**
   * Image information from a file, available prior to loading the GD resource.
   * Image information from a file, available prior to loading the GD object.
   *
   * This contains a copy of the array returned by executing getimagesize()
   * on the image file when the image object is instantiated. It gets reset
   * to NULL as soon as the GD resource is loaded.
   * to NULL as soon as the GD object is loaded.
   *
   * @var array|null
   *
   * @see \Drupal\system\Plugin\ImageToolkit\GDToolkit::parseFile()
   * @see \Drupal\system\Plugin\ImageToolkit\GDToolkit::setResource()
   * @see \Drupal\system\Plugin\ImageToolkit\GDToolkit::setImage()
   * @see http://php.net/manual/function.getimagesize.php
   */
  protected $preLoadInfo = NULL;
@@ -112,6 +110,47 @@ public static function create(ContainerInterface $container, array $configuratio
    );
  }

  /**
   * {@inheritdoc}
   */
  public function __get(string $name) {
    if ($name === 'resource') {
      @trigger_error('Accessing the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.', E_USER_DEPRECATED);
      return $this->image;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function __set(string $name, mixed $value): void {
    if ($name === 'resource') {
      @trigger_error('Setting the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.', E_USER_DEPRECATED);
      $this->image = $value;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function __isset(string $name): bool {
    if ($name === 'resource') {
      @trigger_error('Checking the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.', E_USER_DEPRECATED);
      return isset($this->image);
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function __unset(string $name): void {
    if ($name === 'resource') {
      @trigger_error('Unsetting the \Drupal\system\Plugin\ImageToolkit\GDToolkit::resource property is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::image instead.', E_USER_DEPRECATED);
      unset($this->image);
    }
  }

  /**
   * Sets the GD image resource.
   *
@@ -120,14 +159,18 @@ public static function create(ContainerInterface $container, array $configuratio
   *
   * @return $this
   *   An instance of the current toolkit object.
   *
   * @deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use
   *   \Drupal\system\Plugin\ImageToolkit\GDToolkit::setImage() instead.
   *
   * @see https://www.drupal.org/node/3265963
   */
  public function setResource($resource) {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::setImage() instead. See https://www.drupal.org/node/3265963', E_USER_DEPRECATED);
    if (!$resource instanceof \GdImage) {
      throw new \InvalidArgumentException('Invalid resource argument');
    }
    $this->preLoadInfo = NULL;
    $this->resource = $resource;
    return $this;
    return $this->setImage($resource);
  }

  /**
@@ -135,12 +178,43 @@ public function setResource($resource) {
   *
   * @return \GdImage|null
   *   The GD image resource, or NULL if not available.
   *
   * @deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use
   *   \Drupal\system\Plugin\ImageToolkit\GDToolkit::getImage() instead.
   *
   * @see https://www.drupal.org/node/3265963
   */
  public function getResource() {
    if (!$this->resource) {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use \Drupal\system\Plugin\ImageToolkit\GDToolkit::getImage() instead. See https://www.drupal.org/node/3265963', E_USER_DEPRECATED);
    return $this->getImage();
  }

  /**
   * Sets an image or resets existing one.
   *
   * @param \GdImage|null $image
   *   The GD image object or NULL.
   *
   * @return $this
   *   An instance of the current toolkit object.
   */
  public function setImage(?\GdImage $image): static {
    $this->preLoadInfo = NULL;
    $this->image = $image;
    return $this;
  }

  /**
   * Retrieves the image.
   *
   * @return \GdImage|null
   *   The GD image object, or NULL if not available.
   */
  public function getImage(): ?\GdImage {
    if (!$this->image) {
      $this->load();
    }
    return $this->resource;
    return $this->image;
  }

  /**
@@ -169,7 +243,7 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
  }

  /**
   * Loads a GD resource from a file.
   * Loads an image from a file.
   *
   * @return bool
   *   TRUE or FALSE, based on success.
@@ -194,7 +268,7 @@ protected function load() {

    // Invalidate the image object and return if the load fails.
    try {
      $resource = $function($this->getSource());
      $image = $function($this->getSource());
    }
    catch (\Throwable $t) {
      $this->logger->error("The image toolkit '@toolkit' failed loading image '@image'. Reported error: @class - @message", [
@@ -207,34 +281,33 @@ protected function load() {
      return FALSE;
    }

    $this->setResource($resource);
    if (imageistruecolor($resource)) {
    $this->setImage($image);
    if (imageistruecolor($image)) {
      return TRUE;
    }
    else {
      // Convert indexed images to truecolor, copying the image to a new
      // truecolor resource, so that filters work correctly and don't result
      // truecolor image, so that filters work correctly and don't result
      // in unnecessary dither.
      $data = [
        'width' => imagesx($resource),
        'height' => imagesy($resource),
        'width' => imagesx($image),
        'height' => imagesy($image),
        'extension' => image_type_to_extension($this->getType(), FALSE),
        'transparent_color' => $this->getTransparentColor(),
        'is_temp' => TRUE,
      ];
      if ($this->apply('create_new', $data)) {
        imagecopy($this->getResource(), $resource, 0, 0, 0, 0, imagesx($resource), imagesy($resource));
        imagedestroy($resource);
        imagecopy($this->getImage(), $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
      }
    }
    return (bool) $this->getResource();
    return (bool) $this->getImage();
  }

  /**
   * {@inheritdoc}
   */
  public function isValid() {
    return ((bool) $this->preLoadInfo || (bool) $this->resource);
    return ((bool) $this->preLoadInfo || isset($this->image));
  }

  /**
@@ -260,7 +333,7 @@ public function save($destination) {
    }
    if ($this->getType() == IMAGETYPE_JPEG) {
      try {
        $success = $function($this->getResource(), $destination, $this->configFactory->get('system.image.gd')->get('jpeg_quality'));
        $success = $function($this->getImage(), $destination, $this->configFactory->get('system.image.gd')->get('jpeg_quality'));
      }
      catch (\Throwable $t) {
        $this->logger->error("The image toolkit '@toolkit' failed saving image '@image'. Reported error: @class - @message", [
@@ -275,11 +348,11 @@ public function save($destination) {
    else {
      // Image types that support alpha need to be saved accordingly.
      if (in_array($this->getType(), [IMAGETYPE_PNG, IMAGETYPE_WEBP], TRUE)) {
        imagealphablending($this->getResource(), FALSE);
        imagesavealpha($this->getResource(), TRUE);
        imagealphablending($this->getImage(), FALSE);
        imagesavealpha($this->getImage(), TRUE);
      }
      try {
        $success = $function($this->getResource(), $destination);
        $success = $function($this->getImage(), $destination);
      }
      catch (\Throwable $t) {
        $this->logger->error("The image toolkit '@toolkit' failed saving image '@image'. Reported error: @class - @message", [
@@ -324,16 +397,16 @@ public function parseFile() {
   *   A color string like '#rrggbb', or NULL if not set or not relevant.
   */
  public function getTransparentColor() {
    if (!$this->getResource() || $this->getType() != IMAGETYPE_GIF) {
    if (!$this->getImage() || $this->getType() != IMAGETYPE_GIF) {
      return NULL;
    }
    // Find out if a transparent color is set, will return -1 if no
    // transparent color has been defined in the image.
    $transparent = imagecolortransparent($this->getResource());
    $transparent = imagecolortransparent($this->getImage());
    if ($transparent >= 0) {
      // Find out the number of colors in the image palette. It will be 0 for
      // truecolor images.
      $palette_size = imagecolorstotal($this->getResource());
      $palette_size = imagecolorstotal($this->getImage());
      if ($palette_size == 0 || $transparent < $palette_size) {
        // Return the transparent color, either if it is a truecolor image
        // or if the transparent color is part of the palette.
@@ -341,7 +414,7 @@ public function getTransparentColor() {
        // image rather than of the palette, it is possible that an image
        // could be created with this index set outside the palette size.
        // (see http://stackoverflow.com/a/3898007).
        $rgb = imagecolorsforindex($this->getResource(), $transparent);
        $rgb = imagecolorsforindex($this->getImage(), $transparent);
        unset($rgb['alpha']);
        return Color::rgbToHex($rgb);
      }
@@ -356,7 +429,7 @@ public function getWidth() {
    if ($this->preLoadInfo) {
      return $this->preLoadInfo[0];
    }
    elseif ($res = $this->getResource()) {
    elseif ($res = $this->getImage()) {
      return imagesx($res);
    }
    else {
@@ -371,7 +444,7 @@ public function getHeight() {
    if ($this->preLoadInfo) {
      return $this->preLoadInfo[1];
    }
    elseif ($res = $this->getResource()) {
    elseif ($res = $this->getImage()) {
      return imagesy($res);
    }
    else {
+7 −8
Original line number Diff line number Diff line
@@ -40,12 +40,12 @@ protected function validateArguments(array $arguments) {
   * {@inheritdoc}
   */
  protected function execute(array $arguments) {
    // Create a new resource of the required dimensions and format, and copy
    // the original resource on it with resampling. Destroy the original
    // resource upon success.
    // Create a new image of the required dimensions and format, and copy
    // the original image on it with resampling. Restore the original image upon
    // failure.
    $width = $this->getToolkit()->getWidth();
    $height = $this->getToolkit()->getHeight();
    $original_resource = $this->getToolkit()->getResource();
    $original_image = $this->getToolkit()->getImage();
    $original_type = $this->getToolkit()->getType();
    $data = [
      'width' => $width,
@@ -55,12 +55,11 @@ protected function execute(array $arguments) {
      'is_temp' => TRUE,
    ];
    if ($this->getToolkit()->apply('create_new', $data)) {
      if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, 0, 0, $width, $height, $width, $height)) {
        imagedestroy($original_resource);
      if (imagecopyresampled($this->getToolkit()->getImage(), $original_image, 0, 0, 0, 0, $width, $height, $width, $height)) {
        return TRUE;
      }
      // In case of error, reset resource and type to as it was.
      $this->getToolkit()->setResource($original_resource);
      // In case of error, reset image and type to as it was.
      $this->getToolkit()->setImage($original_image);
      $this->getToolkit()->setType($original_type);
    }
    return FALSE;
+16 −24
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@
 *   toolkit = "gd",
 *   operation = "create_new",
 *   label = @Translation("Set a new image"),
 *   description = @Translation("Creates a new transparent resource and sets it for the image.")
 *   description = @Translation("Creates a new transparent object and sets it for the image.")
 * )
 */
class CreateNew extends GDImageToolkitOperationBase {
@@ -39,7 +39,7 @@ protected function arguments() {
        'default' => '#ffffff',
      ],
      'is_temp' => [
        'description' => 'If TRUE, this operation is being used to create a temporary image by another GD operation. After performing its function, the caller is responsible for destroying the original GD resource.',
        'description' => 'If TRUE, this operation is being used to create a temporary image by another GD operation. After performing its function, the original GD object will be destroyed automatically.',
        'required' => FALSE,
        'default' => FALSE,
      ],
@@ -82,52 +82,44 @@ protected function execute(array $arguments) {
    // Get the image type.
    $type = $this->getToolkit()->extensionToImageType($arguments['extension']);

    // Store the original GD resource.
    $original_res = $this->getToolkit()->getResource();

    // Create the resource.
    if (!$res = imagecreatetruecolor($arguments['width'], $arguments['height'])) {
    // Create the image.
    if (!$image = imagecreatetruecolor($arguments['width'], $arguments['height'])) {
      return FALSE;
    }

    // Fill the resource with transparency as possible.
    // Fill the image with transparency as possible.
    switch ($type) {
      case IMAGETYPE_PNG:
      case IMAGETYPE_WEBP:
        imagealphablending($res, FALSE);
        $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
        imagefill($res, 0, 0, $transparency);
        imagealphablending($res, TRUE);
        imagesavealpha($res, TRUE);
        imagealphablending($image, FALSE);
        $transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
        imagefill($image, 0, 0, $transparency);
        imagealphablending($image, TRUE);
        imagesavealpha($image, TRUE);
        break;

      case IMAGETYPE_GIF:
        if (empty($arguments['transparent_color'])) {
          // No transparency color specified, fill white transparent.
          $fill_color = imagecolorallocatealpha($res, 255, 255, 255, 127);
          $fill_color = imagecolorallocatealpha($image, 255, 255, 255, 127);
        }
        else {
          $fill_rgb = Color::hexToRgb($arguments['transparent_color']);
          $fill_color = imagecolorallocatealpha($res, $fill_rgb['red'], $fill_rgb['green'], $fill_rgb['blue'], 127);
          imagecolortransparent($res, $fill_color);
          $fill_color = imagecolorallocatealpha($image, $fill_rgb['red'], $fill_rgb['green'], $fill_rgb['blue'], 127);
          imagecolortransparent($image, $fill_color);
        }
        imagefill($res, 0, 0, $fill_color);
        imagefill($image, 0, 0, $fill_color);
        break;

      case IMAGETYPE_JPEG:
        imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
        imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 255));
        break;

    }

    // Update the toolkit properties.
    $this->getToolkit()->setType($type);
    $this->getToolkit()->setResource($res);

    // Destroy the original resource if it is not needed by other operations.
    if (!$arguments['is_temp'] && is_resource($original_res)) {
      imagedestroy($original_res);
    }
    $this->getToolkit()->setImage($image);

    return TRUE;
  }
+7 −12
Original line number Diff line number Diff line
@@ -73,10 +73,10 @@ protected function validateArguments(array $arguments) {
   * {@inheritdoc}
   */
  protected function execute(array $arguments) {
    // Create a new resource of the required dimensions, and copy and resize
    // the original resource on it with resampling. Destroy the original
    // resource upon success.
    $original_resource = $this->getToolkit()->getResource();
    // Create a new image of the required dimensions, and copy and resize
    // the original image on it with resampling. Restore the original image upon
    // failure.
    $original_image = $this->getToolkit()->getImage();
    $data = [
      'width' => $arguments['width'],
      'height' => $arguments['height'],
@@ -85,16 +85,11 @@ protected function execute(array $arguments) {
      'is_temp' => TRUE,
    ];
    if ($this->getToolkit()->apply('create_new', $data)) {
      if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, $arguments['x'], $arguments['y'], $arguments['width'], $arguments['height'], $arguments['width'], $arguments['height'])) {
        imagedestroy($original_resource);
      if (imagecopyresampled($this->getToolkit()->getImage(), $original_image, 0, 0, $arguments['x'], $arguments['y'], $arguments['width'], $arguments['height'], $arguments['width'], $arguments['height'])) {
        return TRUE;
      }
      else {
        // In case of failure, destroy the temporary resource and restore
        // the original one.
        imagedestroy($this->getToolkit()->getResource());
        $this->getToolkit()->setResource($original_resource);
      }
      // In case of failure, restore the original image.
      $this->getToolkit()->setImage($original_image);
    }
    return FALSE;
  }
+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ protected function execute(array $arguments) {
      return FALSE;
    }

    return imagefilter($this->getToolkit()->getResource(), IMG_FILTER_GRAYSCALE);
    return imagefilter($this->getToolkit()->getImage(), IMG_FILTER_GRAYSCALE);
  }

}
Loading