无法更改GD imagestring()的字体大小

时间:2012-06-08 19:03:51

标签: php image

我正在尝试关注 this example 以生成带有动态文字的图片。

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

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

这是an example出现的小小:(

这是我的示例代码 -

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

5 个答案:

答案 0 :(得分:15)

你不能放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);
?>

答案 1 :(得分:2)

有人喜欢在盒子外面思考吗?是的我也是。

因此,如果您必须使用PHP imagestring而不是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 );

1: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);

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

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

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

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

1: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);

1:4。打印出来或保存:

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

1: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); 

答案 2 :(得分:1)

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

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

答案 3 :(得分: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);
?>

这对我有用!

答案 4 :(得分:0)

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

//设置图像宽度和高度     $ 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 = 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);