Reputation: 1367
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
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);
Upvotes: 19
Views: 56014
Reputation: 2972
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);
Upvotes: 2
Reputation: 11
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():
//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 = 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:
//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);
Upvotes: 0
Reputation: 1
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.
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!
Upvotes: 0
Reputation: 47667
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);
?>
Upvotes: 26
Reputation: 7644
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
Upvotes: 1