php-将文本与图像匹配

php-将文本与图像匹配,php,image,text,Php,Image,Text,我有这个密码 <?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

我有这个密码

<?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... a very long text';
// 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);
?> 
但我不知道如何在这里应用它。 有什么帮助吗?

请尝试以下代码:

<?php
// Set the content-type
header('Content-type: image/png');

$img_width = 400;
$img_height = 30;

// Create the image
$im = imagecreatetruecolor($img_width, $img_height);

// 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, $img_width, $img_height, $white);

// The text to draw
$text = 'Testing... a very long text bla.. ';
// Replace path by your own font path
$font = 'arial.ttf';

$fontSize = $img_width / strlen($text) * 1.8;

// Add some shadow to the text
imagettftext($im, $fontSize, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, $fontSize, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

新的更新代码:

<?php
// Set the content-type
header('Content-type: image/png');

$img_width = 400;
$img_height = 30;
$font = 'arial.ttf';    
// Create the image
$text = 'Testing... a very long text.. Testing... a very long text.. Testing... a very long text.. Testing... a very long text..';
$curTextLen = strlen($text);
$limit = 35;
$totalLine = ceil($curTextLen / $limit);
$img_height = $img_height * $totalLine;

$im = imagecreatetruecolor($img_width, $img_height);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, $img_width, $img_height, $white);

for($i = 1; $i <= $totalLine; $i++){
    $y = $i * 27;
    $textN = substr($text,($limit * ($i-1)),$limit);
    imagettftext($im, 20, 0, 11, $y, $grey, $font, $textN);
    // Add the text
    imagettftext($im, 20, 0, 10, $y, $black, $font, $textN);
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

尝试以下代码:

<?php
// Set the content-type
header('Content-type: image/png');

$img_width = 400;
$img_height = 30;

// Create the image
$im = imagecreatetruecolor($img_width, $img_height);

// 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, $img_width, $img_height, $white);

// The text to draw
$text = 'Testing... a very long text bla.. ';
// Replace path by your own font path
$font = 'arial.ttf';

$fontSize = $img_width / strlen($text) * 1.8;

// Add some shadow to the text
imagettftext($im, $fontSize, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, $fontSize, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

新的更新代码:

<?php
// Set the content-type
header('Content-type: image/png');

$img_width = 400;
$img_height = 30;
$font = 'arial.ttf';    
// Create the image
$text = 'Testing... a very long text.. Testing... a very long text.. Testing... a very long text.. Testing... a very long text..';
$curTextLen = strlen($text);
$limit = 35;
$totalLine = ceil($curTextLen / $limit);
$img_height = $img_height * $totalLine;

$im = imagecreatetruecolor($img_width, $img_height);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, $img_width, $img_height, $white);

for($i = 1; $i <= $totalLine; $i++){
    $y = $i * 27;
    $textN = substr($text,($limit * ($i-1)),$limit);
    imagettftext($im, 20, 0, 11, $y, $grey, $font, $textN);
    // Add the text
    imagettftext($im, 20, 0, 10, $y, $black, $font, $textN);
}
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>


可能这将帮助您可能这将帮助您只在一行中显示文本。我想利用图像的所有空间:)哈哈哈。。这几乎是不可能的,但也许我上面的新更新代码可以为您提供解决方案。我想利用图像的所有空间:)哈哈哈。。这几乎是不可能的,但也许我上面的新更新代码可以为您提供解决方案。