Problem/Motivation

The AddWatermark operation triggers a deprecated warning in PHP 8.1+ due to implicit float-to-int conversion. The lack of explicit type casting or rounding results in a loss of precision and a deprecated warning. This issue impacts compatibility with modern PHP versions.

PHP Version: 8.3.6

Steps to reproduce

  1. Install Drupal 10.3.*
  2. Install the Basic Watermark module, version 2.1.0
  3. Go to admin/config/media/image-styles/manage/media_library
  4. Add a new "Add watermark" effect
  5. After configuring the new effect, click "Add effect". You will be redirected to "admin/config/media/image-styles/manage/media_library" where you will see the error message: "Deprecated function: Implicit conversion from float 42.5 to int loses precision in Drupal\basic_watermark\Plugin\ImageToolkit\Operation\gd\AddWatermark->execute() (line 89 of modules/contrib/basic_watermark/src/Plugin/ImageToolkit/Operation/gd/AddWatermark.php)."

error

Proposed resolution

Explicitly cast or round the $margins['x'] and $margins['y'] values to integers before passing them to the imagecopy() function

solution
success
After

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

eric.vvf created an issue. See original summary.

eric.vvf’s picture

ekapus’s picture

Mikechr, can we get this reviewed and merged?? I'd love to solve this for my projects with an update instead of manually applying the fix.

ekapus’s picture

I also found another place this happens, on line 157.

Updating it to this solves it:
$watermark_image = imagescale($watermark_image, (int) $watermark['width'], (int) $watermark['height']);

sandeep_k’s picture

StatusFileSize
new430.57 KB

I attempted to reproduce this issue on Drupal version 10.3.1-dev by following all the steps mentioned above. However, I was unable to replicate the error on my D10 setup. I’m not sure if any additional steps are required. I’ve included the results here after adding and saving the image style.

mikechr’s picture

I'm happy to merge this, I'll wait for @ekapus to add the extra fix he mention at #5

vinodhini.e’s picture

Hi, I encountered this issue on Drupal 10.5.1.

Steps I followed:

Installed the module on Drupal 10.5.1.
Created a new image style and added the Add Watermark effect.
Encountered the warning message when trying to save the effect.
Applied Merge Request #4.
Repeated the same steps to add the watermark effect.

The warning message still appears, and the issue is not resolved by this patch.

eric.vvf’s picture

Extra validation added, @mikechr

mikechr’s picture

Status: Needs review » Fixed

Merged

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.