Merge branch 'MDL-27675_master' of git://github.com/grabs/moodle
[moodle.git] / mod / feedback / item / captcha / print_captcha.php
CommitLineData
c70ad9f7 1<?php
9c5bc7a5
AG
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
c70ad9f7 16
84973212 17require_once('../../../../config.php');
18
19$id = required_param('id', PARAM_INT);
20
a6855934 21$PAGE->set_url('/mod/feedback/item/captcha/print_captcha.php', array('id'=>$id));
84973212 22
23if ($id) {
24 if (! $cm = get_coursemodule_from_id('feedback', $id)) {
25 print_error('invalidcoursemodule');
c70ad9f7 26 }
84973212 27
28 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
29 print_error('coursemisconf');
c70ad9f7 30 }
31
84973212 32 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
33 print_error('invalidcoursemodule');
c70ad9f7 34 }
84973212 35}
36
9c5bc7a5 37if (!isset($SESSION->feedback->item->captcha)) {
84973212 38 print_error('captchanotset', 'feedback');
39}
40
41$height = 40;
42$charcount = $SESSION->feedback->item->captcha->charcount;
84973212 43$fontfile = $CFG->libdir.'/default.ttf';
44
45$ttfbox = imagettfbbox ( 30, 0, $fontfile, 'H' );//the text to measure
46$charwidth = $ttfbox[2];
47
48$width = $charcount * $charwidth;
49
50$scale = 0.3;
51$elipsesize = intval((($width + $height)/2) / 5);
9c5bc7a5
AG
52$factor_x = intval($width * $scale);
53$factor_y = intval($height * $scale);
84973212 54
55//I split the colors in three ranges
56//given are the max-min-values
9c5bc7a5 57$colors = array(array(0, 40), array(50, 200), array(210, 255));
84973212 58list($col_text1, $col_el, $col_text2) = $colors;
59
60//if the text is in color_1 so the elipses can be in color_2 or color_3
61//if the text is in color_2 so the elipses can be in color_1 or color_3
62//and so on.
63$textcolnum = rand(1, 3);
64
65//create the numbers to print out
66$nums = array();
9c5bc7a5
AG
67for ($i = 0; $i < $charcount; $i++) {
68 $nums[] = rand(0, 9); //Ziffern von 0-
84973212 69}
84973212 70
71//to draw enough elipses so I draw 0.2 * width and 0.2 * height
72//we need th colors for that
73$properties = array();
9c5bc7a5
AG
74for ($x = 0; $x < $factor_x; $x++) {
75 for ($y = 0; $y < $factor_y; $y++) {
39790bd8 76 $propobj = new stdClass();
84973212 77 $propobj->x = intval($x / $scale);
78 $propobj->y = intval($y / $scale);
79 $propobj->red = get_random_color($col_el[0], $col_el[1]);
80 $propobj->green = get_random_color($col_el[0], $col_el[1]);
81 $propobj->blue = get_random_color($col_el[0], $col_el[1]);
82 $properties[] = $propobj;
c70ad9f7 83 }
84973212 84}
85shuffle($properties);
86
87// create a blank image
88$image = imagecreatetruecolor($width, $height);
89$bg = imagecolorallocate($image, 0, 0, 0);
9c5bc7a5 90for ($i = 0; $i < ($factor_x * $factor_y); $i++) {
84973212 91 $propobj = $properties[$i];
92 // choose a color for the ellipse
93 $col_ellipse = imagecolorallocate($image, $propobj->red, $propobj->green, $propobj->blue);
94 // draw the white ellipse
95 imagefilledellipse($image, $propobj->x, $propobj->y, $elipsesize, $elipsesize, $col_ellipse);
96}
97
98$checkchar = '';
9c5bc7a5
AG
99for ($i = 0; $i < $charcount; $i++) {
100 $colnum = rand(1, 2);
39790bd8 101 $textcol = new stdClass();
84973212 102 $textcol->red = get_random_color(${'col_text'.$colnum}[0], ${'col_text'.$colnum}[1]);
103 $textcol->green = get_random_color(${'col_text'.$colnum}[0], ${'col_text'.$colnum}[1]);
104 $textcol->blue = get_random_color(${'col_text'.$colnum}[0], ${'col_text'.$colnum}[1]);
105 $color_text = imagecolorallocate($image, $textcol->red, $textcol->green, $textcol->blue);
106 $angle_text = rand(-20, 20);
107 $left_text = $i * $charwidth;
108 $text = $nums[$i];
109 $checkchar .= $text;
9c5bc7a5 110 imagettftext($image, 30, $angle_text, $left_text, 35, $color_text, $fontfile, $text);
84973212 111}
112
113$SESSION->feedback->item->captcha->checkchar = $checkchar;
114
115// output the picture
116header("Content-type: image/png");
117imagepng($image);
118
119function get_random_color($val1 = 0, $val2 = 255) {
120 $min = $val1 < $val2 ? $val1 : $val2;
121 $max = $val1 > $val2 ? $val1 : $val2;
122
123 return rand($min, $max);
124}