【问题标题】:convert text to image in php [duplicate]在php中将文本转换为图像[重复]
【发布时间】:2018-08-22 18:02:10
【问题描述】:

我想对取自表单域的文本字符串进行样式设置,然后将其转换为透明的 .PNG(alpha BG)。

这可以通过 PHP 实现吗?如果是这样,请您告诉我如何实现这一目标

【问题讨论】:

标签: php


【解决方案1】:

是的,很有可能,在生成验证码图像时,您将采用与我们相同的技术。

要求:您的 php 中应启用 GD 库。

代码(取自 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);
?> 

【讨论】:

    【解决方案2】:

    文字转图片(php代码):

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

    【讨论】:

    • 感谢自动调整大小的解决方案 :)
    • 一个问题...是否可以在输入文本中换行?
    • 解决方案 1 太棒了!
    【解决方案3】:

    我认为您可以使用 PHP GD 库来实现这一点:http://php.net/manual/en/ref.image.php

    【讨论】:

    • 为什么不在评论中?
    【解决方案4】:
    $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);
    

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

    【讨论】:

      猜你喜欢
      • 2012-05-12
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      相关资源
      最近更新 更多