Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/feedback/item/captcha/print_captcha.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 require_once('../../../../config.php');
00018 
00019 $id = required_param('id', PARAM_INT);
00020 
00021 $PAGE->set_url('/mod/feedback/item/captcha/print_captcha.php', array('id'=>$id));
00022 
00023 if ($id) {
00024     if (! $cm = get_coursemodule_from_id('feedback', $id)) {
00025         print_error('invalidcoursemodule');
00026     }
00027 
00028     if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
00029         print_error('coursemisconf');
00030     }
00031 
00032     if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
00033         print_error('invalidcoursemodule');
00034     }
00035 }
00036 
00037 if (!isset($SESSION->feedback->item->captcha)) {
00038     print_error('captchanotset', 'feedback');
00039 }
00040 
00041 $height = 40;
00042 $charcount = $SESSION->feedback->item->captcha->charcount;
00043 $fontfile = $CFG->libdir.'/default.ttf';
00044 
00045 $ttfbox = imagettfbbox ( 30, 0, $fontfile, 'H' );//the text to measure
00046 $charwidth = $ttfbox[2];
00047 
00048 $width = $charcount * $charwidth;
00049 
00050 $scale = 0.3;
00051 $elipsesize = intval((($width + $height)/2) / 5);
00052 $factor_x = intval($width * $scale);
00053 $factor_y = intval($height * $scale);
00054 
00055 //I split the colors in three ranges
00056 //given are the max-min-values
00057 $colors = array(array(0, 40), array(50, 200), array(210, 255));
00058 list($col_text1, $col_el, $col_text2) = $colors;
00059 
00060 //if the text is in color_1 so the elipses can be in color_2 or color_3
00061 //if the text is in color_2 so the elipses can be in color_1 or color_3
00062 //and so on.
00063 $textcolnum = rand(1, 3);
00064 
00065 //create the numbers to print out
00066 $nums = array();
00067 for ($i = 0; $i < $charcount; $i++) {
00068     $nums[] = rand(0, 9); //Ziffern von 0-
00069 }
00070 
00071 //to draw enough elipses so I draw 0.2 * width and 0.2 * height
00072 //we need th colors for that
00073 $properties = array();
00074 for ($x = 0; $x < $factor_x; $x++) {
00075     for ($y = 0; $y < $factor_y; $y++) {
00076         $propobj = new stdClass();
00077         $propobj->x = intval($x / $scale);
00078         $propobj->y = intval($y / $scale);
00079         $propobj->red = get_random_color($col_el[0], $col_el[1]);
00080         $propobj->green = get_random_color($col_el[0], $col_el[1]);
00081         $propobj->blue = get_random_color($col_el[0], $col_el[1]);
00082         $properties[] = $propobj;
00083     }
00084 }
00085 shuffle($properties);
00086 
00087 // create a blank image
00088 $image = imagecreatetruecolor($width, $height);
00089 $bg = imagecolorallocate($image, 0, 0, 0);
00090 for ($i = 0; $i < ($factor_x * $factor_y); $i++) {
00091     $propobj = $properties[$i];
00092     // choose a color for the ellipse
00093     $col_ellipse = imagecolorallocate($image, $propobj->red, $propobj->green, $propobj->blue);
00094     // draw the white ellipse
00095     imagefilledellipse($image, $propobj->x, $propobj->y, $elipsesize, $elipsesize, $col_ellipse);
00096 }
00097 
00098 $checkchar = '';
00099 for ($i = 0; $i < $charcount; $i++) {
00100     $colnum = rand(1, 2);
00101     $textcol = new stdClass();
00102     $textcol->red = get_random_color(${'col_text'.$colnum}[0], ${'col_text'.$colnum}[1]);
00103     $textcol->green = get_random_color(${'col_text'.$colnum}[0], ${'col_text'.$colnum}[1]);
00104     $textcol->blue = get_random_color(${'col_text'.$colnum}[0], ${'col_text'.$colnum}[1]);
00105     $color_text = imagecolorallocate($image, $textcol->red, $textcol->green, $textcol->blue);
00106     $angle_text = rand(-20, 20);
00107     $left_text = $i * $charwidth;
00108     $text = $nums[$i];
00109     $checkchar .= $text;
00110     imagettftext($image, 30, $angle_text, $left_text, 35, $color_text, $fontfile, $text);
00111 }
00112 
00113 $SESSION->feedback->item->captcha->checkchar = $checkchar;
00114 
00115 // output the picture
00116 header("Content-type: image/png");
00117 imagepng($image);
00118 
00119 function get_random_color($val1 = 0, $val2 = 255) {
00120     $min = $val1 < $val2 ? $val1 : $val2;
00121     $max = $val1 > $val2 ? $val1 : $val2;
00122 
00123     return rand($min, $max);
00124 }
 All Data Structures Namespaces Files Functions Variables Enumerations