| Autor | Zpráva | ||
|---|---|---|---|
| Erixx Profil |
#1 · Zasláno: 30. 9. 2013, 17:42:00
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 |
#2 · Zasláno: 30. 9. 2013, 17:47:43
A v čem je problém? Prostě změníš logiku na řádcích 6 -- 10.
|
||
| Micruss Profil |
#3 · Zasláno: 30. 9. 2013, 17:49:11
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 |
#4 · Zasláno: 30. 9. 2013, 18:00:16
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$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 |
#6 · Zasláno: 30. 9. 2013, 18:45:32
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 |
#7 · Zasláno: 30. 9. 2013, 19:23:30
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 |
#9 · Zasláno: 30. 9. 2013, 19:56:47
function rndchar() {
return chr(mt_rand(97,122));
} |
||
| Erixx Profil |
#10 · Zasláno: 30. 9. 2013, 20:03:29 · Upravil/a: Erixx
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 |
#11 · Zasláno: 30. 9. 2013, 22:34:21
Aha, omlouvám se, nějak jsem si z JS zvykl zkráceně (bez závorek navíc) psát úplně všechno.
|
||
|
Časová prodleva: 12 let
|
|||
0