Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Phalcon\Image\Adapter\Gd throwing an exception for the text function #15188

Closed
nickweavers opened this issue Oct 27, 2020 · 8 comments · Fixed by #15418
Closed

[BUG]: Phalcon\Image\Adapter\Gd throwing an exception for the text function #15188

nickweavers opened this issue Oct 27, 2020 · 8 comments · Fixed by #15418
Assignees
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report status: low Low

Comments

@nickweavers
Copy link

Questions? Forum: https://phalcon.link/forum or Discord: https://phalcon.link/discord

Describe the bug
Phalcon\Image\Adapter\Gd throwing an exception for the text function
I have described the issue here and what I have done to investigate:
https://stackoverflow.com/questions/64541937/phalcon-image-adapter-gd-throwing-an-exception-for-the-text-function
But I think this must be a bug because I am calling the text function with the correct parameters as per the Phalcon 3.4 docs.

I have the following in my code:

use Phalcon\Image\Adapter\GD as Image;
...
    $imagePath = BASE_DIR . '/public/img/map-icons/' . $client->getId() . '/' . $file;
    $image = new Image($imagePath);
...
    $fontFile = $this->getFontPath();
    $image->text($text, $x, $y, 1, '#FFFFFF', $fontSize, $fontFile);

Under Phalcon 1.3.4 and php 5.4.45 it works fine, but when I migrated the application to a new server running Phalcon version 3.4.5 and php 7.3.17 the $image->text($text, $x, $y, 1, '#FFFFFF', $fontSize, $fontFile); is giving the following error:

Project staging.api.my-domain.com raised exception class Phalcon\lmage\Exception with message "Call to imagettfbboxO failed" at gd.zep, line 357

The literal values being passed to $image->text(..) are as follows:

$image->text(H, 9, 20, 1, '#FFFFFF', 11, '/var/www/vhosts/staging.api.mydomain.com/src/fonts/arialbd.ttf');

When I try the example for PHP's imagettfbbox given on the page here https://www.php.net/manual/en/function.imagettfbbox.php the example works and shows a white box with text written across it diagonally.

So it doesn't appear to be a problem with PHP imagettfbbox.

Details

  • Phalcon version: 3.4.5
  • PHP Version: 7.3.17
  • Operating System: Amazon Linux (an EC2)
  • Installation type: sudo yum-config-manager --enable remi-php73
  • Zephir version (if any):
  • Server: Apache
  • Other related info (Database, table schema):

Additional context
Here are the php extensions that are installed:

$ php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dbg
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
json
libxml
mbstring
memcached
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
phalcon
Phar
posix
readline
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
zip
zlib
@nickweavers nickweavers added bug A bug report status: unverified Unverified labels Oct 27, 2020
@nickweavers
Copy link
Author

nickweavers commented Oct 27, 2020

I have created a small test case:

<?php
require('../vendor/autoload.php'); 
use Phalcon\Image\Adapter\GD as Image;
$imagePath = './img/map-icons/sebroche/house.png';
$image = new Image($imagePath);
$fontPath = '../fonts/arialbd.ttf';
$image->text('Hello', 20, 20, 9, '#FFFFFF', 10, $fontPath);
?>

Here's the output when I run it from the cli:

$ php test.php
PHP Fatal error:  Uncaught Phalcon\Image\Exception: Call to imagettfbbox() failed in phalcon/image/adapter/gd.zep:357
Stack trace:
#0 [internal function]: Phalcon\Image\Adapter\Gd->_text('Hello', 20, 20, 9, 255, 255, 255, 10, '../fonts/arialb...')
#1 /var/www/vhosts/staging.api.mydomain.com/src/public/test.php(8): Phalcon\Image\Adapter->text('Hello', 20, 20, 9, '#FFFFFF', 10, '../fonts/arialb...')
#2 {main}
  thrown in phalcon/image/adapter/gd.zep on line 357

@Jeckerson
Copy link
Member

Related #14950

@Jeckerson
Copy link
Member

@nickweavers Please try to specify absolute path to the font inside $fontPath variable.

@nickweavers
Copy link
Author

nickweavers commented Oct 27, 2020

Okay, I have updated my example to use absolute paths:

<?php
require('../vendor/autoload.php'); 
use Phalcon\Image\Adapter\GD as Image;
$imagePath = '/var/www/vhosts/staging.api-mydomain.com/src/public/img/map-icons/sebroche/house.png';
if (!file_exists($imagePath)) {
    die("File {$imagePath} does not exists");
}
$image = new Image($imagePath);
$fontPath = '/var/www/vhosts/staging.api-mydomain.com/src/fonts/arialbd.ttf';
if (!file_exists($fontPath)) {
    die("File {$fontPath} does not exists");
}
$image->text('Hello', 20, 20, 9, '#FFFFFF', 10, $fontPath);  
?>

but I still get the error:

$ php test.php
PHP Fatal error:  Uncaught Phalcon\Image\Exception: Call to imagettfbbox() failed in phalcon/image/adapter/gd.zep:357
Stack trace:
#0 [internal function]: Phalcon\Image\Adapter\Gd->_text('Hello', 20, 20, 9, 255, 255, 255, 10, '/var/www/vhosts...')
#1 /var/www/vhosts/staging.api-mydomain.com/src/public/test.php(8): Phalcon\Image\Adapter->text('Hello', 20, 20, 9, '#FFFFFF', 10, '/var/www/vhosts...')
#2 {main}
  thrown in phalcon/image/adapter/gd.zep on line 357

@Jeckerson Jeckerson added status: low Low 5.0 The issues we want to solve in the 5.0 release transfer 4.1.0 and removed status: unverified Unverified 5.0 The issues we want to solve in the 5.0 release transfer labels Oct 27, 2020
@Jeckerson
Copy link
Member

To hotfix your problem, just do not specify $fontPath variable into text() method.

The problem is located in let s0 = (int) space[0];, which apparently can not be zero (according existing logic).

let space = imagettfbbox(size, 0, fontfile, text);
if isset space[0] {
let s0 = (int) space[0];
let s1 = (int) space[1];
let s4 = (int) space[4];
let s5 = (int) space[5];
}
if unlikely (!s0 || !s1 || !s4 || !s5) {
throw new Exception("Call to imagettfbbox() failed");

@nickweavers
Copy link
Author

@Jeckerson, what font will it use if one cannot be given to the text() method?

@Jeckerson
Copy link
Member

@nickweavers It will use different approach with different functions.

let width = (int) imagefontwidth(size) * strlen(text);
let height = (int) imagefontheight(size);
if offsetX < 0 {
let offsetX = this->width - width + offsetX;
}
if offsetY < 0 {
let offsetY = this->height - height + offsetY;
}
let color = imagecolorallocatealpha(this->image, r, g, b, opacity);
imagestring(this->image, size, offsetX, offsetY, text, color);

Check docs of imagestring function - https://www.php.net/manual/en/function.imagestring.php

@Jeckerson Jeckerson added 4.1.1 The issues we want to solve in the 4.1.1 release and removed 4.1.0 labels Dec 14, 2020
@Jeckerson Jeckerson added 5.0 The issues we want to solve in the 5.0 release and removed 4.1.1 The issues we want to solve in the 4.1.1 release labels Mar 26, 2021
@niden niden linked a pull request Apr 20, 2021 that will close this issue
5 tasks
@niden
Copy link
Member

niden commented Apr 21, 2021

Thank you @nickweavers for the report.

This has been resolved with #15418

@niden niden closed this as completed Apr 21, 2021
@niden niden moved this to Released in Phalcon v5 Aug 25, 2022
@niden niden added this to Phalcon v5 Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report status: low Low
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants