imageloadfont()
se utiliza para cargar mapas de bits definidos por el usuario. Si solo quiere usar Arial o cualquier otra fuente TrueType (.ttf) o fuentes OpenType (.otf) (la compatibilidad con esta última en GD lib es defectuosa), entonces lo que necesita es imagettftext()
. Antes de usar imagettftext()
y escribir texto en su imagen, primero debe saber si le quedará. Para saber esto, solo necesita llamar al imagettfbbox()
y pasarle el tamaño de fuente, el ángulo del texto (0 para texto horizontal), la ruta a su archivo de fuente .ttf o .otf y la cadena de texto misma y devolverá un array con 8 elementos que representan cuatro puntos que forman el cuadro delimitador del texto (consulte el manual de PHP para obtener detalles). A continuación, puede hacer referencia a esos elementos de la matriz y realizar cálculos para conocer el ancho y la altura que ocupará esa cadena de texto en particular. A continuación, puede usar esos valores para crear una imagen con un ancho y alto específico que permita que el texto se muestre en su totalidad.
Aquí es un simple script que logra lo que usted está tratando de hacer para empezar:
<?php # Script 1
/*
* This page creates a simple image.
* The image makes use of a TrueType font.
*/
// Establish image factors:
$text = 'Sample text';
$font_size = 12; // Font size is in pixels.
$font_file = 'Arial.ttf'; // This is the path to your font file.
// Retrieve bounding box:
$type_space = imagettfbbox($font_size, 0, $font_file, $text);
// Determine image width and height, 10 pixels are added for 5 pixels padding:
$image_width = abs($type_space[4] - $type_space[0]) + 10;
$image_height = abs($type_space[5] - $type_space[1]) + 10;
// Create image:
$image = imagecreatetruecolor($image_width, $image_height);
// Allocate text and background colors (RGB format):
$text_color = imagecolorallocate($image, 255, 255, 255);
$bg_color = imagecolorallocate($image, 0, 0, 0);
// Fill image:
imagefill($image, 0, 0, $bg_color);
// Fix starting x and y coordinates for the text:
$x = 5; // Padding of 5 pixels.
$y = $image_height - 5; // So that the text is vertically centered.
// Add TrueType text to image:
imagettftext($image, $font_size, 0, $x, $y, $text_color, $font_file, $text);
// Generate and send image to browser:
header('Content-type: image/png');
imagepng($image);
// Destroy image in memory to free-up resources:
imagedestroy($image);
?>
Cambio de valores en consecuencia para adaptarse a sus necesidades. No olvides leer el manual de PHP.
Tome una mira [imagettfbbox] (http://www.php.net/manual/en/function.imagettfbbox.php) y [imagettftext] (http://php.net/manual/en/function.imagettftext.php). – Vatev