Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   1  <?php
   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/>.
  16  
  17  require_once('../../../../config.php');
  18  
  19  $id = required_param('id', PARAM_INT);
  20  
  21  $PAGE->set_url('/mod/feedback/item/captcha/print_captcha.php', array('id'=>$id));
  22  
  23  if ($id) {
  24      if (! $cm = get_coursemodule_from_id('feedback', $id)) {
  25          throw new \moodle_exception('invalidcoursemodule');
  26      }
  27  
  28      if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
  29          throw new \moodle_exception('coursemisconf');
  30      }
  31  
  32      if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
  33          throw new \moodle_exception('invalidcoursemodule');
  34      }
  35  }
  36  
  37  if (!isset($SESSION->feedback->item->captcha)) {
  38      throw new \moodle_exception('captchanotset', 'feedback');
  39  }
  40  
  41  $height = 40;
  42  $charcount = $SESSION->feedback->item->captcha->charcount;
  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);
  52  $factor_x = intval($width * $scale);
  53  $factor_y = intval($height * $scale);
  54  
  55  //I split the colors in three ranges
  56  //given are the max-min-values
  57  $colors = array(array(0, 40), array(50, 200), array(210, 255));
  58  list($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();
  67  for ($i = 0; $i < $charcount; $i++) {
  68      $nums[] = rand(0, 9); //Ziffern von 0-
  69  }
  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();
  74  for ($x = 0; $x < $factor_x; $x++) {
  75      for ($y = 0; $y < $factor_y; $y++) {
  76          $propobj = new stdClass();
  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;
  83      }
  84  }
  85  shuffle($properties);
  86  
  87  // create a blank image
  88  $image = imagecreatetruecolor($width, $height);
  89  $bg = imagecolorallocate($image, 0, 0, 0);
  90  for ($i = 0; $i < ($factor_x * $factor_y); $i++) {
  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 = '';
  99  for ($i = 0; $i < $charcount; $i++) {
 100      $colnum = rand(1, 2);
 101      $textcol = new stdClass();
 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;
 110      imagettftext($image, 30, $angle_text, $left_text, 35, $color_text, $fontfile, $text);
 111  }
 112  
 113  $SESSION->feedback->item->captcha->checkchar = $checkchar;
 114  
 115  // output the picture
 116  header("Content-type: image/png");
 117  imagepng($image);
 118  
 119  function 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  }