1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63:
<?php
class ErrorImage
{
private $canvas;
public function __construct() {
$this->canvas = new\Imagick();
}
public function generate($headingText, $errorText) {
$draw = new ImagickDraw();
$draw->setFillColor('#777777');
$draw->setFontSize(15);
$draw->setFont('fonts/exo2bold.ttf');
$headingMetrics = $this->canvas->queryFontMetrics($draw, $headingText);
$draw->setFont('fonts/exo2regular.ttf');
$textMetrics = $this->canvas->queryFontMetrics($draw, $errorText);
$this->canvas->newImage(
max($textMetrics['textWidth'], $headingMetrics['textWidth']) + 6,
$textMetrics['textHeight'] + $headingMetrics['textHeight'] + 6,
new ImagickPixel('transparent'));
$this->canvas->annotateImage($draw, 3, $headingMetrics['textHeight'] * 2, 0, $errorText);
$draw->setFont('fonts/exo2bold.ttf');
$draw->setFillColor('#333333');
$draw->setGravity(Imagick::GRAVITY_NORTH);
$this->canvas->annotateImage($draw, 3, 3, 0, $headingText);
$this->canvas->setImageFormat('png');
header('Content-Type: image/'.$this->canvas->getImageFormat());
header("Cache-Control: max-age=60");
header("Expires: ".gmdate("D, d M Y H:i:s", time()+60)." GMT");
die($this->canvas);
}
}