<?php
/**
* Returns with of a line.
* @param array $boudning_box Bounding box
* @return Width of the box
*/
function line_width($bounding_box) {
return abs($bounding_box[2] - $bounding_box[0]);
}
/**
* Returns height of a line.
* @param array $boudning_box Bounding box
* @return Height of the box
*/
function line_height($bounding_box) {
return abs($bounding_box[1] - $bounding_box[5]);
}
/**
* Writes a line of text
* @param resrouce $image PHP GD image
* @param int $size Font size
* @param int|string $x X-coordinate or 'LEFT', 'CENTER', 'RIGHT'
* @param int $y Y-coordinate
* @param int $color Color index
* @param string $font Path to the font
* @param string $text Text to write
* @return Bounding box
*/
function write_line($image, $size, $x, $y, $color, $font, $text) {
$bbox = imagettfbbox($size, 0.0, $font, $text);
$width = line_width($bbox);
$height = line_height($bbox);
if ($x == 'LEFT') {
$x = 0;
} else if ($x == 'CENTER') {
$x = (imagesx($image) - $width) / 2.0;
} else if ($x == 'RIGHT') {
$x = imagesx($image) - $width;
}
imagettftext($image, $size, 0.0, $x, $y + $height, $color, $font, $text);
return $bbox;
}
/**
* Writes multiples lines of text.
* @param resrouce $image PHP GD image
* @param int $size Font size
* @param int|string $x X-coordinate or 'LEFT', 'CENTER', 'RIGHT'
* @param int $y Y-coordinate
* @param int $color Color index
* @param string $font Path to the font
* @param array $text Array of lines to write
* @param int $padding Space between lines
*/
function write_lines($image, $size, $x, $y, $color, $font, $text, $padding = 0) {
foreach ($text as $line) {
$bbox = write_line($image, $size, $x, $y, $color, $font, $line);
$y += line_height($bbox) + $padding;
}
}
// Imposta lo header
header('Content-type: image/png');
// Legge i dati
$nome = $_POST['nome'];
$grado = $_POST['grado'];
$officiante = $_POST['officiante'];
$diocesi = $_POST['diofun'];
$arcidiocesi = $_POST['arcifun'];
$data = $_POST['datafun'];
// Legge l'immagine di sfondo ed imposta il colore del testo
$image = imagecreatefromjpeg("http://i.imgur.com/vOZBXEo.jpg");
$color = imagecolorallocate($image, 0x8B, 0x00, 0x00);
// Imposta le righe di testo da scrivere
$text = array(
"Il fedele dell'Altissimo chiamato",
$nome,
"dopo la prematura morte,",
"ha ricevuto il sacramento del funerale,",
"dal $grado$officiante",
"nella Parrocchia di $diocesi, Arcidiocesi di $arcidiocesi,",
"in data $data"
);
// Scrive il testo
write_lines($image, 14, 'CENTER', 350, $color, "http://www.princexml.com/fonts/larabie/kimberle.ttf", $text, 5);
// Mostra l'immagine
imagepng($image);