hi all,
I am using captcha and image_captcha, and i was wondering if it is possible to render a png (transparent background) instead of the standard jpg?
Does this module have an option to select the type of image?
Everything else is working perfect.
thanks

Comments

soxofaan’s picture

Title: Can captcha module create a png instead of a jpg??? » image CAPTCHA in PNG format (with possibly transparent background)
Version: 6.x-2.0-rc2 » 6.x-2.x-dev
Category: support » feature

This is certainly possible, however not implemented :)
using PNG instead of JPG requires a two line change in image_captcha.user.inc (around line 33):

    drupal_set_header("Content-type: image/png"); // instead of drupal_set_header("Content-type: image/jpeg");
...
    imagepng($image); // instead of imagejpeg($image);

(The choice between PNG and JPEG is pretty arbitrary, but a reason I chose JPEG is because it gives some extra noise and artifacts, which makes automatic character recognition slightly harder.)

Having a transparent background would involve some more changes however (mainly an appropriate call to imagecolortransparent() in _image_captcha_generate_image(). I don't have time to implement this for the moment, but I welcome patches :)

machi27’s picture

thanks for answering so quick.
I have done the first part, setting the image a png, worked perfect,
But i am stuck with calling imagecolortransparent(),
I never used this before so i had to read a litle, after placing imagecolortransparent($image, $background_color); on line 92 nothing really happens,
i tried to define a color to be replace with the trans, in that case i get a black background.
any suggestion?

machi27’s picture

Finally made it, it is working if Distortion level<=1.
I feel like an idiot, I shoud it try to disable the distorsion before, I spent to much time searching for info and it was done at the first try, so i gues that i have to get a bit deper with the function that control the distorsion.
I think that after solving (if i solved it) this issue adding the posibility to the module to choose between jpg or png and giving the option to use a transparent background would be great.

machi27’s picture

soxofaan,

I have been working on the patch, and i would like to ask you few things.
- i have added a select list where user can select what image type you wish, jpg png... working great
- about the transparent background it is working as long there is no distorsion, i am still working on that.
I would like to know what do you think about which is the best option,
1. get ready of the image type selector, i guess that as long an user doesnt want a transparent background image there is no need for a png image, add instead a checkbox to give the option for a transparent image and in case is TRUE, create png, FALSE crate jpg???
2. leave the type image select and add a checkbox to select or not background's transparency???

I am having troubles to make the transparency work as long as distorsion is higher than 1, have you ever tried to do this and leave it because you found it is imposible???

Thanks

soxofaan’s picture

Hi machi27,

Thanks for your work and don't be afraid to post unfinished patches (as long as you mark them as such).
That way I can give you more feedback earlier.

Regarding the file type selection:
I think we should also have a look at file sizes (I don't have access to my Drupal development environment for the moment, so I can't test this now), but I guess the average file size of the JPGs will be smaller than the PNGs (especially if distortion and noise are enabled).
In this regard: PNG only has added value when transparency is desired.

In any case: I would avoid checkboxes, because they would make things more complicated UI-wise than needed.
I would use a select list (or radio buttons) with options:
* JPEG
* PNG with transparent background
If required/desired/requested, we then can always add an option
* PNG (without transparent background)
without much problem.

about your problem with transparency and distortion: please post your unfinished patch, so I can have a look

machi27’s picture

Hi soxofaan,

I agree with you, I just finish those modifications, so now we have a select list with the mentioned options,
transparency it's working with all the other noise options (salt & peper and lines) in any range.
but still not working with distorsion > 1. :(

It didnt take me to much time, it was pretty easy and I used a pretty basic solution, maybe you found the script to basic... I was still trying to found a solution for the dist/trans issue, but i guess that as you said it is gonna be better and faster to upload the patch with the transparency(distorsion bug.

I need to download somethings and make the patch, will upload asap.

machi27’s picture

StatusFileSize
new26.97 KB

HI soxofaan,

I think that i havent done good the patch, it looks a bit weird and long to me, I use the tortoise cvs for windows, also tried the cygwin but it was even worst.
i think i am missing something in the patch settings coz it is adding to many lines and headers doesnt look like in a normal patch.
anyway i am gonna uploaded to let you see it.

Still working with the transparency/distortion bug...

soxofaan’s picture

Status: Active » Needs work

Indeed, the patch is not ok.

I don't use windows, so I can't help you with tortoise.
You should find more info at
http://drupal.org/patch
or more specifically at http://drupal.org/node/113172

If you keep struggling, you could also just post the files you changed, but I would prefer a patch.

There have also been some updates since you checked out the version you are working on
so you should run "cvs update".

thanks

machi27’s picture

i have updated the cvs, and version that i am using is the last dev, downloaded today.
I knew that the patch was wrong,
And yea i was using those pages as refer, gonna give another try.
hope i can make it this time

machi27’s picture

StatusFileSize
new6.37 KB
new3.59 KB
new6.8 KB

***************** forget about this, SORRY!!!! **************************
i hope i am not making you waste your time

machi27’s picture

StatusFileSize
new6.93 KB
new1.11 KB
new3.17 KB

*************** sorry u can forget about this too *******************
I am sorry i was checking i think that i understand now what you mean saying that i wasnt working with the last version, i hope now i did, i am not very sure yet about the patch, i made it by cygwin, with the diif -up, looks better than before but stills a bit weird. I also uploaded a zip with image_captcha.user.in and image_captcha.admin.inc
Hope didnt make you waste your time,

machi27’s picture

Hi soxofaan,

finally WORKS!!!!!! :))
I think that the last modications that you made helped a lot, (working with the last cvs), I guess that maybe my final code still a bit dirty even after cleanning it.
I notice that to render a png transparent background best quality "background color must be set to #FFF", with background = #000, letters are a bit mess. so shouild I add in background color description a litle note???
I checked with all distorsion and noise posibilities, and it is working good for me.
I dont understand why at the end it have been soo easy to make it work, I am using the first code that i wrote but it didnt work first time???
So i guess that you should forget about my latest post and whenever you have time could please check the NEW files, I am not very good making a patch but i am gonna try again.

Thanks and hope didnt make you waste your time...

machi27’s picture

StatusFileSize
new6.67 KB

soxofaan,
Here we go, I hope patch is ok this time,
also upload the files in case my patch is not good.
pls let me know.
Thanks

machi27’s picture

sorry, something weird happened and didnt uploaded the patches, here we go

soxofaan’s picture

Hi Machi27, apparently you've been busy working on this ;)

Some feedback:

  • the patches like in #11 and #14 are almost good (against latest version and only showing what you changed), but best is to put everything in one patch file (so not one patch file per source file). I also have to convert the windows end of lines (carriage return + linefeed) to unix end of lines (linefeed only). What editor are you using? it seems to convert the unix end of lines to windows end of lines. Maybe there is an option in you editor to preserve the unix end of lines (drupal uses unix end of lines).
  • I also noticed you changed the lines with "$Id ...$". No need to do this: those lines are automatically generated by the CVS-system, so they will be overwritten anyway.
  • I noticed you use hardcoded values 1, 2 and for the image types. A better code style (better readability and code maintenance) to use constants. In image_captcha.module you should first define a constant, e.g.
    define('IMAGE_CAPTCHA_FILE_FORMAT_JPG', 1);
    define('IMAGE_CAPTCHA_FILE_FORMAT_PNG', 2);
    define('IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG', 3);
    

    and then use these constants instead of hardcoded values.

  • In Drupal we value our code standards very high: http://drupal.org/coding-standards. There are some indentation issues in you patch: indentation should be 2 spaces per level.
  • It's a bit silly to create a fieldset for only one setting. And the feature is also rather advanced, so I would not put it that high on the settings page. I would put it in the "color settings" fieldset and change its title to "Color and image settings".
  • you're mixing "jpg" and "JPG", I would do it all in capitals (they are abbreviations, not words) and "JPEG" instead of "JPG".
  • in image_captcha.admin.patch: variable_get('image_captcha_type_img', 'jpg') this is wrong: the first argument should be the variable name, which is 'image_captcha_image_format' and the second should be the key of the options array (e.g. the constant IMAGE_CAPTCHA_FILE_FORMAT_JPG I defined higher)
  • in image_captcha.user.patch: $img_type = variable_get('image_captcha_image_format'); this is also wrong, the first argument is ok, but there should be a second argument too
  • about you getting the best results when the background is set to #fff: this is because your page background is probably white or at least a light color. On a design with a black background, you get best results if the captcha background is also set to #000. So the rule should be: set it close to the underlying background color.

This may look like a lot, but I guess this is one of your first Drupal patches (or maybe your first), and it takes some time before you get all those small details.
So no problem, I hope you've learned some things.

thanks for your work

machi27’s picture

Yep you are right, I have done many personal modifications but never a real patch before, and yea i am learning a lot about all this things. which is the main goal...

Zendstudio, gonna take a look to see if there is as option to do that, hope it does. I am using cygwin to make the patch, tortoise to download the last cvs . I am not sure about how to make "diff -up" with all the files that i want to compare.

Newbie mistake modifying the $Id... sorry

I understand and agree with all the other things.

I have done all those changes and carefully looked at the code standards, hope not missing anything.

soxofaan’s picture

I am using cygwin to make the patch, tortoise to download the last cvs . I am not sure about how to make "diff -up" with all the files that i want to compare.

Check if your version of diff under Cygwin has the option --strip-trailing-cr, maybe that will avoid the end of line problems. And you just have to list the files you want to create a patch for (if you don't specify files, diff will take all files that differ. So the following command should do it, I think:
diff -upw --strip-trailing-cr image_captcha.module image_captcha.admin.inc image_captcha.user.inc > pngtransparency.patch
(note the "-w" option is for ignoring changes in whitespace, which is often a good idea to add)

And again: no problem with the newbie mistakes, we've all been newbies. I hope you enjoy hacking Drupal.

machi27’s picture

StatusFileSize
new5.81 KB

I enjoyed to much, :)
Ok this is what i finally get, I made all those changes, and i think code looks a bit cleaner.
I found the option in zend to use end lines as linux.
I am not sure if this is the final result but we are getting closer, maybe i missed something or the patch is not clean enough...

soxofaan’s picture

Status: Needs work » Needs review
StatusFileSize
new6.23 KB

Getting closer indeed.

I think you missed some spots to replace the hardcoded values to the constants
e.g. in several cases of variable_get('image_captcha_image_format', 1) and in the option list of the select widget. There are also some places where there are tabs instead of spaces for indentation.
Some UI strings need some rewording too.

I worked a bit on the patch and changed these small things myself.
be welcome to try it out

machi27’s picture

ouch, i guess all this mistake are the different between a noob an a pro which i am far of it

i was comparing bolth files, damm it dont understand who could i miss e.g $img_type = variable_get('image_captcha_image_format', 1);, I should give it a bit more of time before posting it, just making sure about those things, about the tabs/space issue dont know what to say... :(. About the widget i was confuse, wasn't sure and tried to respect the way you defined the other widgets, but now i understand my mistake. :)

I notice a few changes and i am wonder if you could please explain.

*/ image_captcha.user.inc, I define drupal_set_header("Cache-Control: max-age=3600, must-revalidate"); in the if and in the else, but you only defined in the if. should it be define in both???

/* image_captcha.user.inc, i define $img_type twice, one for IMAGE_CAPTCHA_FILE_FORMAT_JPG and them redifine it for IMAGE_CAPTCHA_FILE_FORMAT_TRANSPARENT_PNG, but you define $img_type and $file_format, I guess (please correct if I am wrong) it must be done your way for avoiding mixing vars???

*/ concerning the "patch making" and the "end of lines" issue, was it ok that way?

what do you mean with

Some UI strings need some rewording too.

, e.g. changing image_captcha_color_settings???

I can only say thanks a lot, I am learning a lot trying to make a patch, i am a bit dissapointment because i couldn't make it perfect.

soxofaan’s picture

Hi,

no need to despair, you are doing fine ;)

I just wanted to tweak some small things that would be easier to do in the code than posting them here as a comment.

Anyway, about your questions:

  1. drupal_set_header("Cache-Control: max-age=3600, must-revalidate"); was code for the 6.x-1.x branch that was no needed anymore, so I removed it all together.
  2. about $img_type and $file_format you are right, I mixed some stuff. I wanted to harmonize the names of the constants (IMAGE_CAPTCHA_FILE_FORMAT_JPG), the drupal variable (image_captcha_file_format) and the local variables (file_format), but I forgot to change one case of "img_type". I prefer "file_format" over "img_format" (which could also refer to the image size) and "img_type" (which could also refer to color/grayscale settings). Patch in attachment should be better.
  3. I didn't experience any end-of-line problems anymore whit your patches, so that should be fine, thanks
  4. about the UI string rewording, you had
    Enter the hexadecimal code for the background color (e.g. #FFF or #FFCE90).[br/] For png-transparent-background best result set it close to the underlying background color.

    I removed the [br/], because it's best to avoid unnecessary markup and your sentence about PNG-transparency didn't look right grammatically. I changed it to

    When using the PNG file format with transparent background, it is recommended to set this close to the underlying background color.

    Another thing I tweaked was putting "JPEG" and "PNG" in t() (not really important) and also changed from "PNG - transparent background" to "PNG with transparent background" because the minus/dash in your version could be interpreted as "without". Another thing I did, was putting the file format option at the end of the "color and image settings" fieldset, because it is typically the least important.

I hope this gives you more insight in my changes.
Thanks anyway for your work

soxofaan’s picture

StatusFileSize
new6.05 KB

really annoying that previewing comments doesn't work #580564: Comment preview doesn't work anymore, I have to edit my post several times and then forget to attach the patch.
So here it is ;)

soxofaan’s picture

StatusFileSize
new6.11 KB

Ok, forget previous patch
this one should be more readable/understandable

machi27’s picture

hi!!!
So... did we/YOU finnally make it??? i have tested and it is working great.
I am sorry that at the end you had to spent some time doing this... but I am glad that as result your captcha module have improve even if no one use the transparency background.
I will definetly use it.

Thanks a lot for everything

soxofaan’s picture

Title: image CAPTCHA in PNG format (with possibly transparent background) » image CAPTCHA in PNG format: check GDlib's PNG support
Status: Needs review » Needs work

yep, I committed the patch:
http://drupal.org/cvs?commit=266448
congratulations and thanks for your work and review.

There is still some follow up work to do:
now the GD-library is only checked for JPG-support (see e.g. #580364: Image CAPTCHA reports no GD, Drupal status report disagrees), I think checking for PNG support should be added too.

machi27’s picture

StatusFileSize
new2.42 KB

I am not sure if is this what we needed, maybe I forgot to touch another file, I have tried and it is working as before, but as I have those GD-lib I dont get any error.

Please let me know if it is OK, (patch is ok?)
Had special care about the tab and space and the end of lines as linux.

BTW i saw that you have introduced new fonts, none worked for me :(, but with my old font everything was perfect.

********forget about this patch, check the nextone********

machi27’s picture

StatusFileSize
new2.39 KB

I am sorry I noticed after uploading i made a mistake not very important but in this file is solved

machi27’s picture

I realize that with this changes the script is checking both JPEG and PNG and display an error message no matter what format is used.
Do you think that it will be better to make the script to first check the image format and depending selected format check if current lib is ok???

soxofaan’s picture

Priority: Normal » Minor
Status: Needs work » Fixed

I think now with the addition of PNG support, things get more complicated.
But I think it's a bit out of the scope of this issue thread, so I decided to start a new one for this: #589388: refactor _image_captcha_check_setup() for GD/JPEG/PNG support and set this thread to fixed.
Thanks anyway

Status: Fixed » Closed (fixed)

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