Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Load My Custom Font in imagestring()?

Tags:

php

fonts

gd

My problem is Q title.

I tried http://www.php.net/manual/en/function.imageloadfont.php and: imageloadfont();

but i do not see any change and i get error:

Warning: imageloadfont(): gd warning: product of memory allocation multiplication would exceed INT_MAX, failing operation gracefully in E:\xampp\htdocs\test\texts\text01.php on line 3

Warning: imageloadfont(): Error reading font, invalid font header in E:\xampp\htdocs\test\texts\text01.php on line 3

Edit: My font.

like image 282
Nastary Avatar asked Oct 08 '14 12:10

Nastary


People also ask

How do you add a font family in HTML?

To change font type purely with HTML, use the CSS font-family property. Set it to the value you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a paragraph, heading, button, or span tag.

Which function is used to write a string into an image?

imagestring() is an inbuilt function in PHP that is used to draw a string horizontally.


1 Answers

To quote myself, you should be using imagettftext(), not imagestring().

Example usage, abbreviated/adapted from the manual page:

$im = imagecreatetruecolor(400, 30);

// Create some colors and set background to white.
$white = imagecolorallocate($im, 255, 255, 255);
$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 = 'btitr.ttf';

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im, 'path_to_file.png');
imagedestroy($im);
like image 107
timclutton Avatar answered Sep 25 '22 17:09

timclutton