Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change font size for GD imagestring()

Tags:

php

image

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);
like image 869
Pow Avatar asked Jun 08 '12 19:06

Pow


3 Answers

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);
?>
like image 86
Zoltan Toth Avatar answered Nov 12 '22 22:11

Zoltan Toth


Anybody like thinking outside the box? Yeah me too.

So if you have to use PHP's imagestring rather than imagettftext there 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.

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 imagecreatetruecolor for transparent backgrounds.

$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.

$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.

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.

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.

$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.

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

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

:7. Stick the text image layers on to the final image:

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:

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

:5. Clean up after ourselves:

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 * imagepng compression level

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); 
like image 42
Harry B Avatar answered Nov 12 '22 22:11

Harry B


I suggest you use imagettftext if you want to write bigger text in any font of your choosing.

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

like image 1
Artefact2 Avatar answered Nov 12 '22 21:11

Artefact2