Hola de nuevo a todos (hola
GatorV)
Estoy intentando ver si consigo mi propósito por medio de PHP (y como bien me has dicho antes, montar un array con las medidas de cada letra para poder calcular el ancho de cada palabra y (mas o menos) ajustarlo al ancho del que dispongo).
Me he encontrado un problema, el cual no se si es por la Fuente que estoy suando o por el
Content-type de mi documento...
Como cabecera para generar la imagen al vuelo estoy usando
Código PHP:
header("Content-type: image/png");
Pero si en mi texto hay algún caracter acentuado, me aparecen elementos
extraños...
Os pongo mi código PHP que genera la imagen y mi código PHP de llamada para la generación de la imagen...
GENERADOR DE LA IMAGEN
Código PHP:
<?php
// Establecer el tipo de contenido
header("Content-type: image/png");
/********************************************************************************
INICIALIZACIÓN DE VARIABLES
******************************************************************************* */
// ***** WIDTH *************************************
$theWidth = 400;
if (isset($_GET["WIDTH"]))
{
$theWidth = $_GET["WIDTH"];
}
// ***** HEIGHT *************************************
$theHeight = 30;
if (isset($_GET["HEIGHT"]))
{
$theHeight = $_GET["HEIGHT"];
}
// ***** COLOR *************************************
$theColor = "FF0000";
if (isset($_GET["COLOR"]))
{
$theColor = $_GET["COLOR"];
}
sscanf($theColor, "%2x%2x%2x", $R, $G, $B);
// ***** TEXT *************************************
$theText = "Probando...";
if (isset($_GET["TEXT"]))
{
$theText = $_GET["TEXT"];
}
// ***** FONT *************************************
$theFont = "fonts/arial.ttf";
if (isset($_GET["FONT"]))
{
$theFont = $_GET["FONT"];
// Si la fuente no está ubicada en una carpeta, ponemos la carpeta por omisión de fuentes dentro de PHP/
if (!eregi("fonts/", $theFont))
{
$theFont = "fonts/".$theFont;
if (!file_exists($theFont))
{
GLOBAL_message("No se encuentra el Archivo ".$theFont, "error", "Generate Image");
}
}
}
// ***** FONT SIZE ***********************************
$theFontSize = 20;
if (isset($_GET["FONT_SIZE"]))
{
$theFontSize = $_GET["FONT_SIZE"];
}
// ***** ANGLE (0 to 360 grados) *********************
$theAngle = 0;
if (isset($_GET["ANGLE"]))
{
$theAngle = $_GET["ANGLE"];
}
// ***** LEFT POSITION *******************************
$theLeft = 10;
if (isset($_GET["LEFT"]))
{
$theLeft = $_GET["LEFT"];
}
// ***** TOP POSITION ********************************
$theTop = 20;
if (isset($_GET["TOP"]))
{
$theTop = $_GET["TOP"];
}
/* **************************************************************************** */
/*
Vamos a recorer el texto, letra a letra para ir calculando lo que ocupan las palabras.
Si nos pasamos del ancho máximo haremos saltos de línea
A su vez, iremos calculando el alto que nos hace falta de imagen TOTAL (dependiendo de la cantidad de líneas, y de la altura de línea de la fuente
*/
include_once("image_fonts_config.php");
/* **************************************************************************** */
// Crear la imagen
$im = imagecreatetruecolor($theWidth, $theHeight);
// Generar transparecia al fondo de la imagen
imagesavealpha($im, true);
// Ponemos el color de Fondo BLANCO
$FFFFFF_background = imagecolorallocate($im, 255, 255, 255);
// Aplicamos un filtro de relleno en la imagen de Fondo con el color recién creado
imagefill($im, 0, 0, $FFFFFF_background);
// Crear algunos colores
$theColor = imagecolorallocate($im, $R, $G, $B);
// Agregar el texto
imagettftext($im, $theFontSize, $theAngle, $theLeft, $theTop, $theColor, $theFont, $theText);
// Usar imagepng() resulta en texto más claro, en comparación con imagejpeg()
imagepng($im);
imagedestroy($im);
?>
LLAMADA A LA GENERACIÓN Código PHP:
<?
function serializeParameters ($array_parameters)
{
$cont_parameters = 0;
$string_params = "?";
foreach ($array_parameters as $key_param => $value_param)
{
$string_params.= $key_param."=".$value_param;
if ($cont_parameters < count($array_parameters) - 1)
{
$string_params.= "&";
}
}
return($string_params);
}
$image_parameters = array (
"WIDTH" => "400",
"HEIGHT" => "30",
"COLOR" => "A953A9",
"TEXT" => urlencode("á"),
"FONT" => "MyriadPro-Regular.otf",
"FONT_SIZE" => 26,
"ANGLE" => 0,
"LEFT" => 0,
"TOP" => 25
);
?>
<img src="image-fonts/generar_imagen.php<?= serializeParameters($image_parameters); ?>">
Si alguien puede o quiere ayudarme, le estaré muy agradecido...
Gracias de antemano.
Javier