php 在php中将文本转换为图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4488549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:11:44  来源:igfitidea点击:

convert text to image in php

php

提问by ruhit

I would like to style a text string taken from a form field and then convert it to a transparent .PNG (alpha BG).

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

Is this possible with PHP? If so, would you kindly show me how this would be achieved

这可以用 PHP 实现吗?如果是这样,你能告诉我这是如何实现的吗

回答by Sourabh Shankar

yes, its very much possible, you are going to follow the same technique as we do while generating a captcha image.

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

Requirement: GD library should be enabled in your php.

要求: GD 库应该在你的 php.ini 中启用。

Code (taken from php help file ;)

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

回答by T.Todua

convert text to image(php code):

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

at first, you need to ensure, that the hosting has enabled GD library (in a new php file, execute phpinfo();and in output see/find if GD library is enabled) .

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

Solution 1 (auto-sized output):

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

TextToImage_my( $text='Helloooo! my unicode words:  ? ? ?  ? ?  ? ');
    // ==== other parameters can be used too, see the function arguments below

function code: text-to-image.php

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



Solution 2 (manual-sized output):

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

(needs to have manual width&height of output, longer strings are cut out)..

(需要手动输出宽度和高度,切出较长的字符串)..

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

回答by Janiek Buysrogge

I think you can achieve this using the PHP GD library: http://php.net/manual/en/ref.image.php

我认为您可以使用 PHP GD 库实现此目的:http: //php.net/manual/en/ref.image.php

回答by UberMouse

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

You could then display the image by going I think thats correct, been awhile since I've done this.

然后你可以显示图像,我认为这是正确的,自从我这样做以来已经有一段时间了。