Me gustaría diseñar una cadena de texto tomada desde un campo de formulario y luego convertirla a .PNG transparente (alfa BG).convertir texto a imagen en php
¿Es esto posible con PHP? Si es así, sería tan amable de mostrar cómo se conseguiría esto
Me gustaría diseñar una cadena de texto tomada desde un campo de formulario y luego convertirla a .PNG transparente (alfa BG).convertir texto a imagen en php
¿Es esto posible con PHP? Si es así, sería tan amable de mostrar cómo se conseguiría esto
Creo que se puede lograr esto usando la librería GD de PHP: http://php.net/manual/en/ref.image.php
por qué no en su comentario? – NaveenDA
sí, es muy posible, que va a seguir la misma técnica que se lo hacemos al generar una imagen de captcha.
Requisito: la biblioteca de GD debe estar habilitada en su php.
Código (tomado del archivo de ayuda php;)
<?php
// Set the content-type
header('Content-type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
$image = imagecreate(widthyouwant,heightyouwant);
$bg = imagecolorallocatealpha($image,255,255,255,0);
$black = imagecolorallocate($image,255,255,255);
imagettftext($image,fontsize, 0, x, y, $black, "fontlocation", $_GET['text']);
header('Content-Type: image/png');
imagepng($image);
A continuación, puede mostrar la imagen yendo creo que eso es correcto, pasado un tiempo desde que he hecho esto.
convertir texto a la imagen (código php):
en un primer momento, es necesario asegurarse de que el alojamiento ha permitido librería GD (en un nuevo archivo PHP, ejecute phpinfo();
y en la producción de ver/encontrar si la biblioteca GD está habilitada).
TextToImage_my($text='Helloooo! my unicode words: ǩ Ƥ Ў ض ط Ⴓ ');
// ==== other parameters can be used too, see the function arguments below
función:
function TextToImage_my(
$text,
$separate_line_after_chars=40,
$font='./myfont.ttf',
$size=24,
$rotate=0,
$padding=2,
$transparent=true,
$color=array('red'=>0,'grn'=>0,'blu'=>0),
$bg_color=array('red'=>255,'grn'=>255,'blu'=>255)
){
$amount_of_lines= ceil(strlen($text)/$separate_line_after_chars);
$x=explode("\n", $text); $final='';
foreach($x as $key=>$value){
$returnes='';
do{ $first_part=mb_substr($value, 0, $separate_line_after_chars, 'utf-8');
$value= "\n".mb_substr($value, $separate_line_after_chars, null, 'utf-8');
$returnes .=$first_part;
} while(mb_strlen($value,'utf-8')>$separate_line_after_chars);
$final .= $returnes."\n";
}
$text=$final;
Header("Content-type: image/png");
$width=$height=$offset_x=$offset_y = 0;
if(!is_file($font)) { file_put_contents($font,file_get_contents('https://github.com/edx/edx-certificates/raw/master/template_data/fonts/Arial%20Unicode.ttf')); }
// get the font height.
$bounds = ImageTTFBBox($size, $rotate, $font, "W");
if ($rotate < 0) {$font_height = abs($bounds[7]-$bounds[1]); }
elseif ($rotate > 0) {$font_height = abs($bounds[1]-$bounds[7]); }
else { $font_height = abs($bounds[7]-$bounds[1]);}
// determine bounding box.
$bounds = ImageTTFBBox($size, $rotate, $font, $text);
if ($rotate < 0){ $width = abs($bounds[4]-$bounds[0]); $height = abs($bounds[3]-$bounds[7]);
$offset_y = $font_height; $offset_x = 0;
}
elseif ($rotate > 0) { $width = abs($bounds[2]-$bounds[6]); $height = abs($bounds[1]-$bounds[5]);
$offset_y = abs($bounds[7]-$bounds[5])+$font_height; $offset_x = abs($bounds[0]-$bounds[6]);
}
else{ $width = abs($bounds[4]-$bounds[6]); $height = abs($bounds[7]-$bounds[1]);
$offset_y = $font_height; $offset_x = 0;
}
$image = imagecreate($width+($padding*2)+1,$height+($padding*2)+1);
$background = ImageColorAllocate($image, $bg_color['red'], $bg_color['grn'], $bg_color['blu']);
$foreground = ImageColorAllocate($image, $color['red'], $color['grn'], $color['blu']);
if ($transparent) ImageColorTransparent($image, $background);
ImageInterlace($image, true);
// render the image
ImageTTFText($image, $size, $rotate, $offset_x+$padding, $offset_y+$padding, $foreground, $font, $text);
imagealphablending($image, true);
imagesavealpha($image, true);
// output PNG object.
imagePNG($image);
}
//======helper function==========
if(!function_exists('mb_substr_replace')){
function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = "UTF-8") {
if (extension_loaded('mbstring') === true){
$string_length = (is_null($encoding) === true) ? mb_strlen($string) : mb_strlen($string, $encoding);
if ($start < 0) { $start = max(0, $string_length + $start); }
else if ($start > $string_length) {$start = $string_length; }
if ($length < 0){ $length = max(0, $string_length - $start + $length); }
else if ((is_null($length) === true) || ($length > $string_length)) { $length = $string_length; }
if (($start + $length) > $string_length){$length = $string_length - $start;}
if (is_null($encoding) === true) { return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length); }
return mb_substr($string, 0, $start, $encoding) . $replacement . mb_substr($string, $start + $length, $string_length - $start - $length, $encoding);
}
return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length);
}
}
(muy simple, pero debe establecer el ancho & altura de salida - más larga cuerdas se cortan) ..
<?php
$text = "YOUR texttttttttttttttt";
$my_img = imagecreate(200, 80); //width & height
$background = imagecolorallocate($my_img, 0, 0, 255);
$text_colour = imagecolorallocate($my_img, 255, 255, 0);
$line_colour = imagecolorallocate($my_img, 128, 255, 0);
imagestring($my_img, 4, 30, 25, $text, $text_colour);
imagesetthickness ($my_img, 5);
imageline($my_img, 30, 45, 165, 45, $line_colour);
header("Content-type: image/png");
imagepng($my_img);
imagecolordeallocate($line_color);
imagecolordeallocate($text_color);
imagecolordeallocate($background);
imagedestroy($my_img);
?>
Gracias por la solución de tamaño automático :) –
una pregunta ... ¿es posible dividir la línea en el texto de entrada? –
¡La solución 1 es brillante! – JohnA10
Puede probar esto: http://www.daftlogic.com/projects-text-to-image.htm –