1<?php
2/**
3 * RRDGraph Plugin: Error image generator
4 *
5 * @author Daniel Goß <developer@flashsystems.de>
6 * @license MIT
7 */
8
9if (! defined('DOKU_INC')) die();
10
11/**
12 * Static class to create an image containing an error message.
13 */
14class ErrorImage {
15    /** Size of the title line. */
16    const TITLE_FONT = 3;
17    /** Size of the second text line. */
18    const FONT = 2;
19    /** Spacing between line 1 and 2 in pixels. */
20    const LINE_SPACING = 2;
21    /** Width of the colored border around the message. */
22    const BORDER_WIDTH = 2;
23    /** Spacing between border and text. */
24    const BORDER_SPACING = 10;
25
26    /**
27     * Create and output an image containing the given error message.
28     * The output is directly sento to the webbrowser. It is equiped with cache inhibition headers.
29     * The image size will be automatically determined in a way that all text is visible.
30     *
31     * @param String $title Title of the error message. This is rendered with a bold typeface on the first row of the output image.
32     * @param String $message Error message. This is rendered with a normal typeface on the second row of the output image.
33     */
34    public static function outputErrorImage($title, $message) {
35        $messageWidth = imagefontwidth(self::FONT) * strlen($message) + self::BORDER_SPACING * 2 + self::BORDER_WIDTH * 2;
36        $titleWidth = imagefontwidth(self::TITLE_FONT) * strlen($title) + self::BORDER_SPACING * 2 + self::BORDER_WIDTH * 2;
37
38        $width = max($messageWidth, $titleWidth);
39        $height = imagefontheight(self::TITLE_FONT) + imagefontheight(self::FONT) + self::BORDER_SPACING * 2 + self::BORDER_WIDTH * 2;
40
41        $image = imagecreatetruecolor($width, $height);
42        $cBackground = imagecolorallocate($image, 255, 255, 255);
43        $cBorder = imagecolorallocate($image, 255, 0, 0);
44        $cBlack = imagecolorallocate($image, 0, 0, 0);
45
46        imagefill($image, 0, 0, $cBorder);
47        imagefilledrectangle($image, self::BORDER_WIDTH, self::BORDER_WIDTH, $width - self::BORDER_WIDTH - 1, $height - self::BORDER_WIDTH - 1, $cBackground);
48
49        $y = self::BORDER_WIDTH + self::BORDER_SPACING;
50        imagestring($image, self::TITLE_FONT, self::BORDER_WIDTH + self::BORDER_SPACING, $y, $title, $cBlack);
51        $y += imagefontheight(self::FONT) + self::LINE_SPACING;
52        imagestring($image, self::FONT, self::BORDER_WIDTH + self::BORDER_SPACING, $y, $message, $cBlack);
53
54        //-- Suppress caching
55        header("Content-Type: image/png");
56        header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
57        header("Cache-Control: post-check=0, pre-check=0", false);
58        header("Pragma: no-cache");
59
60        //-- Output the image. Create a valid Content-Length-Header via output buffering.
61        ob_start();
62        imagepng($image);
63        header("Content-Length: " . ob_get_length());
64        ob_end_flush();
65
66        imagedestroy($image);
67    }
68}