hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

imagettfbbox requires the font full path

Open josepsanzcamp opened this issue 12 years ago • 13 comments

[sanz@localhost hhvm]$ php --version

PHP 5.6.2 (cli) (built: Oct 16 2014 08:36:16) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies

[sanz@localhost hhvm]$ hhvm --version

HipHop VM 3.4.0-dev (rel)
Compiler: heads/master-0-g9a587ba04d6ac4924b6cc3445455452d12853944
Repo schema: 30fbdfaa3bba08f05de929a221f6d6b0078d82a8
Extension API: 20140829

[sanz@localhost hhvm]$ rpm -qa|grep httpd-2.4

httpd-2.4.10-1.fc20.x86_64

[sanz@localhost hhvm]$ cat test2.php

<?php
function font_check($font) {
    if(file_exists($font)) echo "FONT '${font}' FOUND!!!\n";
    if(!file_exists($font)) echo "FONT '${font}' NOT FOUND!!!\n";
    $bbox=imagettfbbox(8,0,$font,"A"); // HERE RUNS AS EXPECTED
    echo "ERROR_GET_LAST=".print_r(error_get_last(),true)."\n";
}

function error_reset() {
    set_error_handler("var_dump",0);
    ob_start();
    $olderror=error_reporting(0);
    trigger_error("");
    error_reporting($olderror);
    ob_end_clean();
    restore_error_handler();
}

error_reset();

// WITH THIS PATH, IMAGETTFBBOX WORKS AS EXPECTED
$font="lib/fonts/GorriSans.ttf";
$font=realpath($font); // REALPATH NEEDED BY HHVM
font_check($font);
error_reset();

// WITH THIS PATH, IMAGETTFBBOX FAILS
$font="lib/fonts/GorriSans.ttf";
font_check($font);
error_reset();
?>

USING APACHE AND PHP MODULE [sanz@localhost hhvm]$ wget -q -O - http://127.0.0.1/saltos/utest/hhvm/test2.php

FONT '/home/sanz/workspace/saltos/utest/hhvm/lib/fonts/GorriSans.ttf' FOUND!!!
ERROR_GET_LAST=Array
(
    [type] => 1024
    [message] => 
    [file] => /home/sanz/workspace/saltos/utest/hhvm/test2.php
    [line] => 13
)

FONT 'lib/fonts/GorriSans.ttf' FOUND!!!
ERROR_GET_LAST=Array
(
    [type] => 1024
    [message] => 
    [file] => /home/sanz/workspace/saltos/utest/hhvm/test2.php
    [line] => 13
)

USING APACHE AND HHVM AS FASTCGI [sanz@localhost hhvm]$ wget -q -O - http://127.0.0.1/saltos/utest/hhvm/test2.php

FONT '/home/sanz/workspace/saltos/utest/hhvm/lib/fonts/GorriSans.ttf' FOUND!!!
ERROR_GET_LAST=Array
(
    [type] => 1024
    [message] => 
    [file] => /home/sanz/workspace/saltos/utest/hhvm/test2.php
    [line] => 13
)

FONT 'lib/fonts/GorriSans.ttf' FOUND!!!
ERROR_GET_LAST=Array
(
    [type] => 2
    [message] => Could not find/open font
    [file] => /home/sanz/workspace/saltos/utest/hhvm/test2.php
    [line] => 5
)

josepsanzcamp avatar Nov 06 '14 17:11 josepsanzcamp

Hi I cannot reproduce this error,Here is my output: FONT '/home/hamid/Desktop/Ubuntu-BI.ttf' FOUND!!! ERROR_GET_LAST=Array ( [type] => 1024 [message] => [file] => /home/hamid/Desktop/4155/index.php [line] => 13 )

FONT '/home/hamid/Desktop/Ubuntu-BI.ttf' FOUND!!! ERROR_GET_LAST=Array ( [type] => 1024 [message] => [file] => /home/hamid/Desktop/4155/index.php [line] => 13 )

Do you have permission to access the file? Also would you provide your ini file?

hamidre13 avatar Nov 13 '14 21:11 hamidre13

Do you have permission to access the file? YES Also would you provide your ini file? YES

[sanz@localhost Desktop]$ cat /etc/php.ini | gawk '{if(substr($1,1,1)!=";" && length($1)>0)print $0}'

[PHP]
engine = On
short_open_tag = Off
asp_tags = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 17
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = On
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 120M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 120M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
[filter]
[iconv]
[intl]
[sqlite]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
sendmail_path = /usr/sbin/sendmail -t -i
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQL]
mysql.allow_local_infile = On
mysql.allow_persistent = On
mysql.cache_size = 2000
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"
[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatibility_mode = Off
mssql.secure_connection = Off
[Assertion]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[curl]

josepsanzcamp avatar Nov 14 '14 08:11 josepsanzcamp

You are executing the php code using the hhvm via apache??? from command line works as expected and the problem only appear when is executed via apache.

josepsanzcamp avatar Nov 14 '14 08:11 josepsanzcamp

I understand that the problem only appear when using relative paths. With absolute paths never fails.

josepsanzcamp avatar Nov 14 '14 08:11 josepsanzcamp

I have made some changes to font_check function to validate that it's not a permissions problem:

function font_check($font) {
    if(file_exists($font)) echo "FONT '${font}' FOUND!!!\n";
    if(!file_exists($font)) echo "FONT '${font}' NOT FOUND!!!\n";
    file_get_contents($font);
    echo "ERROR_GET_LAST=".print_r(error_get_last(),true)."\n";
    $bbox=imagettfbbox(8,0,$font,"A");
    echo "ERROR_GET_LAST=".print_r(error_get_last(),true)."\n";
}

And the result is the same:

FONT '/home/sanz/workspace/saltos/utest/hhvm/lib/fonts/GorriSans.ttf' FOUND!!!
ERROR_GET_LAST=Array
(
    [type] => 1024
    [message] => 
    [file] => /home/sanz/workspace/saltos/utest/hhvm/test2.php
    [line] => 15
)

ERROR_GET_LAST=Array
(
    [type] => 1024
    [message] => 
    [file] => /home/sanz/workspace/saltos/utest/hhvm/test2.php
    [line] => 15
)

FONT 'lib/fonts/GorriSans.ttf' FOUND!!!
ERROR_GET_LAST=Array
(
    [type] => 1024
    [message] => 
    [file] => /home/sanz/workspace/saltos/utest/hhvm/test2.php
    [line] => 15
)

ERROR_GET_LAST=Array
(
    [type] => 2
    [message] => Could not find/open font
    [file] => /home/sanz/workspace/saltos/utest/hhvm/test2.php
    [line] => 7
)

josepsanzcamp avatar Nov 14 '14 08:11 josepsanzcamp

Could be related with this problem (http://php.net/manual/en/function.imagettftext.php):

fontfile

The path to the TrueType font you wish to use.

Depending on which version of the GD library PHP is using, when fontfile does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.

In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.

    <?php
    // Set the enviroment variable for GD
    putenv('GDFONTPATH=' . realpath('.'));

    // Name the font to be used (note the lack of the .ttf extension)
    $font = 'SomeFont';
    ?>

josepsanzcamp avatar Nov 14 '14 09:11 josepsanzcamp

works for me if i use the __DIR__ magic constant as seen here: https://github.com/r3wt/superuser/blob/master/libs/custom/funcs.php#L574

r3wt avatar Nov 14 '14 09:11 r3wt

I understand that this is not a bug of hhvm, and the problem is explained in the fontfile specification, For me the solution (to hhvm and php too) is to use an absolute path. Is use

$font=getcwd()."/lib/fonts/GorriSans.ttf";

Because the __DIR__ magic constant contains the directory of the php script that contains the code, but the script can be launched from another script located in another directory:

- saltos/
    - code/
        - index.php
        - php/
            - action/
                - captcha.php
        - lib/
            - fonts/
                - GorriSans.ttf

With this directory structure, the __DIR__ magic constant are:

__DIR__ of index.php => /.../saltos/code
__DIR__ of captcha.php => /.../saltos/code/php/action

And to access to the lib/fonts/GorriSans.ttf from the captcha.php using the ``DIR` magic constant, the valid path is:

$font=__DIR__."/../../lib/fonts/GorriSans.ttf";

josepsanzcamp avatar Nov 14 '14 10:11 josepsanzcamp

@josepsanzcamp I suspect it's unrelated but I fixed a relative path bug earlier this week (d4f7f958648b567e222f1cd23c021834c76eef9d), have you tried running with master recently?

paulbiss avatar Nov 14 '14 16:11 paulbiss

@paulbiss I tested that the problem persists with the last code.

josepsanzcamp avatar Nov 17 '14 11:11 josepsanzcamp

@hamidre13 are you working on this?

paulbiss avatar Dec 08 '14 21:12 paulbiss

@paulbiss Hi, I did not have time yet. Also since I have to enable fast cgi in HHVm I am not sure If I can debug it efficiently.

hamidre13 avatar Dec 08 '14 21:12 hamidre13

I am going over old issues on this repository, to see which ones apply to the current versions of hhvm.

I tried to reproduce this issue in a modern hhvm build 4.56.

tree
.
├── AFont.ttf
└── file.hack

0 directories, 2 files
function font_check(string $font): void {
    if (file_exists($font)) echo "FONT '{$font}' FOUND!!!\n";
    if (!file_exists($font)) echo "FONT '{$font}' NOT FOUND!!!\n";
    $bbox = imagettfbbox(8., 0., $font, "A"); // HERE RUNS AS EXPECTED
    \var_dump($bbox);
    echo "ERROR_GET_LAST=".print_r(error_get_last(), true)."\n";
}

function error_reset(): void {
    set_error_handler("var_dump", 0);
    ob_start();
    $olderror = error_reporting(0);
    trigger_error("");
    error_reporting($olderror);
    ob_end_clean();
    restore_error_handler();
}

<<__EntryPoint>>
function main(): void {
    error_reset();
    $font = "AFont.ttf";
    font_check(\realpath($font));
    error_reset();
    font_check($font);
    error_reset();
}

This resulted in it finding bounding boxes, so I believe this is fixed. If there is something I missed, please reopen the issue.

lexidor avatar May 19 '20 18:05 lexidor