Ad Widget

Collapse

PHP-GD issues (imagettftext & imagettfbbox) after upgrading to Zabbix v5.0

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • catalyst
    Junior Member
    • Jul 2018
    • 4

    #1

    PHP-GD issues (imagettftext & imagettfbbox) after upgrading to Zabbix v5.0

    Warning - this is a long post, and as far as I can tell not really a Zabbix bug. But I'm seeing it after upgrading our servers to Zabbix 5.0 and would love anyone/everyone's thoughts & ideas to solve the problem.

    I'm running into issues with php-gd (or libgd, more specifically) which is causing the graphs not to load properly. My server is RHEL 7 with Apache, and per the new requirements with release 5.0, I upgraded php from 5.4 to 7.4. (Had to load from REMI repos, b/c my company hasn't setup RHSCL subscriptions.)

    Here's the error that I'm seeing in /var/log/httpd/error_log
    Code:
    [Mon Jun 15 09:31:50.017879 2020] [php7:warn] [pid 19761] [client //redacted//] PHP Warning: imagettfbbox(): Problem loading glyph in /usr/share/zabbix/include/graphs.inc.php on line 584, referer: http://redacted/zabbix/zabbix.php?action=charts.view&view_as=showgraph&fi lter_hostids%5B%5D=10551&filter_set=1
    [Mon Jun 15 09:31:50.017941 2020] [php7:notice] [pid 19761] [client //redacted//] PHP Notice: Trying to access array offset on value of type bool in /usr/share/zabbix/include/graphs.inc.php on line 587, referer: http://redacted/zabbix/zabbix.php?action=charts.view&view_as=showgraph&fi lter_hostids%5B%5D=10551&filter_set=1
    [Mon Jun 15 09:31:50.017956 2020] [php7:notice] [pid 19761] [client //redacted//] PHP Notice: Trying to access array offset on value of type bool in /usr/share/zabbix/include/graphs.inc.php on line 587, referer: http://redacted/zabbix/zabbix.php?action=charts.view&view_as=showgraph&fi lter_hostids%5B%5D=10551&filter_set=1
    [Mon Jun 15 09:31:50.017969 2020] [php7:notice] [pid 19761] [client //redacted//] PHP Notice: Trying to access array offset on value of type bool in /usr/share/zabbix/include/graphs.inc.php on line 588, referer: http://redacted/zabbix/zabbix.php?action=charts.view&view_as=showgraph&fi lter_hostids%5B%5D=10551&filter_set=1
    [Mon Jun 15 09:31:50.017980 2020] [php7:notice] [pid 19761] [client //redacted//] PHP Notice: Trying to access array offset on value of type bool in /usr/share/zabbix/include/graphs.inc.php on line 588, referer: http://redacted/zabbix/zabbix.php?action=charts.view&view_as=showgraph&fi lter_hostids%5B%5D=10551&filter_set=1
    [Mon Jun 15 09:31:50.017990 2020] [php7:notice] [pid 19761] [client //redacted//] PHP Notice: Trying to access array offset on value of type bool in /usr/share/zabbix/include/graphs.inc.php on line 589, referer: http://redacted/zabbix/zabbix.php?action=charts.view&view_as=showgraph&fi lter_hostids%5B%5D=10551&filter_set=1
    [Mon Jun 15 09:31:50.018301 2020] [php7:warn] [pid 19761] [client //redacted//] PHP Warning: imagettftext(): Problem loading glyph in /usr/share/zabbix/include/graphs.inc.php on line 539, referer: http://redacted/zabbix/zabbix.php?action=charts.view&view_as=showgraph&fi lter_hostids%5B%5D=10551&filter_set=1
    I know this isn't a Zabbix-specific bug because I can recreate the segfault by running the same line (with dummy input values) on the php command line that graphs.inc.php uses to find the bounding box for drawing/writing the text onto the gd-based image:
    Code:
    (base) server:user # php -r "var_dump(imagettfbbox(9,0,'/usr/share/fonts/dejavu/DejaVuSans.ttf','2020-06-19'));"                          
    Segmentation fault (core dumped)
    If I change the fontfile, I get the "Problem loading glyph" error:
    Code:
    (base) server:user # php -r "var_dump(imagettfbbox(9,0,'/usr/share/fonts/ubuntu.ttf','2020-06-19'));"                      
    PHP Warning:  imagettfbbox(): Problem loading glyph in Command line code on line 1
    The imagettfbbox() function is called from the Zabbix php function "imageTextSize()" and used within another function called imageText() where you can also find the imagettftext() command (which calls to the gd library to actually draw the text onto the image). Both of these GD functions are causing issues (see initial log output, first line & last line). From un-altered graphs.inc.php:
    Code:
    /**
     * Draws a text on an image. Supports TrueType fonts.
     *
     * @param resource      $image
     * @param int           $fontsize
     * @param int           $angle
     * @param int           $x
     * @param int           $y
     * @param int           $color          a numeric color identifier from imagecolorallocate() or imagecolorallocatealpha()
     * @param string        $string
     */
    function imageText($image, $fontsize, $angle, $x, $y, $color, $string) {
            if ((preg_match(ZBX_PREG_DEF_FONT_STRING, $string) && $angle != 0) || ZBX_FONT_NAME == ZBX_GRAPH_FONT_NAME) {
                    $ttf = ZBX_FONTPATH.'/'.ZBX_FONT_NAME.'.ttf';
                    imagettftext($image, $fontsize, $angle, $x, $y, $color, $ttf, $string);
            }
            elseif ($angle == 0) {
                    $ttf = ZBX_FONTPATH.'/'.ZBX_GRAPH_FONT_NAME.'.ttf';
                    imagettftext($image, $fontsize, $angle, $x, $y, $color, $ttf, $string);
            }
            else {
                    $ttf = ZBX_FONTPATH.'/'.ZBX_GRAPH_FONT_NAME.'.ttf';
                    $size = imageTextSize($fontsize, 0, $string);
    
                    $imgg = imagecreatetruecolor($size['width'] + 1, $size['height']);
                    $transparentColor = imagecolorallocatealpha($imgg, 200, 200, 200, 127);
                    imagefill($imgg, 0, 0, $transparentColor);
                    imagettftext($imgg, $fontsize, 0, 0, $size['height'], $color, $ttf, $string);
    
                    $imgg = imagerotate($imgg, $angle, $transparentColor);
                    imagealphablending($imgg, false);
                    imagesavealpha($imgg, true);
                    imagecopy($image, $imgg, $x - $size['height'], $y - $size['width'], 0, 0, $size['height'], $size['width'] + 1);
                    imagedestroy($imgg);
            }
    }
    
    /**
     * Calculates the size of the given string.
     *
     * Returns the following data:
     * - height     - height of the text;
     * - width              - width of the text;
     * - baseline   - baseline Y coordinate (can only be used for horizontal text, can be negative).
     *
     * @param int           $fontsize
     * @param int           $angle
     * @param string        $string
     *
     * @return array
     */
    function imageTextSize($fontsize, $angle, $string) {
            if (preg_match(ZBX_PREG_DEF_FONT_STRING, $string) && $angle != 0) {
                    $ttf = ZBX_FONTPATH.'/'.ZBX_FONT_NAME.'.ttf';
            }
            else {
                    $ttf = ZBX_FONTPATH.'/'.ZBX_GRAPH_FONT_NAME.'.ttf';
            }
    
            $ar = imagettfbbox($fontsize, $angle, $ttf, $string);
    
            return [
                    'height' => abs($ar[1] - $ar[5]),
                    'width' => abs($ar[0] - $ar[4]),
                    'baseline' => $ar[1]
            ];
    }
    I'm thinking libgd 2.3.0 release may be part of the issue? Here's the link to the libgd bug (that was recently patched) saying that empty strings used to cause a failure, but that the return values have been updated.
    When trying to render an empty string via gdImageStringFT(), the function fails as of libgd 2.3.0, while formerly it was just a no-op. Test script #include #include int main() { gd...


    I tried downgrading PHP to 7.3 & 7.2, and I even found a release candidate of PHP-GD that got me back to v2.2.5 (with truetype font support enabled), but nothing works reliably. The only way to get the graphs to show up without fault is to truncate them to the first character of the string (image below). Sometimes 2 or 3 characters will work, but not always.
    Code:
    (base) server:user # php -r "var_dump(imagettfbbox(9,0,'/usr/share/fonts/ubuntu.ttf','2020-06-19'));"
    PHP Warning:  imagettfbbox(): Problem loading glyph in Command line code on line 1
    bool(false)
    (base) server:user #
    (base) server:user #
    (base) server:user # php -r "var_dump(imagettfbbox(9,0,'/usr/share/fonts/ubuntu.ttf','2'));"        
    array(8) {
      [0]=>
      int(0)
      [1]=>
      int(0)
      [2]=>
      int(6)
      [3]=>
      int(0)
      [4]=>
      int(6)
      [5]=>
      int(-8)
      [6]=>
      int(0)
      [7]=>
      int(-8)
    }
    (base) server:user # php -r "var_dump(imagettfbbox(9,0,'/usr/share/fonts/ubuntu.ttf','20'));"
    array(8) {
      [0]=>
      int(0)
      [1]=>
      int(0)
      [2]=>
      int(6)
      [3]=>
      int(0)
      [4]=>
      int(6)
      [5]=>
      int(-8)
      [6]=>
      int(0)
      [7]=>
      int(-8)
    }
    (base) server:user # php -r "var_dump(imagettfbbox(9,0,'/usr/share/fonts/ubuntu.ttf','202'));"
    array(8) {
      [0]=>
      int(0)
      [1]=>
      int(0)
      [2]=>
      int(6)
      [3]=>
      int(0)
      [4]=>
      int(6)
      [5]=>
      int(-8)
      [6]=>
      int(0)
      [7]=>
      int(-8)
    }
    (base) server:user # php -r "var_dump(imagettfbbox(9,0,'/usr/share/fonts/ubuntu.ttf','2020'));"
    Segmentation fault (core dumped)
    Click image for larger version  Name:	1char_graphs.png Views:	0 Size:	106.8 KB ID:	403842

    I'm back on PHP 7.4 / PHP-GD 2.3.0, and have hardcoded the graphs.inc.php file to return a standardized array value for imageTextSize() and modify the $string variable to just the first character at the top of the imageText() function.

    I'm willing to try anything anyone can suggest to get this working. Thanks in advance.
    Last edited by catalyst; 23-06-2020, 03:58. Reason: added tags to post
  • catalyst
    Junior Member
    • Jul 2018
    • 4

    #2
    Solution

    I worked with our IT dept to get RHSCL packages made available to our VMs. Using the suggested rh-php72 packages as the Zabbix v5 upgrade instructions suggest, php-gd is now running v2.1.0 and shows "bundled" (whereas the 2.3.0 & 2.1.5 versions from the Remi repos never did). Bundled, from my understanding, just means that libgd was enabled at compile time, so I bet if someone were to build from source, they could get the newer version working. But I'm happy to report no more issues with segfaults!

    Click image for larger version

Name:	libgd_210_bundled.png
Views:	3013
Size:	10.8 KB
ID:	405462

    Comment

    • BrunoSRibeiro
      Junior Member
      • Aug 2021
      • 2

      #3
      Hello!

      Im using Zabbix 6.0.6, Centos 9, Apache 2.4.53 and PHP 8.1 and the texts in graphs dont appear.
      In the apache log there is follow errors :

      PHP message: PHP Warning: imagettftext(): Could not find/open font in /usr/share/zabbix/include/graphs.inc.php on line 546

      GD was installed with PHP
      Click image for larger version

Name:	gd.png
Views:	1836
Size:	28.3 KB
ID:	448014

      Thanks in advance.

      Comment

      • BrunoSRibeiro
        Junior Member
        • Aug 2021
        • 2

        #4
        I found the problem!

        In path /usr/share/zabbix/assets/fonts/ there is a symbolic link graphfont.ttf to /etc/alternatives/zabbix-web-font, in path /etc/alternatives/ there is a symbolic link zabbix-web-font to /usr/share/fonts/dejavu/DejaVuSans.ttf, but in path /usr/share/fonts/ doesnt exist dejavu folder intead exist dejavu-sans-fonts folder, and them I renamed dejavu-sans-fonts folder to dejavu, it worked.

        Comment


        • aquarios
          aquarios commented
          Editing a comment
          Thanks for the hint, it worked:
          ln -s /etc/alternatives/zabbix-web-font /usr/share/zabbix/assets/fonts/graphfont.ttf
      Working...