Deprecated warning: Float to int conversion in imagecopy() due to non-integer margins in AddWatermark operation

Created on 12 December 2024, 9 months ago

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)."

Proposed resolution

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



After

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs review

Version

2.1

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @eric.vvf
  • Merge request !4FIX: fixing implicity conversion. → (Open) created by eric.vvf
  • 🇺🇸United States ekapus

    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.

  • 🇺🇸United States ekapus

    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']);

  • 🇮🇳India sandeep_k New Delhi

    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.

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

  • 🇮🇳India vinodhini.e chennai

    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.

Production build 0.71.5 2024