Exemplo n.º 1
0
 public static function Create()
 {
     // Initial setup
     $string = self::GenerateCode();
     $font_file = INCLUDES_DIR . '/' . self::FONT;
     $box = imagettfbboxextended(self::FONT_SIZE, 0, $font_file, $string);
     $width = $box['width'] + self::PADDING_LEFT * 2;
     $height = $box['height'] + self::PADDING_TOP * 2;
     // Generate colors
     //self::$foreground_color = array(rand(0,100),rand(0,100),rand(0,100));
     //self::$background_color = array(rand(200,255),rand(200,255),rand(200,255));
     // Setup the image
     $image = imagecreatetruecolor($width, $height);
     $foreground = imagecolorallocate($image, self::$foreground_color[0], self::$foreground_color[1], self::$foreground_color[2]);
     $background = imagecolorallocate($image, self::$background_color[0], self::$background_color[1], self::$background_color[2]);
     imagealphablending($image, true);
     imagefill($image, 0, 0, $background);
     // Draw characters
     $offset = 0;
     for ($i = 0; $i < strlen($string); $i++) {
         $bb = imagettfbboxextended(self::FONT_SIZE, 0, $font_file, $string[$i]);
         imagettftext($image, self::FONT_SIZE, 0, $box['x'] + self::PADDING_LEFT + $offset, $box['y'] + self::PADDING_TOP + rand(-5, 5), $foreground, $font_file, $string[$i]);
         $offset += $bb['width'] + self::CHAR_OFFSET;
     }
     // Warp the text
     $image = self::Warp($image, $width, $height);
     // Set CAPTCHA cookie
     $session = sha1(uniqid(rand(), true));
     setcookie(self::COOKIE, $session, time() + self::EXPIRES, Config::Get('cookie_path'), Config::Get('cookie_domain'));
     // Insert code into database
     $DB = GetDB();
     $DB->Update('DELETE FROM # WHERE `timestamp` < ?', array(self::TABLE, time() - self::EXPIRES));
     $DB->Update("INSERT INTO # VALUES (?,?,?)", array(self::TABLE, $session, $string, time()));
     // Output the image
     if (function_exists('imagepng')) {
         header('Content-type: image/png');
         imagepng($image);
     } else {
         header('Content-type: image/jpeg');
         imagejpeg($image, null, 95);
     }
 }
Exemplo n.º 2
0
 function GenerateAndDisplay()
 {
     global $C;
     // Initial setup
     $string = $this->GenerateCode();
     $font_file = DIR_ASSETS . '/' . CAPTCHA_FONT;
     $box = imagettfbboxextended(CAPTCHA_FONT_SIZE, 0, $font_file, $string);
     $width = $box['width'] + CAPTCHA_PADDING_LEFT * 2;
     $height = $box['height'] + CAPTCHA_PADDING_TOP * 2;
     // Setup the image
     $image = imagecreatetruecolor($width, $height);
     $foreground = imagecolorallocate($image, $this->foreground_color[0], $this->foreground_color[1], $this->foreground_color[2]);
     $background = imagecolorallocate($image, $this->background_color[0], $this->background_color[1], $this->background_color[2]);
     imagealphablending($image, true);
     imagefill($image, 0, 0, $background);
     // Draw characters
     $offset = 0;
     for ($i = 0; $i < strlen($string); $i++) {
         $bb = imagettfbboxextended(CAPTCHA_FONT_SIZE, 0, $font_file, $string[$i]);
         imagettftext($image, CAPTCHA_FONT_SIZE, 0, $box['x'] + CAPTCHA_PADDING_LEFT + $offset, $box['y'] + CAPTCHA_PADDING_TOP + rand(-5, 5), $foreground, $font_file, $string[$i]);
         $offset += $bb['width'] + CAPTCHA_CHAR_OFFSET;
     }
     // Warp the text
     $image = $this->Warp($image, $width, $height);
     // Set CAPTCHA cookie
     $session = sha1(uniqid(rand(), true));
     setcookie(CAPTCHA_COOKIE, $session, time() + CAPTCHA_EXPIRES, $C['cookie_path'], $C['cookie_domain']);
     require_once 'textdb.php';
     $db = new CaptchasDB();
     $db->Add(array('session' => $session, 'code' => $string, 'timestamp' => time()));
     // Output the image
     if (function_exists('imagepng')) {
         header('Content-type: image/png');
         imagepng($image);
     } else {
         header('Content-type: image/jpeg');
         imagejpeg($image, null, 95);
     }
 }
Exemplo n.º 3
0
 /**
  * Draw the chord according to given parameters
  *
  */
 function draw($fname)
 {
     //header('Content-type: image/png');
     $img = imagecreatetruecolor($this->width, $this->height);
     if (!$img) {
         throw new Exception('Cannot Initialize new GD image stream');
     }
     // Values computed and used for the drawing
     //count number of strings and compute space between strings
     $nbStrings = count($this->data);
     $realWidth = $this->width - $this->margin['left'] - $this->margin['right'];
     $stringSpacing = $realWidth / ($nbStrings - 1);
     $upperSymbolSize = intval(round($stringSpacing * $this->upperSymbolSize));
     //space needed to draw the "o" or "x"
     $totalTopMargin = $this->margin['top'] + $upperSymbolSize;
     $titleMargin = 0;
     //if theres a title, compute additionnal top margin induced
     if (!empty($this->title)) {
         if (is_int($this->fontTitle)) {
             $titleMargin = imagefontheight($this->fontTitle);
         } else {
             $box = imagettfbboxextended($this->fontTitleSize, 0, $this->fontTitle, $this->title);
             $titleMargin = $box['height'];
         }
     }
     $totalTopMargin += $titleMargin;
     $realHeight = $this->height - $totalTopMargin - $this->margin['bottom'];
     $fretsSpacing = $realHeight / $this->maxFrets;
     //create colors
     $bgColor = imagecolorallocate($img, $this->bgColor[0], $this->bgColor[1], $this->bgColor[2]);
     $gridColor = imagecolorallocate($img, $this->gridColor[0], $this->gridColor[1], $this->gridColor[2]);
     $symbolColor = imagecolorallocate($img, $this->symbolColor[0], $this->symbolColor[1], $this->symbolColor[2]);
     //clear image with background color
     imagefilledrectangle($img, 0, 0, $this->width, $this->height, $bgColor);
     //if there is a background picture, set it
     if (!empty($this->bgPicture)) {
         $bg = file_get_contents($this->bgPicture);
         if ($bg) {
             $bgPic = imagecreatefromstring($bg);
             if ($bgPic) {
                 imagecopy($img, $bgPic, 0, 0, 0, 0, $this->width, $this->height);
                 imagedestroy($bgPic);
             }
         }
     }
     //if there's a title, draw it
     if (!empty($this->title)) {
         if (is_int($this->fontTitle)) {
             $font_width = imagefontwidth($this->fontTitle);
             imagestring($img, $this->fontTitle, ($this->width - $font_width) / 2.0, $this->margin['top'], $this->title, $symbolColor);
         } else {
             $box = imagettfbboxextended($this->fontTitleSize, 0, $this->fontTitle, $this->title);
             $font_width = $box['width'];
             imagettftext($img, $this->fontTitleSize, 0, ($this->width - $font_width) / 2.0, $this->margin['top'] + $box['top'] - 4, $symbolColor, $this->fontTitle, $this->title);
         }
     }
     //draw the strings
     $xoffset = 0;
     for ($i = 0; $i < $nbStrings; $i++) {
         imageline($img, $this->margin['left'] + $xoffset, $totalTopMargin, $this->margin['left'] + $xoffset, $this->height - $this->margin['bottom'], $gridColor);
         $xoffset += $stringSpacing;
     }
     //draw the frets
     $yoffset = 0;
     for ($i = 0; $i <= $this->maxFrets; $i++) {
         if ($i == 0 && $this->startingFret == 0) {
             //if the starting fret is 0 then we draw the nut
             imagefilledrectangle($img, $this->margin['left'], $totalTopMargin + $yoffset, $this->width - $this->margin['right'], $totalTopMargin + $fretsSpacing * $this->nutSize, $gridColor);
         } else {
             imageline($img, $this->margin['left'], $totalTopMargin + $yoffset, $this->width - $this->margin['right'], $totalTopMargin + $yoffset, $gridColor);
         }
         $yoffset += $fretsSpacing;
     }
     //if the starting fret isn't 0 add a mention with a 2 pixel margin
     if ($this->startingFret != 0) {
         $str = sprintf('%dfr.', $this->startingFret);
         if (is_int($this->fontStartingFret)) {
             $font_height = imagefontheight($this->fontStartingFret);
             imagestring($img, $this->fontStartingFret, $this->width - $this->margin['right'] + 2, $totalTopMargin - $font_height / 2.0, $str, $symbolColor);
         } else {
             $box = imagettfbboxextended($this->fontStartingFretSize, 0, $this->fontStartingFret, $str);
             imagettftext($img, $this->fontStartingFretSize, 0, $this->width - $this->margin['right'] + 2, $totalTopMargin - $box['height'] / 2.0 + $box['top'], $symbolColor, $this->fontStartingFret, $str);
         }
     }
     //draw chord
     $xoffset = 0;
     for ($i = 0; $i < $nbStrings; $i++) {
         if ($this->data[$i] === 'x') {
             if ($this->showUnstrummedStrings) {
                 imagecross($img, $this->margin['left'] + $xoffset - $upperSymbolSize / 2.0, $this->margin['top'] + $titleMargin - 1, $this->margin['left'] + $xoffset + $upperSymbolSize / 2.0, $this->margin['top'] + $titleMargin + $upperSymbolSize, $symbolColor);
             }
         } else {
             $stringPos = intval($this->data[$i]);
             if ($stringPos) {
                 $yoffset = $fretsSpacing / 2 + $fretsSpacing * ($stringPos - 1 - $this->startingFret);
                 //imagefilledellipse($img, $this->margin['left']+$xoffset, $this->margin['top'] + $yoffset, $fretsSpacing*$this->circleSize, $fretsSpacing*$this->circleSize, $gridColor);
                 imagesmoothcircle($img, $this->margin['left'] + $xoffset, $totalTopMargin + $yoffset, intval(round($fretsSpacing * $this->circleSize / 2.0)), array('R' => $this->symbolColor[0], 'G' => $this->symbolColor[1], 'B' => $this->symbolColor[2]));
             } else {
                 if ($this->showZeros) {
                     imageellipse($img, $this->margin['left'] + $xoffset, $this->margin['top'] + $upperSymbolSize / 2.0 - 1 + $titleMargin, $upperSymbolSize, $upperSymbolSize, $symbolColor);
                 }
             }
         }
         $xoffset += $stringSpacing;
     }
     //draw barre chords
     if (count($this->barre)) {
         $x1 = 0;
         $x2 = 0;
         $yoffset = 0;
         foreach ($this->barre as $fret => $bar) {
             $yoffset = $fretsSpacing / 2 + $fretsSpacing * ($fret - 1 - $this->startingFret);
             $x1 = ($bar[0] - 1) * $stringSpacing;
             $x2 = ($bar[1] - 1) * $stringSpacing;
             imagesmoothcircle($img, $this->margin['left'] + $x1, $totalTopMargin + $yoffset, intval(round($fretsSpacing * $this->barreSize / 2.0)), array('R' => $this->symbolColor[0], 'G' => $this->symbolColor[1], 'B' => $this->symbolColor[2]));
             imagesmoothcircle($img, $this->margin['left'] + $x2, $totalTopMargin + $yoffset, intval(round($fretsSpacing * $this->barreSize / 2.0)), array('R' => $this->symbolColor[0], 'G' => $this->symbolColor[1], 'B' => $this->symbolColor[2]));
             imagefilledrectangle($img, $this->margin['left'] + $x1, $totalTopMargin + $yoffset - intval(round($fretsSpacing * $this->barreSize / 2.0)), $this->margin['left'] + $x2, $totalTopMargin + $yoffset + intval(round($fretsSpacing * $this->barreSize / 2.0)), $symbolColor);
         }
     }
     $fname = str_replace('/', '_', $fname);
     $fullfname = base_path() . '/public/images/chords/' . $fname . '.png';
     imagepng($img, $fullfname);
     imagedestroy($img);
 }