(PHP 4, PHP 5, PHP 7, PHP 8)
imageloadfont — Carga una nueva fuente
imageloadfont() carga una nueva fuente de usuario y devuelve su identificador.
filenameEl formato de las fuentes depende actualmente del sistema operativo. Esto significa que es necesario generar archivos de fuentes para la máquina que ejecuta PHP.
| Posición | Tipo de datos C | Descripción |
|---|---|---|
| Octetos 0-3 | int | Número de caracteres de la fuente |
| Octetos 4-7 | int | Valor del primer carácter de la fuente (generalmente 32 para espacio) |
| Octetos 8-11 | int | Ancho en píxeles de los caracteres |
| Octetos 12-15 | int | Altura en píxeles de los caracteres |
| Octetos 16- | char | Tabla con los datos de los caracteres, un octeto por píxel para cada carácter, con un total de (número de caracteres * ancho * altura) octetos. |
| Versión | Descripción |
|---|---|
| 8.1.0 | Ahora devuelve una instancia de GdFont ; anteriormente se devolvía un int . |
Ejemplo #1 Ejemplo con imageloadfont()
<?php
// Creación de una nueva imagen
$im = imagecreatetruecolor(50, 20);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
// Define el fondo en blanco
imagefilledrectangle($im, 0, 0, 49, 19, $white);
// Carga la fuente GD y escribe '¡Hola!'
$font = imageloadfont('./04b.gdf');
imagestring($im, $font, 0, 0, '¡Hola!', $black);
// Muestra en el navegador
header('Content-type: image/png');
imagepng($im);
?>Working under the assumption that the only 'architecture dependant' part of the font files is endianness, I wrote a quick and dirty Python script to convert between the two. It has only been tested on a single font on a single machine so don't bet your life on it working. All it does is swap the byte order of the first four ints.
#!/usr/bin/env python
f = open("myfont.gdf", "rb");
d = open("myconvertedfont.gdf", "wb");
for i in xrange(4):
b = [f.read(1) for j in xrange(4)];
b.reverse();
d.write(''.join(b));
d.write(f.read());
I successfully used this script to convert anonymous.gdf, from one of the font links below, into something useable on Mac OS X.Confirmation code generation for preventing automated registrations on a website.
Function arguments are:
$code - the code that you shall random generate
$location - relative location of the image that shall be generated
$fonts_dir - relative location for the GDF fonts directory
This function will create an image with the code given by you and will save it in the directory specified with the name formed by MD5 hash of the code.
You may insert as many font types in the fonts directory as you wish, with random names.
<?php
function generate_image($code, $location, $fonts_dir)
{
$image = imagecreate(150, 60);
imagecolorallocate($image, rand(0, 100), rand(100, 150), rand(150, 250));
$fonts = scandir($fonts_dir);
$max = count($fonts) - 2;
$width = 10;
for ($i = 0; $i <= strlen($code); $i++)
{
$textcolor = imagecolorallocate($image, 255, 255, 255);
$rand = rand(2, $max);
$font = imageloadfont($fonts_dir."/".$fonts[$rand]);
$fh = imagefontheight($font);
$fw = imagefontwidth($font);
imagechar($image, $font, $width, rand(10, 50 - $fh), $code[$i], $textcolor);
$width = $width + $fw;
}
imagejpeg($image, $location."/".md5($code).".jpg", 100);
imagedestroy($image);
return $code;
}
?>Remember - GD fonts aren't antialiased. If you're planning on using a pre-existing (TrueType) font, you may want to consider using imagettftext() instead of phillip's function.