0

I'm trying to make a captcha in php. This is my code:

$amountChars = 5;
$randString = substr(md5(uniqid()), 0, $amountChars);
$img = imagecreatfromjpeg("noise.jpg");
imageantialias($img, true);

$x = 20;
$y = 35;
$deltaX = 40;

for($i = 0; $i < $amountChars; $i++){
    $size = rand(18, 28);
    $r = rand(0,152);
    $g = rand(0,152);
    $b = rand(0,152);
    $color = imagecolorallocate($img, $r, $g, $b);
    $angle = -30 + rand(0, 60);
    imagettftext($img, $size, $angle, $x, $y, $color, "Blokletters-Balpen.ttf", $randString{$i});
    $x += $deltaX;
}

header("Content-Type: image/png");
imagePNG($img);

Problem is, that it does not work. I'm searching a mistake over an hour, but unsuccessfully.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
PiraTa
  • 475
  • 2
  • 8
  • 22
  • 1
    Please explain what is not working. (And how much a dinar is) – nl-x May 09 '14 at 18:57
  • images need to get rendered on their own page. – a coder May 09 '14 at 18:57
  • 1
    Why are you mixing PNG and JPEG ? (imagecreatefromjpeg, and later imagePNG) Are you sure that is allowed? If not, have you tried converting your base image to a PNG and use imagecreatefrompng ? – nl-x May 09 '14 at 18:59
  • **Placeholders_in_use** I have... **nl-x** image doesn't display. – PiraTa May 09 '14 at 19:00
  • Does `image_capcha.php` even render the output to a file? That is what you should try first. If that works, then check the PHP code again to see how to render it directly into an image `SRC` tag. – Giacomo1968 May 09 '14 at 20:20
  • If you look at my answer, this seems to be a simple typographical answer. If there is more to this that we are not understanding, please provide details. – Giacomo1968 May 09 '14 at 21:42

2 Answers2

0

Trying this again since I got flamed for not giving a real answer... Walking through your code step by step I was able to get it running with the following modifications:

$amountChars = 5;
$randString = substr(md5(uniqid()), 0, $amountChars);
// TYPO: MISSING E
$img = imagecreatefromjpeg("noise.jpg");

// IT'S POSSIBLE THIS FUNCTION ISN'T DEFINED:
// SEE REFERENCE LINK BELOW CODE
// imageantialias($img, true);

$x = 20;
$y = 35;
$deltaX = 40;

for($i = 0; $i < $amountChars; $i++) {
    $size = rand(18, 28);
    $r = rand(0,152);
    $g = rand(0,152);
    $b = rand(0,152);
    $color = imagecolorallocate($img, $r, $g, $b);
    $angle = -30 + rand(0, 60);
    // ADDED ./ TO FONT PATH, CHANGED $randString{$i} TO $randString[$i]
    imagettftext($img, $size, $angle, $x, $y, $color, "./Blokletters-Balpen.ttf", $randString[$i]);
    // WHY IS THIS HERE?
    $x += $deltaX;
}
header("Content-Type: image/png");
imagePNG($img);

The imageantialias function doesn't appear to be available on all systems. It wasn't available on mine, so I just commented it out.

Those changes should at least get your code working. If you're looking for a more in depth example, I recommend you review this tutorial.

Community
  • 1
  • 1
mister martin
  • 6,197
  • 4
  • 30
  • 63
-1

I took your code, created a noise.jpg images and even downloaded Blokletters-Balpen.ttf from here to see what’s up. A quick look at your code shows a typo:

$img = imagecreatfromjpeg("noise.jpg");

Shouldn’t that be imagecreatefromjpeg? Note the missing e in your original code:

$img = imagecreatefromjpeg("noise.jpg");

It should work now, I assume? Unless there are issues you are having placing it in <img src="image_capcha.php" />?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103