ValueError: imagecreatetruecolor(): Argument #1 ($width) must be greater than 0 in imagecreatetruecolor()

Created on 24 July 2023, 11 months ago
Updated 27 July 2023, 11 months ago

Problem/Motivation

Here is the Error in the log message:

ValueError: imagecreatetruecolor(): Argument #1 ($width) must be greater than 0 in imagecreatetruecolor() (line 103 of /var/www/web/modules/contrib/captcha/modules/image_captcha/src/Service/ImageCaptchaRenderService.php)
#0 /var/www/web/modules/contrib/captcha/modules/image_captcha/src/Service/ImageCaptchaRenderService.php(103): imagecreatetruecolor(0.0, 0)
#1 /var/www/web/modules/contrib/captcha/modules/image_captcha/src/Controller/CaptchaImageGeneratorController.php(123): Drupal\image_captcha\Service\ImageCaptchaRenderService->generateImage('')
#2 [internal function]: Drupal\image_captcha\Controller\CaptchaImageGeneratorController->image(Object(Symfony\Component\HttpFoundation\Request))
#3 /var/www/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#4 /var/www/web/core/lib/Drupal/Core/Render/Renderer.php(580): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#5 /var/www/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#6 /var/www/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#7 /var/www/vendor/symfony/http-kernel/HttpKernel.php(169): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#8 /var/www/vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#9 /var/www/web/modules/contrib/simple_oauth/src/HttpMiddleware/BasicAuthSwap.php(67): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#10 /var/www/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#11 /var/www/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#12 /var/www/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(191): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#13 /var/www/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(128): Drupal\page_cache\StackMiddleware\PageCache->fetch(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#14 /var/www/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(82): Drupal\page_cache\StackMiddleware\PageCache->lookup(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#15 /var/www/web/modules/contrib/services/src/StackMiddleware/FormatSetter.php(44): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#16 /var/www/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\services\StackMiddleware\FormatSetter->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#17 /var/www/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\ban\BanMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#18 /var/www/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#19 /var/www/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#20 /var/www/web/core/lib/Drupal/Core/DrupalKernel.php(718): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#21 /var/www/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#22 {main}

Steps to reproduce

1. installed the captcha 2.0.3
2. Enable the image captcha as well.
3. After configure to all the form in the project

The image is not loading in the form.

Proposed resolution

Image size should be greater than 0 while calculated.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Postponed: needs info

Version

2.0

Component

Image Captcha (image_captcha)

Created by

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

Comments & Activities

  • Issue created by @jungKunwar
  • Also, I got this error on the console whenever I refresh the image.

    http://local-local.docksal.site/image-captcha-generate/70/1690235624 500 (500 Service unavailable (with message))
    Image (async)
    attr @ jquery.min.js?v=3.6.3:2
    B @ jquery.min.js?v=3.6.3:2
    attr @ jquery.min.js?v=3.6.3:2
    (anonymous) @ image_captcha_refresh.js?v=9.5.9:33
    c @ jquery.min.js?v=3.6.3:2
    fireWith @ jquery.min.js?v=3.6.3:2
    l @ jquery.min.js?v=3.6.3:2
    (anonymous) @ jquery.min.js?v=3.6.3:2
    load (async)
    send @ jquery.min.js?v=3.6.3:2
    ajax @ jquery.min.js?v=3.6.3:2
    E.<computed> @ jquery.min.js?v=3.6.3:2
    (anonymous) @ image_captcha_refresh.js?v=9.5.9:28
    dispatch @ jquery.min.js?v=3.6.3:2
    y.handle @ jquery.min.js?v=3.6.3:2

    any help will be appreciated.
    Thank you.

  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks, could someone prepare a MR to fix this for the next release, combined with 🐛 Error float>int conversion after update to version 2.0.3 Fixed

  • 🇮🇳India ayush.pandey

    Tested the module with version 2.x-dev on Drupal 10 with PHP 8.1 and the issue seems to not reproducible (enabled on all forms).

  • That's weird. I also tried the

    2.0.3 Version.
    PHP: 8.1
    Drupal: 9.5.9

    I will try with the 2.x-dev and keep update here.

  • Status changed to Postponed: needs info 11 months ago
  • 🇩🇪Germany Anybody Porta Westfalica

    @jungKunwar looks like a local issue. Please check and resave your image captcha settings.
    What values do you use for

    • image_captcha_font_size
    • image_captcha_character_spacing

    and which font did you choose?

    /**
    * Helper function for calculating image height and width.
    *
    * They are based on given code and current font/spacing settings.
    *
    * @param string $code
    * The utf8 string which will be used to split in characters.
    *
    * @return array
    * [$width, $heigh].
    */
    function _image_captcha_image_size($code) {
    $config = \Drupal::config('image_captcha.settings');
    $font_size = (int) $config->get('image_captcha_font_size');
    $character_spacing = (float) $config->get('image_captcha_character_spacing');
    $characters = _image_captcha_utf8_split($code);
    $character_quantity = count($characters);

    // Calculate height and width.
    $width = $character_spacing * $font_size * $character_quantity;
    $height = 2 * $font_size;

    // Return the values as full pixel values (no floats):
    return [(int) $width, (int) $height];
    }

Production build 0.69.0 2024