18

我正在尝试按照此示例生成带有动态文本的图像。

我想改变字体的大小,我什至放了 100 而不是 4,但它仍然和以前一样。

我不是很擅长 PHP。任何形式的帮助将不胜感激。

这是一个看起来有多小的例子:(

这是我的示例代码 -

       $font = 'arial.ttf'; //FONT SIZE

       $width = imagefontwidth($font) * strlen($string) ;
       $height = imagefontheight($font) ;
       $im = imagecreatefrompng($imageO);

       $x = imagesx($im) / 2;   //PLACEMENT CENTERISH – X
       $y = imagesy($im) / 2;   //PLACEMENT CENTERISH – Y

      // $backgroundColor = imagecolorallocate ($im, 255, 255, 255);

       $transparency = 25;
       imagesavealpha($im, true);
       $background = imagecolorallocatealpha($im, background_r, background_g, background_b, $transparency);

       $textColor = imagecolorallocate ($im, 0,0,0);
       imagestring ($im, $font, $x, $y, $string, $textColor);
       imagepng($im,$imageN[$k]);
       $w = imagesx($im);
       $h = imagesy($im);

谢谢

稍后添加

好的,现在这就是我所做的,但结果,标注框中没有文本可见。

       $font = 'arial.ttf'; //YOUR FONT SIZE

       $im = imagecreatefrompng($imageO);
       $string = "My Text";
       $imageN ="NewImage.png";

       $transparency = 25;
       imagesavealpha($im, true);
       $background = imagecolorallocatealpha($im, background_r, background_g, background_b, $transparency);

       $textColor = imagecolorallocate ($im, 0,0,0);
       //imagestring ($im, 5, $x, $y, $string, $textColor);
       imagettftext($im, 36, 0, 10, 20, $textColor, $font, $string);
       imagepng($im,$imageN);
4

5 回答 5

21

你不能放 100 - http://php.net/manual/en/function.imagestring.php

只有 1-5 个(默认)

更新

为了能够完全控制您可能想要使用的字体大小http://php.net/manual/en/function.imagettftext.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);
?>
于 2012-06-08T19:06:32.910 回答
2

有人喜欢跳出框框思考吗?我也是。

因此,如果您必须使用 PHPimagestring而不是imagettftext有一种方法可以创建大于标准 1-5 范围的文本大小,并且它需要您将文本创建为大小 A,然后将您创建文本的图像调整为变大。多大取决于你想要的文本有多大。

所以让我们来看看它......

:1。创建空白 png 只是为了将我们的文本放到上面。我们还需要一个最终的图像来编译东西。这些可以imagecreatetruecolor用于透明背景。

$ImageText1Small = imagecreate( 148, 16 );
$ImageText1Large = imagecreate( 148, 16 );
$ImageText2Small = imagecreate( 308, 40 );
$ImageText2Large = imagecreate( 308, 40 );
$ImageFinal = imagecreate( 500, 100 );

:2。对我们的文本图像的背景和文本颜色进行排序。黑与白。多么原始。

$backgroundColor1 = imagecolorallocate($ImageText1Small, 255,255,255);
$textColor1 = imagecolorallocate($ImageText1Small, 0,0,0);

$backgroundColor2 = imagecolorallocate($ImageText2Small, 255,255,255);
$textColor2 = imagecolorallocate($ImageText2Small, 0,0,0);

:3。我们需要文字。添加它。

imagestring( $ImageText1Small, 1, 1, 0, 'Stack Overflow',  $textColor1 );
imagestring( $ImageText2Small, 5, 1, 0, 'Harry Harry Harry',  $textColor2 );

:4。这是聪明的一点。调整较小的文本图像,使其大于最大 5 字体。

imagecopyresampled($ImageText1Large, $ImageText1Small, 0, 0, 0, 0, 148, 16, 74, 8);
imagecopyresampled($ImageText2Large, $ImageText2Small, 0, 0, 0, 0, 308, 40, 154, 20);

:5。在这里我做了一些旋转,但显然这是可选的。

$ImageText1Large = imagerotate ( $ImageText1Large, 20, $backgroundColor1 );
$ImageText2Large = imagerotate ( $ImageText2Large, -5, $backgroundColor2 );

:6。获取我们新旋转图像的尺寸。如果您旋转,这也是可选的。

$ImageText1W = imagesx($ImageText1Large);
$ImageText1H = imagesy($ImageText1Large);

$ImageText2W = imagesx($ImageText2Large);
$ImageText2H = imagesy($ImageText2Large);

:7。将文本图像层粘贴到最终图像上:

imagecopymerge($ImageFinal, $ImageText1Large, 350, 20, 0, 0, $ImageText1W, $ImageText1H, 100);
imagecopymerge($ImageFinal, $ImageText2Large, 20, 20, 0, 0, $ImageText2W, $ImageText2H, 100);

:4。打印出来,或者保存:

header( 'Content-type: image/png' );
imagepng($ImageFinal, 0);

:5。自己清理:

imagecolordeallocate( $ImageText1Small, $textColor1 );
imagecolordeallocate( $ImageText1Small, $backgroundColor1 );
imagecolordeallocate( $ImageText1Large, $textColor2 );
imagecolordeallocate( $ImageText1Large, $backgroundColor2 );
imagedestroy($ImageText1); 
imagedestroy($ImageText2);
imagedestroy($ImageFinal);

显然你可以玩弄: * 起始图像大小 * 起始字体 (1-5) * 旋转 * 进一步放大 * 背景颜色 * 透明背景 * 定位 *imagepng压缩级别

整个脚本,不完美,但功能在这里:

$ImageText1Small = imagecreate( 148, 16 );
$ImageText1Large = imagecreate( 148, 16 );
$ImageText2Small = imagecreate( 308, 40 );
$ImageText2Large = imagecreate( 308, 40 );
$ImageFinal = imagecreate( 500, 100 );

$backgroundColor1 = imagecolorallocate($ImageText1Small, 255,255,255);
$textColor1 = imagecolorallocate($ImageText1Small, 0,0,0);

$backgroundColor2 = imagecolorallocate($ImageText2Small, 255,255,255);
$textColor2 = imagecolorallocate($ImageText2Small, 0,0,0);

imagestring( $ImageText1Small, 1, 1, 0, 'Stack Overflow',  $textColor1 );
imagestring( $ImageText2Small, 5, 1, 0, 'Harry Harry Harry',  $textColor2 );

imagecopyresampled($ImageText1Large, $ImageText1Small, 0, 0, 0, 0, 148, 16, 74, 8);
imagecopyresampled($ImageText2Large, $ImageText2Small, 0, 0, 0, 0, 308, 40, 154, 20);

$ImageText1Large = imagerotate ( $ImageText1Large, 20, $backgroundColor1 );
$ImageText2Large = imagerotate ( $ImageText2Large, -5, $backgroundColor2 );

$ImageText1W = imagesx($ImageText1Large);
$ImageText1H = imagesy($ImageText1Large);

$ImageText2W = imagesx($ImageText2Large);
$ImageText2H = imagesy($ImageText2Large);

imagecopymerge($ImageFinal, $ImageText1Large, 350, 20, 0, 0, $ImageText1W, $ImageText1H, 100);
imagecopymerge($ImageFinal, $ImageText2Large, 20, 20, 0, 0, $ImageText2W, $ImageText2H, 100);

header( "Content-type: image/png" );
imagepng($ImageFinal);

imagecolordeallocate( $ImageText1, $textColor1 );
imagecolordeallocate( $ImageText2, $textColor2 );
imagedestroy($ImageText1); 
imagedestroy($ImageText2); 
于 2018-04-01T18:30:22.543 回答
1

如果您想以您选择的任何字体编写更大的文本,我建议您使用 imagettftext。

http://php.net/manual/en/function.imagettftext.php

于 2012-06-08T19:10:51.320 回答
0

很多建议,但没有解决方案...

这是一个有效的解决方案。诀窍是为此使用imagettftext()。它使您可以选择任何大小的任何 truetype 字体。

在现有图像上使用以下代码:

<?php
  $txt = 'This is the text overlay';

  header('Content-type: image/png');

  $image = ImageCreateFromJPEG('existingimage.jpg'); // path to the existing image
  $color = imagecolorallocate($im, 0, 0, 0); // black
  $font = 'fontname.ttf'; // path to font
  imagettftext($image, 14, 0, 395, 85, $color, $font, $txt );

  imagepng($image); // png gets better font results than jpg
  imagedestroy($image);
?>

这对我有用!

于 2013-07-12T08:36:40.757 回答
0

可以使用 imagettftext() 函数或 imageloadfont() 更改字符串的字体样式。两者都可以更改字体样式,但使用 imageloadfont() 只是加载定义明确的二进制字体文件,在这种情况下,您无法控制字体的确切大小,无法自定义它。您也不能加载真正的字体文件,它们应该是 GDF 文件。使用 imagettftext() 函数,您可以完全控制字体样式、大小、字体系列等。在这种情况下,您应该通过在计算机/服务器上分配文件来直接使用真字体文件。以下是两个具有这两个功能的示例: 使用 imageloadfont() 的验证码功能示例:

//设置图片宽高 $width = 150; $高度 = 30;

//Create the image resource 
$image = ImageCreate($width, $height);  

//We are making three colors, white, black and gray 
$white = ImageColorAllocate($image, 255, 255, 255); 
$black = ImageColorAllocate($image, 46, 29, 29); 
$grey = ImageColorAllocate($image, 204, 204, 204); 

//Make the background black 
ImageFill($image, 0, 0, $black); 

$font = imageloadfont('04b.gdf');

$text='Testing'; 
//Add randomly generated string in white to the image
ImageString($image, $font, 20, 3, $text, $white);

ImageRectangle($image,0,0,$width-1,$height-1,$grey); 
imageline($image, 0, $height/3, $width, $height/3, $grey);
imageline($image, 0, ($height/3)*2, $width, ($height/3)*2, $grey);
imageline($image, $width/2, 0, $width/2, $height, $grey); 

//Tell the browser what kind of file is come in 
header("Content-Type: image/jpeg"); 

//Output the newly created image in jpeg format 
ImageJpeg($image); 

这是 imagettftext() 函数的示例:

//Set the image width and height 
$width = 150; 
$height = 30;

//Create the image resource 
$image = ImageCreate($width, $height);  

//We are making three colors, white, black and gray 
$white = ImageColorAllocate($image, 255, 255, 255); 
$black = ImageColorAllocate($image, 46, 29, 29); 
$grey = ImageColorAllocate($image, 204, 204, 204); 

//Make the background black 
ImageFill($image, 0, 0, $black); 

$font = 'arial.ttf';
$text='Testing';
imagettftext($image, 20, 5, 10, 20, $white, $font, $text);
ImageRectangle($image,0,0,$width-1,$height-1,$grey); 
imageline($image, 0, $height/3, $width, $height/3, $grey);
imageline($image, 0, ($height/3)*2, $width, ($height/3)*2, $grey);
imageline($image, $width/2, 0, $width/2, $height, $grey); 

//Tell the browser what kind of file is come in 
header("Content-Type: image/jpeg"); 

//Output the newly created image in jpeg format 
ImageJpeg($image); 
于 2013-08-16T12:28:08.063 回答