my captcha_image module failes to generate the images.

From http://drupal.org/node/205525, i get some code to check imagegd2 module.

if (function_exists('imagegd2')) {
  var_export(gd_info());
}
$fs = array('imagejpeg', 'imagecreatetruecolor', 'imagedestroy',
  'imagecolorallocate', 'imagefilledrectangle', 'imagecolorat', 'imagesetpixel',
  'imageline', 'imagefontwidth', 'imagettfbbox', 'imagestring', 'imagettftext',
);
foreach($fs as $f) {
  print "$f: ". var_export(function_exists($f), TRUE) .'<br />';
}

then, the output is

array ( 'GD Version' => 'bundled (2.0.34 compatible)', 'FreeType Support' => true, 'FreeType Linkage' => 'with freetype', 'T1Lib Support' => true, 'GIF Read Support' => true, 'GIF Create Support' => true, 'JPG Support' => true, 'PNG Support' => true, 'WBMP Support' => true, 'XPM Support' => false, 'XBM Support' => true, 'JIS-mapped Japanese Font Support' => false, )imagejpeg: true
imagecreatetruecolor: true
imagedestroy: true
imagecolorallocate: true
imagefilledrectangle: true
imagecolorat: true
imagesetpixel: true
imageline: true
imagefontwidth: true
imagettfbbox: true
imagestring: true
imagettftext: true

seems everything is fine, then what's the problem?

thanks.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

soxofaan’s picture

FileSize
114.82 KB

Do you get an image on the image CAPTCHA settings page (like in attached screenshot)?

Also, does the math CAPTCHA work, including blocking on wrong answer?

gnosis.kv’s picture

the math captcha works well, but i did not get the pic in the admin either.

soxofaan’s picture

Please try the following:

disable line 390 in image_captcha.module (v 5.x-3.1):

  // unset the code from $_SESSION to prevent rerendering the CAPTCHA
  // unset($_SESSION['image_captcha'][$seed]);

This makes it possible to request the image more than one time, which makes debugging easier

Now go to a form or the admin page where a CAPTCHA image should show up. Try to find out the url of the image, you can do this for example by looking in the html source or right clicking on the image and chose something as "view image" (depending on which browser you use). The url should be something like http://example.com/?q=image_captcha/715730387 .
Load this url by pasting it in the address bar of your browser and report the error message if any.

gnosis.kv’s picture

thansk soxofaan,

i tried, but it still does not work. i tried open the imgage directly like http://example.com/?q=image_captcha/715730387 , then i got an "red cross"(empty image).

i tried image_captcha on another server, still end up with the same problem.

soxofaan’s picture

The red cross is with Internet Explorer, I presume? Can you try a different browser (Firefox, opera, ...)?

What happens if you replace that line 390 with

echo $code;

and again open a CAPTCHA image directly with the URL?

Can you also check your watchdog log (at example.com/?q=admin/logs/watchdog) for (image) CAPTCHA related error messages?

gnosis.kv’s picture

FileSize
15.47 KB

I tried to do

dpm($code);

in the line 390. it returns me the code genereted by captcha.

i also checked watchdog, there is no error for captcha.

screen shot attached. here.

gnosis.kv’s picture

i tried to download the image file from

http://example.com/?q=image_captcha/715730387

then manually rename it as 715730387.jpg.
the jpg file can be viewed and edited in Photoshop CS, but it cannot be opened in "Windows Picture and Fax viewer".

soxofaan’s picture

I don't think there is something wrong with

    drupal_set_header("Content-type: image/jpeg");

but maybe removing

     drupal_set_header("Cache-Control: max-age=3600, must-revalidate");

helps for you?

soxofaan’s picture

the jpg file can be viewed and edited in Photoshop CS, but it cannot be opened in "Windows Picture and Fax viewer".

Can you view it in your web browser?
Please attach that file.

gnosis.kv’s picture

FileSize
5.73 KB

it cannot be viewed in broswer (either ie or ff).

file attached.

gnosis.kv’s picture

i guess there is sth wrong with my GD.

soxofaan’s picture

Status: Active » Fixed

I think I found the problem.

If you look at the binary content of that file, you'll see that it starts with four bytes of newlines, before the JPEG data. Most viewers will fail on this.

The reason: you probably have a module or include file in your setup that ends with "?>" followed by two new lines. These new lines are always emitted on every request before any real content.
It is recommended in php to not use a closing "?>" at the end of the file (also see http://drupal.org/node/247058#comment-875378)
The problem is probably in a custom module because Drupal core and most contrib modules don't use a closing "?>".

soxofaan’s picture

Title: Captcha_image fails to generate the images. » corrupted images with image CAPTCHA
gnosis.kv’s picture

thanks so much, soxofaan.

i have removed the all the "?>" from all the module files. (suprisely, quite a lot of contrib module got this ending ?>), but the the problem is still there.

anyway, i think the direction you pointed is correct, i am trying to disable the modules one by one to find the root cause.

any other suggestion??

gnosis.kv’s picture

finally, find out the reason. In of my customized module file, i mistakelly added 2 new lines at a beginning.

a BIG lesson for me. thanks soxofaan .

soxofaan’s picture

Title: corrupted images with image CAPTCHA » no CAPTCHA image because of corrupted images

you're welcome

indeed, whitespace before the opening "<?php" is equally evil, but that doesn't happen as often as trailing whitespace after the closing "?>" so I didn't mention it.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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

matheusgorino’s picture

I searched for "?>" and "{space}<?php" in all my .module and .inc files and didn't find any error.
Not sure how warm it is, but I fixed the issue by calling ob_clean() before rendering the image, on the line 26 of the image_captcha.user.inc file.

jaddamo’s picture

I know that issue is closed but maybe this information helps someone else.
matheusgorino, THANKS!!!

I had the same problem. When installing the module CAPTCHA worked fine, but after installing another module could verify that stopped working. I don't remember which module installed first.

I changed the line 26 with the function ob_clean() and the image will display correctly. Thanks for writing the solution as I have been reading for days the forum and had not found anything.

I'm using Drupal v.7 on a server Debian 5.0.7.
Modules installed:
- Captcha
- Views
- Chaos tools suite
- Calendar
- Date
- Media
- Entity API
- Rules
- Services
- Services authentication
- Userpoints

Thank again!!
Juan

goldengalaxy’s picture

Thanks a lot matheusgorino, your solution did solve the issue. It really saved me from drowning as I was having the issue on live site.

Note - Later I realized that this is taken care in the latest version of captcha module. I was using 6.x-2.4 version of the module and the fix is implemented in 6.x-2.5 version of the module.

yogen.prasad’s picture

Issue summary: View changes

Thanks a lot soxofaan , one of my custom .module file had a space before <?php.