[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/phpbb/captcha/ -> char_cube3d.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\captcha;
  15  
  16  class char_cube3d
  17  {
  18      var $bitmap;
  19      var $bitmap_width;
  20      var $bitmap_height;
  21  
  22      var $basis_matrix = array(array(1, 0, 0), array(0, 1, 0), array(0, 0, 1));
  23      var $abs_x = array(1, 0);
  24      var $abs_y = array(0, 1);
  25      var $x = 0;
  26      var $y = 1;
  27      var $z = 2;
  28      var $letter = '';
  29  
  30      /**
  31      */
  32  	function __construct(&$bitmaps, $letter)
  33      {
  34          $this->bitmap            = $bitmaps['data'][$letter];
  35          $this->bitmap_width        = $bitmaps['width'];
  36          $this->bitmap_height    = $bitmaps['height'];
  37  
  38          $this->basis_matrix[0][0] = mt_rand(-600, 600);
  39          $this->basis_matrix[0][1] = mt_rand(-600, 600);
  40          $this->basis_matrix[0][2] = (mt_rand(0, 1) * 2000) - 1000;
  41          $this->basis_matrix[1][0] = mt_rand(-1000, 1000);
  42          $this->basis_matrix[1][1] = mt_rand(-1000, 1000);
  43          $this->basis_matrix[1][2] = mt_rand(-1000, 1000);
  44  
  45          $this->normalize($this->basis_matrix[0]);
  46          $this->normalize($this->basis_matrix[1]);
  47          $this->basis_matrix[2] = $this->cross_product($this->basis_matrix[0], $this->basis_matrix[1]);
  48          $this->normalize($this->basis_matrix[2]);
  49  
  50          // $this->basis_matrix[1] might not be (probably isn't) orthogonal to $basis_matrix[0]
  51          $this->basis_matrix[1] = $this->cross_product($this->basis_matrix[0], $this->basis_matrix[2]);
  52          $this->normalize($this->basis_matrix[1]);
  53  
  54          // Make sure our cube is facing into the canvas (assuming +z == in)
  55          for ($i = 0; $i < 3; ++$i)
  56          {
  57              if ($this->basis_matrix[$i][2] < 0)
  58              {
  59                  $this->basis_matrix[$i][0] *= -1;
  60                  $this->basis_matrix[$i][1] *= -1;
  61                  $this->basis_matrix[$i][2] *= -1;
  62              }
  63          }
  64  
  65          // Force our "z" basis vector to be the one with greatest absolute z value
  66          $this->x = 0;
  67          $this->y = 1;
  68          $this->z = 2;
  69  
  70          // Swap "y" with "z"
  71          if ($this->basis_matrix[1][2] > $this->basis_matrix[2][2])
  72          {
  73              $this->z = 1;
  74              $this->y = 2;
  75          }
  76  
  77          // Swap "x" with "z"
  78          if ($this->basis_matrix[0][2] > $this->basis_matrix[$this->z][2])
  79          {
  80              $this->x = $this->z;
  81              $this->z = 0;
  82          }
  83  
  84          // Still need to determine which of $x,$y are which.
  85          // wrong orientation if y's y-component is less than it's x-component
  86          // likewise if x's x-component is less than it's y-component
  87          // if they disagree, go with the one with the greater weight difference.
  88          // rotate if positive
  89          $weight = (abs($this->basis_matrix[$this->x][1]) - abs($this->basis_matrix[$this->x][0])) + (abs($this->basis_matrix[$this->y][0]) - abs($this->basis_matrix[$this->y][1]));
  90  
  91          // Swap "x" with "y"
  92          if ($weight > 0)
  93          {
  94              list($this->x, $this->y) = array($this->y, $this->x);
  95          }
  96  
  97          $this->abs_x = array($this->basis_matrix[$this->x][0], $this->basis_matrix[$this->x][1]);
  98          $this->abs_y = array($this->basis_matrix[$this->y][0], $this->basis_matrix[$this->y][1]);
  99  
 100          if ($this->abs_x[0] < 0)
 101          {
 102              $this->abs_x[0] *= -1;
 103              $this->abs_x[1] *= -1;
 104          }
 105  
 106          if ($this->abs_y[1] > 0)
 107          {
 108              $this->abs_y[0] *= -1;
 109              $this->abs_y[1] *= -1;
 110          }
 111  
 112          $this->letter = $letter;
 113      }
 114  
 115      /**
 116      * Draw a character
 117      */
 118  	function drawchar($scale, $xoff, $yoff, $img, $background, $colours)
 119      {
 120          $width    = $this->bitmap_width;
 121          $height    = $this->bitmap_height;
 122          $bitmap    = $this->bitmap;
 123  
 124          $colour1 = $colours[array_rand($colours)];
 125          $colour2 = $colours[array_rand($colours)];
 126  
 127          $swapx = ($this->basis_matrix[$this->x][0] > 0);
 128          $swapy = ($this->basis_matrix[$this->y][1] < 0);
 129  
 130          for ($y = 0; $y < $height; ++$y)
 131          {
 132              for ($x = 0; $x < $width; ++$x)
 133              {
 134                  $xp = ($swapx) ? ($width - $x - 1) : $x;
 135                  $yp = ($swapy) ? ($height - $y - 1) : $y;
 136  
 137                  if ($bitmap[$height - $yp - 1][$xp])
 138                  {
 139                      $dx = $this->scale($this->abs_x, ($xp - ($swapx ? ($width / 2) : ($width / 2) - 1)) * $scale);
 140                      $dy = $this->scale($this->abs_y, ($yp - ($swapy ? ($height / 2) : ($height / 2) - 1)) * $scale);
 141                      $xo = $xoff + $dx[0] + $dy[0];
 142                      $yo = $yoff + $dx[1] + $dy[1];
 143  
 144                      $origin = array(0, 0, 0);
 145                      $xvec = $this->scale($this->basis_matrix[$this->x], $scale);
 146                      $yvec = $this->scale($this->basis_matrix[$this->y], $scale);
 147                      $face_corner = $this->sum2($xvec, $yvec);
 148  
 149                      $zvec = $this->scale($this->basis_matrix[$this->z], $scale);
 150                      $x_corner = $this->sum2($xvec, $zvec);
 151                      $y_corner = $this->sum2($yvec, $zvec);
 152  
 153                      imagefilledpolygon($img, $this->gen_poly($xo, $yo, $origin, $xvec, $x_corner,$zvec), 4, $colour1);
 154                      imagefilledpolygon($img, $this->gen_poly($xo, $yo, $origin, $yvec, $y_corner,$zvec), 4, $colour2);
 155  
 156                      $face = $this->gen_poly($xo, $yo, $origin, $xvec, $face_corner, $yvec);
 157  
 158                      imagefilledpolygon($img, $face, 4, $background);
 159                      imagepolygon($img, $face, 4, $colour1);
 160                  }
 161              }
 162          }
 163      }
 164  
 165      /*
 166      * return a roughly acceptable range of sizes for rendering with this texttype
 167      */
 168  	function range()
 169      {
 170          return array(3, 4);
 171      }
 172  
 173      /**
 174      * Vector length
 175      */
 176  	function vectorlen($vector)
 177      {
 178          return sqrt(pow($vector[0], 2) + pow($vector[1], 2) + pow($vector[2], 2));
 179      }
 180  
 181      /**
 182      * Normalize
 183      */
 184  	function normalize(&$vector, $length = 1)
 185      {
 186          $length = (( $length < 1) ? 1 : $length);
 187          $length /= $this->vectorlen($vector);
 188          $vector[0] *= $length;
 189          $vector[1] *= $length;
 190          $vector[2] *= $length;
 191      }
 192  
 193      /**
 194      */
 195  	function cross_product($vector1, $vector2)
 196      {
 197          $retval = array(0, 0, 0);
 198          $retval[0] =  (($vector1[1] * $vector2[2]) - ($vector1[2] * $vector2[1]));
 199          $retval[1] = -(($vector1[0] * $vector2[2]) - ($vector1[2] * $vector2[0]));
 200          $retval[2] =  (($vector1[0] * $vector2[1]) - ($vector1[1] * $vector2[0]));
 201  
 202          return $retval;
 203      }
 204  
 205      /**
 206      */
 207  	function sum($vector1, $vector2)
 208      {
 209          return array($vector1[0] + $vector2[0], $vector1[1] + $vector2[1], $vector1[2] + $vector2[2]);
 210      }
 211  
 212      /**
 213      */
 214  	function sum2($vector1, $vector2)
 215      {
 216          return array($vector1[0] + $vector2[0], $vector1[1] + $vector2[1]);
 217      }
 218  
 219      /**
 220      */
 221  	function scale($vector, $length)
 222      {
 223          if (count($vector) == 2)
 224          {
 225              return array($vector[0] * $length, $vector[1] * $length);
 226          }
 227  
 228          return array($vector[0] * $length, $vector[1] * $length, $vector[2] * $length);
 229      }
 230  
 231      /**
 232      */
 233  	function gen_poly($xoff, $yoff, &$vec1, &$vec2, &$vec3, &$vec4)
 234      {
 235          $poly = array();
 236          $poly[0] = $xoff + $vec1[0];
 237          $poly[1] = $yoff + $vec1[1];
 238          $poly[2] = $xoff + $vec2[0];
 239          $poly[3] = $yoff + $vec2[1];
 240          $poly[4] = $xoff + $vec3[0];
 241          $poly[5] = $yoff + $vec3[1];
 242          $poly[6] = $xoff + $vec4[0];
 243          $poly[7] = $yoff + $vec4[1];
 244  
 245          return $poly;
 246      }
 247  
 248      /**
 249      * dimensions
 250      */
 251  	function dimensions($size)
 252      {
 253          $xn = $this->scale($this->basis_matrix[$this->x], -($this->bitmap_width / 2) * $size);
 254          $xp = $this->scale($this->basis_matrix[$this->x], ($this->bitmap_width / 2) * $size);
 255          $yn = $this->scale($this->basis_matrix[$this->y], -($this->bitmap_height / 2) * $size);
 256          $yp = $this->scale($this->basis_matrix[$this->y], ($this->bitmap_height / 2) * $size);
 257  
 258          $p = array();
 259          $p[0] = $this->sum2($xn, $yn);
 260          $p[1] = $this->sum2($xp, $yn);
 261          $p[2] = $this->sum2($xp, $yp);
 262          $p[3] = $this->sum2($xn, $yp);
 263  
 264          $min_x = $max_x = $p[0][0];
 265          $min_y = $max_y = $p[0][1];
 266  
 267          for ($i = 1; $i < 4; ++$i)
 268          {
 269              $min_x = ($min_x > $p[$i][0]) ? $p[$i][0] : $min_x;
 270              $min_y = ($min_y > $p[$i][1]) ? $p[$i][1] : $min_y;
 271              $max_x = ($max_x < $p[$i][0]) ? $p[$i][0] : $max_x;
 272              $max_y = ($max_y < $p[$i][1]) ? $p[$i][1] : $max_y;
 273          }
 274  
 275          return array($min_x, $min_y, $max_x, $max_y);
 276      }
 277  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1