在php中将文本转换为图像

在php中将文本转换为图像,php,Php,我想设置一个从表单字段中提取的文本字符串的样式,然后将其转换为透明的.PNG(alpha-BG) 这在PHP中是可能的吗?如果是这样,请告诉我如何实现这一点。我认为您可以使用PHP GD库实现这一点:是的,非常有可能,您将遵循与生成验证码图像相同的技术 要求:应该在php中启用GD库 代码(取自php帮助文件;) 然后,您可以通过以下方式显示图像: 我认为这是正确的,我已经做了一段时间了。将文本转换为图像(php代码): 首先,您需要确保宿主已启用GD库(在新的php文件中,执行phpinf

我想设置一个从表单字段中提取的文本字符串的样式,然后将其转换为透明的.PNG(alpha-BG)


这在PHP中是可能的吗?如果是这样,请告诉我如何实现这一点。

我认为您可以使用PHP GD库实现这一点:

是的,非常有可能,您将遵循与生成验证码图像相同的技术

要求:应该在php中启用GD库

代码(取自php帮助文件;)


然后,您可以通过以下方式显示图像:
我认为这是正确的,我已经做了一段时间了。

将文本转换为图像(php代码):

首先,您需要确保宿主已启用GD库(在新的php文件中,执行
phpinfo();
并在输出中查看/查找GD库是否已启用)

解决方案1(自动调整大小输出): 功能代码:


解决方案2(手动大小的输出): (需要手动输出宽度和高度,切出更长的字符串)



您可以尝试以下方法:感谢自动调整大小的解决方案:)一个问题。。。有可能在输入文本中换行吗?解决方案1太棒了!为什么不发表评论?
<?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);
TextToImage_my( $text='Helloooo! my unicode words:  ǩ Ƥ Ў  ض ط  Ⴓ ');
    // ==== other parameters can be used too, see the function arguments below
<?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 );
?>