Autor Zpráva
Erixx
Profil
Zdravím znovu.
Tentokrát mám script na generování captchy.
V obrázku se zobrazují 4 náhodná písmena či číslice.
Já bych rád, aby se v obrázku ale zobrazila vždy jen jediná číslice a 3 písmena.
Poradíte?
Děkuji.


<?php
session_start(); 

$im = imagecreatefrompng("./captcha.png");

$chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6','7','8','9');
$str1 = $chars[mt_rand(0, count($chars)-1)];
$str2 = $chars[mt_rand(0, count($chars)-1)];
$str3 = $chars[mt_rand(0, count($chars)-1)];
$str4 = $chars[mt_rand(0, count($chars)-1)];



$font = "creativeblock.ttf";
$size = mt_rand(14, 18);

$_SESSION['captcha'] = $str1.$str2.$str3.$str4;

$angle = mt_rand(-5, 5);
$color = imagecolorallocate($im, 55, 55, 55);
$textsize = imagettfbbox($size, $angle, $font, $textstr);

$twidth = abs($textsize[2]-$textsize[0]);
$theight = abs($textsize[5]-$textsize[3]);

$x = mt_rand(5, 60);
$y = mt_rand(18, 35);

imagettftext($im, $size, $angle, $x, $y, $color, $font, $str1);
imagettftext($im, $size, $angle, $x+mt_rand(20, 25), $y+mt_rand(1, 3), $color, $font, $str2);
imagettftext($im, $size, $angle, $x+mt_rand(45, 50), $y+mt_rand(1, 3), $color, $font, $str3);
imagettftext($im, $size, $angle, $x+mt_rand(65, 70), $y+mt_rand(1, 3), $color, $font, $str4);

// Output the image 
header("Content-type: image/png"); 
imagepng($im);
imagedestroy($im);
?>
juriad
Profil
A v čem je problém? Prostě změníš logiku na řádcích 6 -- 10.
Micruss
Profil
Tak vytvoř nový array() kde budou číslice

$chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 't', 'u', 'v', 'w', 'x', 'y', 'z');
$int = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
$str1 = $int[mt_rand(0, count($int)-1)];
$str2 = $chars[mt_rand(0, count($chars)-1)];
$str3 = $chars[mt_rand(0, count($chars)-1)];
$str4 = $chars[mt_rand(0, count($chars)-1)];
Erixx
Profil
Micruss:
Jasný. S tím samostatným array to funguje.
Díky!
Amunak
Profil
Erixx:
Proč to generuješ tak složitě? Můžeš použít prostě:
function rndchar() {//generuje náhodné malé písmeno
  return chr(mt_rand(97,122));
}
$str = mt_rand(0,9) . rndchar() . rndchar() . rndchar();
$str = str_shuffle($str); //Tohle použij, jestli chceš mít to číslo na náhodné pozici
V $str máš pak řetězec, kde je jedno číslo a tři znaky. Všechno náhodné, a zapsané rozumně.

Opravena deklarace funkce rndchar
Erixx
Profil
Amunak:
Zobrazení 1 čísla na náhodné pozici by bylo dobré.
Tvoje řešení se mi bohužel nedaří vnořit do původního scriptu.
Toto řešení mi hodí samozřejmě číslici vždy na třetí pozici.

<?php
session_start(); 

$im = imagecreatefrompng("./captcha.png");

$chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 't', 'u', 'v', 'w', 'x', 'y', 'z');
$int = array(1, 2, 3, 4, 5);
$str1 = $chars[mt_rand(0, count($chars)-1)];
$str2 = $chars[mt_rand(0, count($chars)-1)];
$str3 = $int[mt_rand(0, count($int)-1)];
$str4 = $chars[mt_rand(0, count($chars)-1)];


$font = "creativeblock.ttf";
$size = mt_rand(14, 18);

$_SESSION['captcha'] = $str1.$str2.$str3.$str4;

$angle = mt_rand(-5, 5);
$color = imagecolorallocate($im, 55, 55, 55);
$textsize = imagettfbbox($size, $angle, $font, $textstr);

$twidth = abs($textsize[2]-$textsize[0]);
$theight = abs($textsize[5]-$textsize[3]);

$x = mt_rand(5, 60);
$y = mt_rand(18, 35);

imagettftext($im, $size, $angle, $x, $y, $color, $font, $str1);
imagettftext($im, $size, $angle, $x+mt_rand(20, 25), $y+mt_rand(1, 3), $color, $font, $str2);
imagettftext($im, $size, $angle, $x+mt_rand(45, 50), $y+mt_rand(1, 3), $color, $font, $str3);
imagettftext($im, $size, $angle, $x+mt_rand(65, 70), $y+mt_rand(1, 3), $color, $font, $str4);

// Output the image 
header("Content-type: image/png"); 
imagepng($im);
imagedestroy($im);
?>
Amunak
Profil
Erixx:
Místo řádků 6-12 vlož to, co jsem napsal, na řádku 17 pak dáš $_SESSION['captcha'] = $str; a na řádcích 29-32 budeš mít místo $str1, $str2 atd. proměnné $str[0], $str[1] atd.
Erixx
Profil
Amunak:

Obrázek se nezobrazí.
Zkoušel jsem dát za 6. řádek { a pak to níže uzavřít }, pak se zobrazí prázdný obrázek bez znaků.

<?php
session_start(); 

$im = imagecreatefrompng("./captcha.png");

function rndchar() 
  return chr(mt_rand(97,122));

$str = mt_rand(0,9) . rndchar() . rndchar() . rndchar();
$str = str_shuffle($str);

$font = "creativeblock.ttf";
$size = mt_rand(14, 18);

$_SESSION['captcha'] = $str;

$angle = mt_rand(-5, 5);
$color = imagecolorallocate($im, 55, 55, 55);
$textsize = imagettfbbox($size, $angle, $font, $textstr);

$twidth = abs($textsize[2]-$textsize[0]);
$theight = abs($textsize[5]-$textsize[3]);

$x = mt_rand(5, 60);
$y = mt_rand(18, 35);

imagettftext($im, $size, $angle, $x, $y, $color, $font, $str[0]);
imagettftext($im, $size, $angle, $x+mt_rand(20, 25), $y+mt_rand(1, 3), $color, $font, $str[1]);
imagettftext($im, $size, $angle, $x+mt_rand(45, 50), $y+mt_rand(1, 3), $color, $font, $str[2]);
imagettftext($im, $size, $angle, $x+mt_rand(65, 70), $y+mt_rand(1, 3), $color, $font, $str[3]);

header("Content-type: image/png"); 
imagepng($im);
imagedestroy($im);


?>
Tori
Profil
function rndchar() {
  return chr(mt_rand(97,122));
}
Erixx
Profil
Tori:
JJ, toto jsem zkoušel a nefungovalo.
ted jsem to zkusil znovu a je to funkční.
Díky za postřeh!


Amunak:
Díky za cenné rady!
Funguje.
Amunak
Profil
Aha, omlouvám se, nějak jsem si z JS zvykl zkráceně (bez závorek navíc) psát úplně všechno.

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0