You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.6 KiB

9 years ago
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Creates an image dynamically.
*
* @package mod_game
* @copyright 2007 Vasilis Daloukas
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require( '../../../config.php');
require_login();
9 years ago
$id = required_param('id', PARAM_INT); // Course Module ID.
$attemptid = required_param('id2', PARAM_INT); // Course Module ID.
9 years ago
$foundcells = required_param('f', PARAM_SEQUENCE); // CSV.
$cells = required_param('cells', PARAM_SEQUENCE); // CSV.
$filehash = required_param('p', PARAM_PATH);
$cols = required_param('cols', PARAM_INT);
$rows = required_param('rows', PARAM_INT);
9 years ago
$filenamenumbers = required_param('n', PARAM_PATH); // Path to numbers picture.
create_image( $id, $attemptid, $foundcells, $cells, $filehash, $cols, $rows, $filenamenumbers);
/**
* Create an image.
*
* @param int $id
* @param int $attemptid
* @param boolean $foundcells
* @param stdClass $cells
* @param string $filehash
* @param int $cols
* @param int $rows
* @param string $filenamenumbers
*/
9 years ago
function create_image( $id, $attemptid, $foundcells, $cells, $filehash, $cols, $rows, $filenamenumbers) {
global $CFG;
9 years ago
$a = explode( ',', $foundcells);
$found = array();
9 years ago
foreach ($a as $s) {
$found[ $s] = 1;
}
9 years ago
$a = explode( ',', $cells);
$cells = array();
9 years ago
foreach ($a as $s) {
$cells[ $s] = 1;
}
$file = get_file_storage()->get_file_by_hash( $filehash);
$image = $file->get_imageinfo();
9 years ago
if ($image === false) {
die("Aknown filehash $filehash");
return false;
}
9 years ago
$imghandle = imagecreatefromstring($file->get_content());
$mime = $image[ 'mimetype'];
9 years ago
$imgnumbers = imagecreatefrompng( $filenamenumbers);
$sizenumbers = getimagesize ($filenamenumbers);
header("Content-type: $mime");
$color = imagecolorallocate( $imghandle, 100, 100, 100);
$width = $image[ 'width'];
$height = $image[ 'height'];
$pos = 0;
9 years ago
$font = 1;
9 years ago
for ($y = 0; $y < $rows; $y++) {
for ($x = 0; $x < $cols; $x++) {
$pos++;
9 years ago
if (!array_key_exists( $pos, $found)) {
$x1 = $x * $width / $cols;
$y1 = $y * $height / $rows;
9 years ago
imagefilledrectangle( $imghandle, $x1, $y1, $x1 + $width / $cols, $y1 + $height / $rows, $color);
if (array_key_exists( $pos, $cells)) {
shownumber( $imghandle, $imgnumbers, $pos, $x1 , $y1, $width / $cols, $height / $rows, $sizenumbers);
}
}
}
}
9 years ago
switch ($mime) {
case 'image/png':
imagepng ($imghandle);
break;
case 'image/jpeg':
imagejpeg ($imghandle);
break;
case 'image/gif':
imagegif ($imghandle);
break;
default:
die('Aknown mime type $mime');
return false;
}
9 years ago
imagedestroy ($imghandle);
}
/**
* Show number.
*
* @param object $imghandle
* @param object $imgnumbers
* @param int $number
* @param int $x1
* @param int $y1
* @param int $width
* @param int $height
* @param int $sizenumbers
*/
9 years ago
function shownumber( $imghandle, $imgnumbers, $number, $x1 , $y1, $width, $height, $sizenumbers) {
if ($number < 10) {
$widthnumber = $sizenumbers[ 0] / 10;
$dstx = $x1 + $width / 3;
$dsty = $y1 + $height / 3;
$srcx = $number * $sizenumbers[ 0] / 10;
$srcw = $sizenumbers[ 0] / 10;
$srch = $sizenumbers[ 1];
$dstw = $width / 10;
$dsth = $dstw * $srch / $srcw;
9 years ago
imagecopyresized( $imghandle, $imgnumbers, $dstx, $dsty, $srcx, 0, $dstw, $dsth, $srcw, $srch);
9 years ago
} else {
$number1 = floor( $number / 10);
$number2 = $number % 10;
9 years ago
shownumber( $imghandle, $imgnumbers, $number1, $x1 - $width / 20, $y1, $width, $height, $sizenumbers);
shownumber( $imghandle, $imgnumbers, $number2, $x1 + $width / 20, $y1, $width, $height, $sizenumbers);
}
}