Gaurav Gupta
Gaurav Gupta

Reputation: 1

Inserting text in image not working in PHP

My function returns the error message:

Call to undefined function imagettftext()

I've already tried every thing and won't able enable (--with-freetype-dir=DIR)

    $black = imagecolorallocate($text_im, 0, 0, 0);
    imagefilledrectangle($text_im, 0, 0, 399, 29, $white);
    $text = 'testing shop ,9876543214';
    // Replace path by your own font path
    $font = 'arial.ttf';
    imagettftext($text_im, 20, 0, 10, 20, $black, $font, $text);
    imagepng($text_im);```

Upvotes: 0

Views: 298

Answers (1)

MaartenDev
MaartenDev

Reputation: 5802

Both GD and Freetype have to be installed, Freetype can be installed using:

brew install freetype

When the installation finishes execute the following command to get the path:

echo "/usr/local/Cellar/freetype/$(ls /usr/local/Cellar/freetype)/lib"

and then use that path in the flag:

--with-freetype-dir=/usr/local/Cellar/freetype/2.10.1/lib

The second option is to compile PHP from scratch by following these steps:

  1. Download php: https://www.php.net/downloads.php
  2. Unzip the zip
  3. open the terminal and navigate to the folder
  4. run:
./configure \
  --prefix=/usr/local/bin/php \
  --with-apxs2=/usr/local/apache/bin/apxs \
  --enable-mbstring \
  --with-curl \
  --with-openssl \
  --with-xmlrpc \
  --enable-soap \
  --enable-zip \
  --with-gd \
  --with-jpeg-dir \
  --with-png-dir \
  --with-mysqli \
  --with-pgsql \
  --enable-embedded-mysqli \
  --with-freetype-dir \
  --with-ldap \
  --enable-intl \
  --with-xsl \
  --with-zlib
  1. then run:
make
  1. and install it:
make install

Upvotes: 2

Related Questions