将文字转换为图片?

时间:2009-09-20 07:17:32

标签: php image-processing

在一些情况下,我看到网站根据文本/数据输入生成图像。如何用PHP实现?

5 个答案:

答案 0 :(得分:5)

我相信libGD是最常用的生成图像的替代方法之一(并且它对Web开发中使用的大多数语言都有绑定)。

请参阅PHP.net上的文档。我想你对imagettftext特别感兴趣。

答案 1 :(得分:4)

另一种选择:尝试imagick

答案 2 :(得分:3)

工作解决方案:

(首先,您需要确保托管已启用GD库:在任何php文件中,执行phpinfo();并在输出中检查是否启用了GD库。

解决方案1(自动调整大小的输出):

TextToImage_my( $text='Helloooo! my unicode words:  ǩ Ƥ Ў  ض ط  Ⴓ ');
    // ==== other parameters can be used too, see the function arguments below

功能代码:text-to-image.php


解决方案2(手动大小的输出):

(需要手动宽度和输出高度,切出更长的琴弦)..

<?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 );
?> 

答案 3 :(得分:2)

使用gd或其他此类库(或基于gd构建的库)。

答案 4 :(得分:1)

PHP GD扩展允许文本覆盖在图像上。

事实上,您首先不需要图像,您可以生成仅包含文本的图像。

我用它来做按钮。

相关问题