【问题标题】:Convert text to image?将文本转换为图像?
【发布时间】:2010-11-29 20:19:13
【问题描述】:

我曾在几个案例中看到网站根据文本/数据输入生成图像。用 PHP 是如何实现的?

【问题讨论】:

    标签: php image-processing


    【解决方案1】:

    工作解决方案:

    (首先,您需要确保主机已启用 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 );
    ?> 
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        另一种选择:试试imagick

        【讨论】:

          【解决方案4】:

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

          其实你一开始并不需要图片,你可以生成只包含文字的图片。

          我用它来做按钮。

          【讨论】:

            【解决方案5】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-09-18
              • 2019-08-06
              • 2017-10-09
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多