function view_image($id = null)
 {
     if (is_numeric($id)) {
         $question = $this->Question->findById($id);
         $line_count = 0;
         // array of lines
         $question_text = array();
         // Other kinds of newlines causing problems.
         $question['Question']['question'] = str_replace(array("\r", "\r\n", "\n"), '\\n', $question['Question']['question']);
         $question_wrapped = wordwrap(strip_tags(QH_urldecode($question['Question']['question'])), $this->number_of_characters, '\\n', true);
         $question_text = array_merge($question_text, explode('\\n', $question_wrapped));
         $line_count += count($question_text);
         $answers = Set::combine($question['Answer'], '{n}.order', '{n}.answer');
         $answer_text = array();
         foreach ($answers as $answer) {
             $answer_wrapped = wordwrap(strip_tags($answer), $this->number_of_characters - 4, '\\n', true);
             $line_array = explode('\\n', $answer_wrapped);
             $answer_text[] = $line_array;
             $line_count += count($line_array);
         }
         $box_height = $this->padding * 2 + $this->line_height * $line_count;
         $box_width = $this->padding * 2 + $this->number_of_characters * $this->character_width;
         $image = imagecreatetruecolor($box_width, $box_height);
         $background = imagecolorallocate($image, 255, 255, 255);
         imagefill($image, 0, 0, $background);
         $black = imagecolorallocate($image, 0, 0, 0);
         imagerectangle($image, 0, 0, $box_width - 1, $box_height - 1, $black);
         $y = $this->padding + $this->character_height;
         foreach ($question_text as $line) {
             imagettftext($image, $this->font_size, 0, $this->padding, $y, $black, APP . 'Lib/unifont_5.1.20080907.ttf', html_entity_decode($line));
             // h_e_d for preventing   in the image... sometimes. Not clear on that, but
             //   this fixed #206.
             $y += $this->line_height;
         }
         foreach ($answer_text as $order => $answer) {
             if ($order == $question['Question']['correct_answer']) {
                 imagefilledellipse($image, $this->character_width * 3, $y - $this->character_height / 2, 8, 8, $black);
             } else {
                 imageellipse($image, $this->character_width * 3, $y - $this->character_height / 2, 8, 8, $black);
             }
             foreach ($answer as $answer_line) {
                 $answer_line = '    ' . $answer_line;
                 imagettftext($image, $this->font_size, 0, $this->padding, $y, $black, APP . 'Lib/unifont_5.1.20080907.ttf', $answer_line);
                 $y += $this->line_height;
             }
         }
         $this->layout = 'image';
         header("Content-type: image/png");
         imagepng($image);
         imagedestroy($image);
         $this->autoRender = false;
     }
 }
 function view_text_box_image($type = 'one-line', $text = '')
 {
     $this->layout = 'image';
     $text = QH_urldecode($text);
     if ($type == 'one-line' || $type == 'multi-line') {
         $string = ucfirst($type);
         if (!empty($text)) {
             $string .= ' free response: ' . $text;
         }
     } else {
         $string = 'Image creation error!';
     }
     $string = wordwrap(strip_tags($string), $this->number_of_characters, '\\n', true);
     $text = explode('\\n', $string);
     $number_of_lines = count($text);
     $box_height = $this->padding + $this->line_height * $number_of_lines;
     $box_width = $this->padding * 2 + $this->number_of_characters * $this->character_width;
     $image = imagecreatetruecolor($box_width, $box_height);
     $black = imagecolorallocate($image, 0, 0, 0);
     $white = imagecolorallocate($image, 255, 255, 255);
     imagefill($image, 0, 0, $white);
     imagerectangle($image, 0, 0, $box_width - 1, $box_height - 1, $black);
     $y = $this->padding + $this->character_height;
     foreach ($text as $line) {
         imagettftext($image, $this->font_size, 0, $this->padding, $y, $black, APP . 'Lib/unifont_5.1.20080907.ttf', $line);
         $y += $this->line_height;
     }
     header("Content-type: image/png");
     imagepng($image);
     imagedestroy($image);
     $this->autoRender = false;
 }
 protected function _generateTextBoxHTML($matches)
 {
     $uuid = $this->uuid();
     $type = htmlentities($matches[1], ENT_QUOTES, 'UTF-8');
     $matches[2] = QH_urldecode(strip_tags($matches[2], allowed_tags('strip_tags')));
     $matches[3] = QH_urldecode(strip_tags($matches[3], allowed_tags('strip_tags')));
     $prompt = htmlentities($matches[2], ENT_QUOTES, 'UTF-8');
     $placeholder = htmlentities($matches[3], ENT_QUOTES, 'UTF-8');
     $name = "free-response[{$uuid}][{$prompt}]";
     if ('one-line' == $type) {
         return "<label for='{$name}'>{$prompt}<br /><input placeholder='{$placeholder}' class='text-box' name='{$name}' /></label>";
     } elseif ('multi-line' == $type) {
         return "<label for='{$name}'>{$prompt}<br /><textarea placeholder='{$placeholder}' " . "class='text-box' name='{$name}'></textarea></label>";
     }
 }
 protected function _parseTextBoxes($step_content)
 {
     $text_box_pattern = '/\\<img[^>]*class\\="text-box"[^>]*src\\="tutorials\\/view_text_box_image\\/([^\\/]+)\\/([^"]+)"[^>]*>/';
     $step_content = QH_urldecode(preg_replace_callback($text_box_pattern, array($this, '_generateTextBoxHTML'), $step_content));
     return $step_content;
 }