fork download
  1. /**********************************************************************
  2. //Crear texto curvados
  3. //Ondulación del texto en los ejes X e Y
  4. // Genero ondas verticales (eje X)
  5. //Genero ondas horizontales (eje Y)
  6. **********************************************************************/
  7.  
  8. $period = $scale*$periodoX;
  9. $amplitude = $scale*$amplitudX;
  10. $k = rand(0,100);
  11. for ($i = 0;$i < ($width*$scale);$i++) {
  12. imagecopy($im,$im,
  13. $i-1, sin($k+$i/$period) * $amplitude,
  14. $i,0,
  15. 1,$height*$scale);
  16. }
  17.  
  18. $period = $scale*$periodoY;
  19. $amplitude = $scale*$amplitudY;
  20. $k = rand(0,100);
  21. for ($i = 0;$i < ($height*$scale);$i++) {
  22. imagecopy($im,$im,
  23. sin($k+$i/$period) * $amplitude, $i-1,
  24. 0,$i,
  25. $width*$scale,1);
  26. }
  27.  
  28. /**********************************************************************
  29. //Texto desde un archivo .txt
  30. **********************************************************************/
  31.  
  32. function getDictionaryCaptchaText($extended = true) {
  33. $fp = fopen("words-es.txt", "r");
  34. $linea = rand(0, (filesize("words-es.txt")/8)-1);
  35. fseek($fp, 8*$linea);
  36. $text = trim(fgets($fp));
  37. fclose($fp);
  38. // Cambio vocales al azar
  39. if ($extended) {
  40. $text = str_split($text, 1);
  41. $vocals = array('a','e','i','o','u');
  42. foreach ($text as $i => $char) {
  43. if (mt_rand(0,1) && in_array($char, $vocals)) {
  44. $text[$i] = $vocals[mt_rand(0,4)];
  45. }
  46. }
  47. $text = implode('', $text);
  48. }
  49. return $text;
  50. }
  51.  
  52. /**********************************************************************
  53. //Alternación de color de textos
  54. **********************************************************************/
  55.  
  56. $colors = array(
  57. array(27,78,181), // azul
  58. array(22,163,35), // verde
  59. array(214,36,7), // rojo
  60. );
  61.  
  62. $color = $colors[mt_rand(0, sizeof($colors)-1)];
  63. $fg_color = imagecolorallocate($im, $color[0], $color[1], $color[2]);
  64.  
  65. /**********************************************************************
  66. //Disminuir el espaciado entre caracteres
  67. //Variación de tamaños de caracteres
  68. **********************************************************************/
  69.  
  70. $text = getDictionaryCaptchaText($extended);
  71.  
  72. // Definiciones de latipografía a utilizar
  73. // - font: archivo TTF
  74. // - minSize: tamaño de fuente mínimo a utilizar
  75. // - maxSize: tamaño de fuente máximo a utilizar
  76. // - condensation: Cantidad de pixeles que se quitará entre cada caracter
  77. $fontcfg = $fonts[mt_rand(0, sizeof($fonts)-1)];
  78.  
  79. $x = 20;
  80. for ($i=0; $i<=6; $i++) {
  81. $coords = imagettftext($im, rand($fontcfg['minSize'],$fontcfg['maxSize']), 0,
  82. $x, 47, $fg_color, 'fonts/'.$fontcfg['font'], substr($text, $i, 1));
  83. $x += ($coords[2]-$x)-$fontcfg['condensation'];
  84. }
  85.  
  86. /**********************************************************************
  87. //genera textos aleatorios alternando consonantes y vocales al azar:
  88. **********************************************************************/
  89.  
  90. function getCaptchaText() {
  91. $length = rand(5,7);
  92. $consonants = "abcdefghijlmnopqrstvwyz";
  93. $vocals = "aeiou";
  94. $text = "";
  95. $vocal = rand(0,1);
  96. for ($i=0; $i<$length; $i++) {
  97. if ($vocal) {
  98. $text .= substr($vocals, mt_rand(0, 4), 1);
  99. } else {
  100. $text .= substr($consonants, mt_rand(0, 22), 1);
  101. }
  102. $vocal = !$vocal;
  103. }
  104. return $text;
  105. }
  106.  
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
/**********************************************************************
//Crear texto curvados
//Ondulación del texto en los ejes X e Y
// Genero ondas verticales (eje X)
//Genero ondas horizontales (eje Y)
**********************************************************************/

$period    = $scale*$periodoX;
$amplitude = $scale*$amplitudX;
$k         = rand(0,100);
for ($i = 0;$i < ($width*$scale);$i++) {
 imagecopy($im,$im,
           $i-1, sin($k+$i/$period) * $amplitude,
           $i,0,
           1,$height*$scale);
}

$period    = $scale*$periodoY;
$amplitude = $scale*$amplitudY;
$k         = rand(0,100);
for ($i = 0;$i < ($height*$scale);$i++) {
   imagecopy($im,$im,
           sin($k+$i/$period) * $amplitude, $i-1,
           0,$i,
          $width*$scale,1);
}

/**********************************************************************
//Texto desde un archivo .txt
**********************************************************************/

function getDictionaryCaptchaText($extended = true) {
   $fp = fopen("words-es.txt", "r");
   $linea = rand(0, (filesize("words-es.txt")/8)-1);
   fseek($fp, 8*$linea);
   $text = trim(fgets($fp));
   fclose($fp);
   // Cambio vocales al azar
   if ($extended) {
       $text = str_split($text, 1);
       $vocals = array('a','e','i','o','u');
       foreach ($text as $i => $char) {
           if (mt_rand(0,1) && in_array($char, $vocals)) {
               $text[$i] = $vocals[mt_rand(0,4)];
           }
       }
       $text = implode('', $text);
   }
   return $text;
}

/**********************************************************************
//Alternación de color de textos
**********************************************************************/

$colors = array(
 array(27,78,181), // azul
 array(22,163,35), // verde
 array(214,36,7),  // rojo
);

$color = $colors[mt_rand(0, sizeof($colors)-1)];
$fg_color = imagecolorallocate($im, $color[0], $color[1], $color[2]);

/**********************************************************************
//Disminuir el espaciado entre caracteres
//Variación de tamaños de caracteres
**********************************************************************/

$text = getDictionaryCaptchaText($extended);

// Definiciones de latipografía a utilizar
// - font: archivo TTF
// - minSize: tamaño de fuente mínimo a utilizar
// - maxSize: tamaño de fuente máximo a utilizar
// - condensation: Cantidad de pixeles que se quitará entre cada caracter
$fontcfg = $fonts[mt_rand(0, sizeof($fonts)-1)];

$x = 20;
for ($i=0; $i<=6; $i++) {
   $coords = imagettftext($im, rand($fontcfg['minSize'],$fontcfg['maxSize']), 0,
           $x, 47, $fg_color, 'fonts/'.$fontcfg['font'], substr($text, $i, 1));
   $x += ($coords[2]-$x)-$fontcfg['condensation'];
}

/**********************************************************************
//genera textos aleatorios alternando consonantes y vocales al azar:
**********************************************************************/

function getCaptchaText() {
$length = rand(5,7);
$consonants = "abcdefghijlmnopqrstvwyz";
$vocals = "aeiou";
$text = "";
$vocal = rand(0,1);
for ($i=0; $i<$length; $i++) {
if ($vocal) {
  $text .= substr($vocals, mt_rand(0, 4), 1);
} else {
  $text .= substr($consonants, mt_rand(0, 22), 1);
}
$vocal = !$vocal;
}
return $text;
}