Error 500 in image_captcha image

Created on 22 June 2023, over 1 year ago
Updated 7 November 2023, about 1 year ago

Problem/Motivation

I use the image_captcha for the log in form but the image doesn't appear and it returns the following error:

TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given a preg_match().

The line causing the error is this one:

image_captcha/src/Response/CaptchaImageResponse.php(397): $bbox = imagettfbbox($font_size, 0, $this->fileSystem->realpath($font), $character);

🐛 Bug report
Status

Closed: cannot reproduce

Version

2.0

Component

Image Captcha (image_captcha)

Created by

🇪🇸Spain roger.bertran Barcelona

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

Comments & Activities

  • Issue created by @roger.bertran
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    44 pass
  • 🇪🇸Spain roger.bertran Barcelona

    I've created this patch to fix it, works for me.

  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks for the report. Sorry, but the reason isn't really clear to me at the moment, and nobody reported this before. So we should find out, if this might be a project-specific configuration error or a general issue.

    Your fix looks like a workaround, as

    $font = $fonts[array_rand($fonts)];
    

    should always return only one item. Why does it return more than one for you? Any ideas?

  • 🇳🇱Netherlands corneboele

    I'm encountering the same issue.

    Yes,
    $font = $fonts[array_rand($fonts)];
    returns only one.

    But
    $font = _image_captcha_get_font_uri($font);
    doesn't.

    It returns an array of all fonts available, because in _image_captcha_get_font_uri it expects a token, but a font path is provided.

    The fix works, even on 1.x, but is probably not the correct solution.

  • 🇦🇺Australia camerongreen Brisbane

    Also getting this error.

  • 🇩🇪Germany Anybody Porta Westfalica
  • Issue was unassigned.
  • 🇩🇪Germany Anybody Porta Westfalica
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    44 pass
  • 🇳🇱Netherlands corneboele

    Attached a new temporary patch for latest version.

  • 🇩🇪Germany Anybody Porta Westfalica

    @corneboele any difference to #2

    Could someone prepare this as MR please?

  • 🇳🇱Netherlands corneboele

    @Anybody just that it applies to 1.12/2.x latest. But the patch is imo not the right solution, see #4. I think we need to fix that not a font path is given to `_image_captcha_get_font_uri`, but a token.

  • Status changed to Needs work over 1 year ago
  • 🇩🇪Germany Anybody Porta Westfalica

    as of #10

  • 🇲🇦Morocco redamakhchan

    I'm facing the same issue after I did a composer update today, for #04 🐛 Error 500 in image_captcha image Closed: cannot reproduce , the param is already a token I confirm (key value of the $fonts).

    Just the $fonts recieved on param at captcha/modules/image_captcha/src/Service/ImageCaptchaRenderService.php:293, are not the same $available_fonts returned by _image_captcha_get_available_fonts_from_directories() on web/modules/contrib/captcha/modules/image_captcha/image_captcha.module:54. the list is different, which cause the token to not be found.

  • 🇲🇦Morocco redamakhchan

    ok found it I think $fonts returned on web/modules/contrib/captcha/modules/image_captcha/src/Service/ImageCaptchaRenderService.php:97:

      public function generateImage($code) {
        $fonts = _image_captcha_get_enabled_fonts();
    

    Are not the same as $fonts returned on web/modules/contrib/captcha/modules/image_captcha/image_captcha.module:52 :

    function _image_captcha_get_font_uri($token = NULL) {
      $fonts = [
        'BUILTIN' => 'BUILTIN',
      ];
      $available_fonts = _image_captcha_get_available_fonts_from_directories();
      foreach ($available_fonts as $file_token => $font_info) {
        $fonts[$file_token] = $font_info['uri'];
      }
      return ((!empty($token) && !empty($fonts[$token])) ? $fonts[$token] : $fonts);
    }
    

    What fixed the issue for me is to do drush updb, after doing it I started to see common fonts.

  • 🇩🇪Germany Anybody Porta Westfalica

    Did everyone here run update.php or drush updb after the update?
    Any please also run drush cr afterwards. Who still has the issue then?

  • 🇩🇪Germany daveiano

    Confirming that drush updb resolves the issue. I forgot to run it locally on my dev machine.

  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks @daveiano! :)

  • Status changed to Closed: cannot reproduce over 1 year ago
  • 🇩🇪Germany Anybody Porta Westfalica
  • Our deployment process runs drush deploy, which I understand runs database updates and cache rebuild. However for the sake of completeness I ran drush updb and drush cr, and we are still seeing this issue.

    The suggestion from #5 of setting a font did resolve the error.

  • I've revisited this.
    Now using drupal 10.0.11, captcha 2.0.5 and image_captcha 2.0.5 on a staging site and the error shows.
    Using `drush updb` and `drush cr` does not resolve the issue.

    However, now after setting a font I'm no longer seeing the error.

Production build 0.71.5 2024