Error 500 in image_captcha image

Created on 22 June 2023, about 2 years 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

Needs review

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 about 2 years 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 almost 2 years 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 almost 2 years 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 almost 2 years 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.

  • πŸ‡ΊπŸ‡ΈUnited States bburg Washington D.C.

    I was running into this error, and it resolved after I went into the module settings, selected a font, and saved. It seemed as if there weren't any selected. Not sure how it got into that state.

Production build 0.71.5 2024