Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

php - Can't change font size for GD imagestring()

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.

I'm not very good at PHP. Any sort of help would be appreciated.

Here's an example how 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't put 100 - http://php.net/manual/en/function.imagestring.php

Only 1-5 (by default)

UPDATE

To be able fully control the font size you might want to use 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);
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...