Problem/Motivation

Upgrade Status module reports the following PHP 8.4 deprecation problems for a D11 migration:

modules/contrib/captcha/modules/image_captcha/src/Service/ImageCaptchaRenderService.php	128	Call to deprecated function lcg_value().
modules/contrib/captcha/modules/image_captcha/src/Service/ImageCaptchaRenderService.php	129	Call to deprecated function lcg_value().
modules/contrib/captcha/modules/image_captcha/src/Service/ImageCaptchaRenderService.php	132	Call to deprecated function lcg_value().
modules/contrib/captcha/modules/image_captcha/src/Service/ImageCaptchaRenderService.php	133	Call to deprecated function lcg_value().

Steps to reproduce

Install the upgrade_status module on 10.x site with Captcha installed..
Scan the captcha module

See https://www.php.net/manual/en/function.lcg-value.php

Proposed resolution

Change the lcg_value calls to use the Random extension. E.g:

$randomizer = new Random\Randomizer();
$random_float = $randomizer->getFloat();

Note this was added in PHP 8.2. But since PHP 8.1 officially reached End of Life (EOL) on December 31, 2025 this shouldn't be a blocker.

Remaining tasks

Make changes

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Issue fork captcha-3570435

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

cgmonroe created an issue. See original summary.

cgmonroe’s picture

Status: Active » Needs review
StatusFileSize
new1.53 KB

Here's a patch to remove the deprecated calls

anybody’s picture

Version: 2.0.9 » 2.x-dev
Status: Needs review » Needs work

@cgmonroe could you please provide this as MR, not patch? Thanks!

anybody’s picture

Priority: Normal » Major
Status: Needs work » Needs review
lfersoy’s picture

Patch confirmed. +1 RTBC

anybody’s picture

Issue summary: View changes
anybody’s picture

Still needs someone to set it RTBC once reviewed.

herved’s picture

There are also other deprecations for 8.5, like imagedestroy(), but maybe that should be a followup?

------ --------------------------------------------------------------------------------------------------------------------------------------- 
  Line   captcha/captcha.install                                                                                                                
 ------ --------------------------------------------------------------------------------------------------------------------------------------- 
  119    Call to deprecated function node_type_get_names():                                                                                     
         in drupal:11.3.0 and is removed from drupal:13.0.0. Use \Drupal::service('entity_type.bundle.info')->getBundleLabels('node') instead.  
         🪪  function.deprecated                                                                                                                
 ------ --------------------------------------------------------------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------- 
  Line   captcha/modules/image_captcha/image_captcha.admin.inc  
 ------ ------------------------------------------------------- 
  67     Call to deprecated function imagedestroy().            
         🪪  function.deprecated                                
 ------ ------------------------------------------------------- 

 ------ ------------------------------------------------------------------------------- 
  Line   captcha/modules/image_captcha/src/Controller/CaptchaFontPreviewController.php  
 ------ ------------------------------------------------------------------------------- 
  100    Call to deprecated function imagedestroy().                                    
         🪪  function.deprecated                                                        
 ------ ------------------------------------------------------------------------------- 

 ------ ---------------------------------------------------------------------------------- 
  Line   captcha/modules/image_captcha/src/Controller/CaptchaImageGeneratorController.php  
 ------ ---------------------------------------------------------------------------------- 
  144    Call to deprecated function imagedestroy().                                       
         🪪  function.deprecated                                                           
 ------ ---------------------------------------------------------------------------------- 

 ------ ------------------------------------------------------------------------- 
  Line   captcha/modules/image_captcha/src/Service/ImageCaptchaRenderService.php  
 ------ ------------------------------------------------------------------------- 
  128    Call to deprecated function lcg_value().                                 
         🪪  function.deprecated                                                  
  129    Call to deprecated function lcg_value().                                 
         🪪  function.deprecated                                                  
  132    Call to deprecated function lcg_value().                                 
         🪪  function.deprecated                                                  
  133    Call to deprecated function lcg_value().                                 
         🪪  function.deprecated                                                  
 ------ -------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------ 
  Line   captcha/src/Element/Captcha.php                                                           
 ------ ------------------------------------------------------------------------------------------ 
  41     Call to method __construct() of deprecated class Drupal\Core\Render\Element\FormElement:  
         in drupal:10.3.0 and is removed from drupal:12.0.0. use                                   
           \Drupal\Core\Render\Element\FormElementBase instead.                                    
         🪪  method.deprecatedClass                                                                
 ------ ------------------------------------------------------------------------------------------
anybody’s picture

@herved yes I think let's do that separately.

csakiistvan’s picture

Status: Needs review » Reviewed & tested by the community

Environment

  • Drupal 11.3.9, PHP 8.3.30, DDEV v1.25.2
  • drupal/captcha: 2.0.10

Step 1 — Install the module

composer require drupal/captcha
ddev drush en captcha image_captcha -y

Step 2 — Enable distortion to trigger the affected code path

The lcg_value() calls only execute when image_captcha_distortion_amplitude is greater than 1:

ddev drush config:set image_captcha.settings image_captcha_distortion_amplitude 5 -y

Step 3 — Apply the fix from MR !131

In modules/image_captcha/src/Service/ImageCaptchaRenderService.php, replace the four lcg_value() calls with \Random\Randomizer:

$randomizer = new \Random\Randomizer();
$wavelength_xr = (2 + 3 * $randomizer->getFloat(0, 1)) * $font_size;
$wavelength_yr = (2 + 3 * $randomizer->getFloat(0, 1)) * $font_size;
// ...
$wavelength_xt = (2 + 3 * $randomizer->getFloat(0, 1)) * $font_size;
$wavelength_yt = (2 + 3 * $randomizer->getFloat(0, 1)) * $font_size;

Step 4 — Verify the fix

Syntax check:

ddev exec php -l web/modules/contrib/captcha/modules/image_captcha/src/Service/ImageCaptchaRenderService.php

Expected: No syntax errors detected

Confirm the image endpoint returns a valid image:

curl -s -o /dev/null -w "%{http_code} %{content_type}" https://tester.ddev.site/image-captcha-generate/{session_id}/{timestamp}

Expected: 200 image/jpeg

Results

Test Result
Module installs and enables without errors ✅ Pass
All four lcg_value() calls replaced with \Random\Randomizer::getFloat() ✅ Pass
PHP syntax check passes after the change ✅ Pass
CAPTCHA image generates successfully with distortion enabled ✅ Pass

The fix works correctly. \Random\Randomizer::getFloat() is available since PHP 8.2 and is the appropriate replacement for the deprecated lcg_value().

This comment was produced with the assistance of an LLM

anybody’s picture

Thanks, merged into dev to push things forward here. Could we please have a separate follow-up for #9?

anybody’s picture

Status: Reviewed & tested by the community » Fixed

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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

pfrenssen’s picture

hoporr’s picture

(issue was closed, cannot reopen, so I created a second issue with this text)

Captcha 2.0.10:
Drupal core 11.3.13
PHP 8.4

1) Tried to install the MR patch, and it failed.

2) I then installed the patch from #2, and am getting this error when the captcha is supposed to be rendered (also on the image_captcha config page):

ParseError: syntax error, unexpected token ".", expecting identifier or variable or "{" or "$" in {closure:Composer\Autoload\ClassLoader::initializeIncludeClosure():575}() (Zeile 131 in /...../www/modules/contrib/captcha/modules/image_captcha/src/Service/ImageCaptchaRenderService.php).

The patch applied like this -- note the '.' in front of "getFloat" , so yes here is a problem:
$wavelength_yr = (2 + 3 * $randomizer->.getFloat(0, 1)) * $font_size;

I then inspected the actual patches, and noticed this:

patch #2 actually has the error in it, note the '.' :
+ $wavelength_yr = (2 + 3 * $randomizer->.getFloat(0, 1)) * $font_size;

In the MR, the line is correct:
+ $wavelength_yr = (2 + 3 * $randomizer->getFloat(0, 1)) * $font_size;

But the MR fails to install for me.