Título: Convertir texto en imagen con PHP
Publicado por: <housedir> en 13 Septiembre 2013, 09:40 am
hola a todos, quisiera saber como convertir texto en imagen con php, tengo un codigo y funciona bien: <?php if(!isset($_GET['txt'])) { exit(); } header ("Content-type: image/png"); $string = $_GET['txt']; $font = 4; $width = ImageFontWidth($font) * strlen($string); $height = ImageFontHeight($font); $im = @imagecreate ($width,$height); $background_color = imagecolorallocate ($im, 255, 255, 255); //white background $text_color = imagecolorallocate ($im, 0, 0,0);//black text imagestring ($im, $font, 0, 0, $string, $text_color); imagepng ($im); ?>
El problema es que este codigo no hace saltos de linea, quisiera por ejemplo que al enviar un texto tome de limite de ancho 600px y al toparse con ese limite haga un salto de linea y asi en todo el texto
Título: Re: Convertir texto en imagen con PHP
Publicado por: Ragnarok en 1 Octubre 2013, 19:37 pm
Las librerías estándar no hacen eso, o encuentras una librería que lo haga o lo calculas a mano (te será más fácil con una fuente de ancho fijo).
Título: Re: Convertir texto en imagen con PHP
Publicado por: miloscorp en 25 Octubre 2013, 00:12 am
Hola housedir podrias probar algo asi, lo escribi sobre la marcha y no lo probe asi que seguro habra algo que corregir, despues si tengo tiempo lo pruebo. Cualquier duda me avisas, y con unos pequeños retoques podrias hasta hacer que el texto quede centrado. <?php if(!isset($_GET['txt'])) { exit(); } header ("Content-type: image/png"); $string = $_GET['txt']; $font = 4; $ancho_caracteres = ImageFontWidth($font); $alto_caracteres = ImageFontHeight($font); $width = 600; $cant_caracteres = strlen($string); $cant_caracteres_linea = $width / ancho_caracteres; //Cantidad de caracteres que entran en una linea segun el ancho de la imagen //Ahora que tenemos la cantidad de caracteres que entran a lo ancho podemos calcular la cantidad de lineas $cant_lineas = strlen($string) / $cant_caracteres; //Esto nos devuelve la cantidad de lineas $espacio = 2;//Espacio entre lineas $height = (ImageFontHeight($font) + $espacio) * $cant_lineas; $im = @imagecreate ($width,$height); $background_color = imagecolorallocate ($im, 255, 255, 255); //white background $text_color = imagecolorallocate ($im, 0, 0,0);//black text
$i = 0; $j = 0; $tmp = ""; $y = 0; $x = 0;
Do{ Do{ $tmp .= substr($string,$i,1); $i++; if($j == $cant_caracteres_linea or ($cant_caracteres - $i) < $cant_caracteres_linea) { imagestring ($im, $font, $x += $ancho_caracteres, $y, $string, $text_color); $y += ImageFontHeight($font); $j = 0; $tmp = ""; if (($cant_caracteres - $i) < $cant_caracteres_linea){ break; } }else{ $j++; } }while ($j < $cant_caracteres_linea); }while ($i < $cant_caracteres); imagepng ($im); ?>
|