php 无法更改 GD imagestring() 的字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10954652/
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
Can't change font size for GD imagestring()
提问by Pow
I'm trying to follow this example to generate an image with dynamic text.
我正在尝试按照此示例生成带有动态文本的图像。
I wanted to change the size of the font, I put even 100 instead of 4, but it still appears same as before.
我想改变字体的大小,我什至把 4 改为 100,但它仍然看起来和以前一样。
I'm not very good at PHP. Any sort of help would be appreciated.
我不是很擅长 PHP。任何形式的帮助将不胜感激。
Here's an examplehow small it appears :(
Here's my example code -
这是我的示例代码 -
$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);
Thanks
谢谢
ADDED LATER
稍后添加
Ok now here it is what I have done but as a result, no text is visible in the callout box.
好的,现在这是我所做的,但结果是,标注框中看不到任何文本。
$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);
回答by Zoltan Toth
You can't put 100 - http://php.net/manual/en/function.imagestring.php
你不能放 100 - http://php.net/manual/en/function.imagestring.php
Only 1-5 (by default)
只有 1-5 个(默认)
UPDATE
更新
To be able fully control the font size you might want to use http://php.net/manual/en/function.imagettftext.php
为了能够完全控制字体大小,您可能需要使用http://php.net/manual/en/function.imagettftext.php
Example (from the same site):
示例(来自同一站点):
<?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 Harry B
Anybody like thinking outside the box? Yeah me too.
有人喜欢跳出框框思考吗?我也是。
So if you have to use PHP's imagestringrather than imagettftextthere is an way to create text sizes larger than the standard 1-5 range, and it requires you to create the text as size A and then resize the image you've created the text on to be larger. How large depends on how big you want the text.
因此,如果您必须使用 PHPimagestring而不是imagettftext有一种方法可以创建大于标准 1-5 范围的文本大小,并且它需要您将文本创建为大小 A,然后将您创建文本的图像调整为更大。多大取决于你想要多大的文本。
So let's walk through it...
那么让我们来看看它......
:1. Create blank pngs just to put our text onto. We also need a final image to compile things to. These could use imagecreatetruecolorfor transparent backgrounds.
:1。创建空白 png 只是为了将我们的文本放在上面。我们还需要一个最终的图像来编译。这些可以imagecreatetruecolor用于透明背景。
$ImageText1Small = imagecreate( 148, 16 );
$ImageText1Large = imagecreate( 148, 16 );
$ImageText2Small = imagecreate( 308, 40 );
$ImageText2Large = imagecreate( 308, 40 );
$ImageFinal = imagecreate( 500, 100 );
:2. Sort the background and text colour for our text images. Black and white. How original.
: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. We need text. Add it.
:3。我们需要文字。添加它。
imagestring( $ImageText1Small, 1, 1, 0, 'Stack Overflow', $textColor1 );
imagestring( $ImageText2Small, 5, 1, 0, 'Harry Harry Harry', $textColor2 );
:4. This is the clever bit. Resize the smaller text images to make them larger than the max 5 font.
: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. Here I do some rotation, but obviously that's optional.
:5。在这里我做了一些轮换,但显然这是可选的。
$ImageText1Large = imagerotate ( $ImageText1Large, 20, $backgroundColor1 );
$ImageText2Large = imagerotate ( $ImageText2Large, -5, $backgroundColor2 );
:6. Get dimensions of our newly rotated images. Again this is optional if you rotate.
:6。获取我们新旋转的图像的尺寸。如果您旋转,这也是可选的。
$ImageText1W = imagesx($ImageText1Large);
$ImageText1H = imagesy($ImageText1Large);
$ImageText2W = imagesx($ImageText2Large);
$ImageText2H = imagesy($ImageText2Large);
:7. Stick the text image layers on to the final image:
:7。将文本图像层粘贴到最终图像上:
imagecopymerge($ImageFinal, $ImageText1Large, 350, 20, 0, 0, $ImageText1W, $ImageText1H, 100);
imagecopymerge($ImageFinal, $ImageText2Large, 20, 20, 0, 0, $ImageText2W, $ImageText2H, 100);
:4. Print it out, or save it:
:4。打印出来或保存:
header( 'Content-type: image/png' );
imagepng($ImageFinal, 0);
:5. Clean up after ourselves:
:5。在我们自己之后清理:
imagecolordeallocate( $ImageText1Small, $textColor1 );
imagecolordeallocate( $ImageText1Small, $backgroundColor1 );
imagecolordeallocate( $ImageText1Large, $textColor2 );
imagecolordeallocate( $ImageText1Large, $backgroundColor2 );
imagedestroy($ImageText1);
imagedestroy($ImageText2);
imagedestroy($ImageFinal);
Obviously you can play around with:
* Starting image size
* Starting font (1-5)
* Rotation
* Scaling up further
* Background colours
* Transparent backgrounds
* Positioning
* imagepngcompression level
显然你可以玩: * 起始图像大小 * 起始字体 (1-5) * 旋转 * 进一步放大 * 背景颜色 * 透明背景 * 定位 *imagepng压缩级别
The whole script, imperfect, but functional is here:
整个脚本,不完善,但功能在这里:
$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);
回答by Artefact2
I suggest you use imagettftext if you want to write bigger text in any font of your choosing.
如果您想用您选择的任何字体编写更大的文本,我建议您使用 imagettftext。
回答by Hartger Visser
Many suggestions, but no solutions...
很多建议,但没有解决方案......
Here's a working solution. The trick is to use imagettftext()for this. It lets you choose any truetype font in any size.
这是一个有效的解决方案。诀窍是为此使用imagettftext()。它允许您选择任何大小的任何 truetype 字体。
On an existing image use the following code:
在现有图像上使用以下代码:
<?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);
?>
This works for me!
这对我有用!
回答by user2689252
Changing the font style of a sting can be done either with imagettftext() function or imageloadfont() . With both you can change the font style but with imageloadfont() you are just loading well defined binary font file, and in this case you have no control over the exact size of the font, can't customize it. You can NOT load also true type font files they should be GDF Files. Using the imagettftext() function you have full control over the font style, size, font-family etc. in this case you should use true type font files directly by allocating the file on your computer/server. Here are 2 examples with both functions: Example with captcha functionality using imageloadfont():
可以使用 imagettftext() 函数或 imageloadfont() 来更改字符串的字体样式。两者都可以更改字体样式,但使用 imageloadfont() 您只是加载定义良好的二进制字体文件,在这种情况下,您无法控制字体的确切大小,也无法对其进行自定义。您也不能加载真正的字体文件,它们应该是 GDF 文件。使用 imagettftext() 函数,您可以完全控制字体样式、大小、字体系列等。在这种情况下,您应该通过在计算机/服务器上分配文件来直接使用真字体文件。以下是两个具有这两个功能的示例: 使用 imageloadfont() 的验证码功能示例:
//Set the image width and height $width = 150; $height = 30;
//设置图片宽高 $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);
HERE IS EXAMPLE WITH imagettftext() FUNCTION:
这是带有 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);

