PHP - imagettftext not working and GD installed
Asked Answered
F

5

9

It's being long hours that I'm still looking for answer to this problem.. All the solutions I find are around catching the font name but I am pretty sure this isn't my problem.

It looks like GD is installed

array(11) {
  ["GD Version"]=>
  string(27) "bundled (**2.0.34 compatible**)"
  ["FreeType Support"]=>
  bool(false)
  ["T1Lib Support"]=>
  bool(false)
  ["GIF Read Support"]=>
  bool(true)
  ["GIF Create Support"]=>
  bool(true)
  ["JPEG Support"]=>
  bool(true)
  ["PNG Support"]=>
  bool(true)
  ["WBMP Support"]=>
  bool(true)
  ["XPM Support"]=>
  bool(true)
  ["XBM Support"]=>
  bool(true)
  ["JIS-mapped Japanese Font Support"]=>
  bool(false)
}

Above you can see my GD support. My PHP version is 5.3 and I'm running on Linux.

I have tried few different code examples from different websites and none works. ImageString does work for me but I need to get imagettftext to work..

This is the last code I have tried now-

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Set the content-type
header('Content-Type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 100) or die("Can't create image!");

// 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, 'arial.ttf', $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, 'arial.ttf', $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

Result: http://www.7679679.com/app/test-ansi.php

Floating answered 21/2, 2013 at 10:35 Comment(4)
If you set header... just before imagepng, what errormsg do you get?Elum
Does php give you any error/notice? What does imagettftext function return? It should returns array of points or false on error. try $text_result = imagettftext(...); if($text_result===false){echo("ERROR");}else print_r($text_result);. Another thing, when you are filling your newly created image, you left one pixel on height and filled only 29 pixels of 100 on width, is that on purpose?Xeric
how did you print that array???Blayze
please consider to accept an answer (click tick mark on the left) if it actually answered your questionElum
E
21

Had the same problem, with FreeType installed, solution was

 $font = "./Arial.ttf"; // <--- put ./ in front of filename
Elum answered 21/2, 2013 at 11:52 Comment(5)
Worked for me! Lol! Can you please explain in detail why this works?Natale
sorry, I have no idea of why this is so!Elum
worked! - but really weird, would also like to know why this is happening?Concessionaire
I even had to add realpath('./Arial.ttf')Latitudinarian
defined('ROOT_PATH') || define('ROOT_PATH', realpath(dirname(FILE) . '')); $font_path = ROOT_PATH.'/fonts/arial-italic.ttf';Macaco
S
8

Notice you don't have Free Type installed:

["FreeType Support"]=>
  bool(false)

This function requires both the GD library and the » FreeType library.

You will need to install Free Type library before you can use this function.

try installing these package:s freetype, freetype-devel

If you compiled PHP you can make sure you added enabled freetype during compile time:

--with-freetype-dir=/usr/include/freetype2/ --with-freetype

Or if you using something such as YUM or APT-GET it should be really simple to install those libraries, and a quick search ob google with get you started.

Substrate answered 21/2, 2013 at 10:40 Comment(1)
Requirement for Freetype support is documented in PHP manual, see php.net/manual/en/function.imagettftext.php and php.net/manual/en/function.imagettfbbox.phpSteelmaker
H
8

Well i also got problem around

$font='arial.tff'; 

I think you should provide absolute path to the $font like

$font="c:/windows/fonts/arial.ttf";

i assume you are a windows user. and remove

header('Content-Type:image/png');

to get the real error

Hindustani answered 15/3, 2015 at 7:22 Comment(2)
adding local path might not be a perfect solution maybe use getcwd() + file name.Sheridansherie
@Sheridansherie you are absolutely correct, but this path is for the font installed in OS, I will update the answer. Thanks for valuable suggestionHindustani
H
6

I solved this problem with this solution:

$fontfile= __DIR__.'/Fontname.ttf';
Hasbeen answered 12/6, 2019 at 14:20 Comment(0)
B
-1

Andrew is right, the php manual for imagettftext states FreeType is required to use imagettftext, imagettfbox, and others. Most people the GD devel package will install FreeType automatically:

Fedora/Redhat:

yum install gd gd-devel php-gd

Debian/Ubuntu:

apt-get install php5-gd libgd2-xpm libgd2-xpm-dev

This error was probably in your logs:

PHP Fatal error:  Call to undefined function imageTTFText()
Balderas answered 3/11, 2014 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.