1

I am using Laravel with the docker PHP:7.3-fpm image.

I get the issue Call to undefined function Intervention\Image\Gd\imagejpeg()" when using Image Intervention Package like this:

$avatar = \Image::make($image->getRealPath());
$avatar->fit(221, 146)->encode('jpg', 80);

Here is the full error message:

exception: Symfony\Component\Debug\Exception\FatalThrowableError^ {#8221
      -originalClassName: "Error"
      #message: "Call to undefined function Intervention\Image\Gd\imagejpeg()"
      #code: 0
      #file: "/var/www/vendor/intervention/image/src/Intervention/Image/Gd/Encoder.php"
      #line: 17
      #severity: E_ERROR
      trace: {
        /var/www/vendor/intervention/image/src/Intervention/Image/Gd/Encoder.php:17 { …}
        /var/www/vendor/intervention/image/src/Intervention/Image/AbstractEncoder.php:123 { …}
        /var/www/vendor/intervention/image/src/Intervention/Image/AbstractDriver.php:79 { …}
        /var/www/vendor/intervention/image/src/Intervention/Image/Image.php:121 { …}
        /var/www/app/Repositories/MediaRepository.php:37 {
          App\Repositories\MediaRepository->addGalleryToTraining(array $images): void^
          › 
          › $avatar->fit(221, 146)->encode('jpg', 80);
          › $avatar_path  = 'trainings/gallery/' . $gallery->id . '/avatars/' . $image->hashName();
          arguments: {
            $format: "jpg"
            $quality: 80
          }
        }

Line 17 of Gd/Encoder.php is this:

imagejpeg($this->image->getCore(), null, $this->quality);

I found here that I should install GD with jpg / png support. However, the answer there was for the php:7.2-fpm-alpine image. Since I don't use the image, I can't apply it the apk add libjpg .. solution.

I also found here which uses the PHP:7.2-fpm image and claims to fix jpg issues.

I used

RUN apt-get install -y build-essential libssl-dev zlib1g-dev libpng-dev libjpeg-dev libfreetype6-dev

and

RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install gd

from that solution, but I still get the above error. Here is my complete Dockerfile:

FROM php:7.3-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    git \
    curl \
    libssl-dev \
    zlib1g-dev \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libonig-dev \
    libxml2-dev \
    libicu-dev \
    zip \
    unzip \
    libzip-dev \
    -y mariadb-client

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install zip mysqli soap pdo_mysql mbstring exif pcntl bcmath gd  && docker-php-ext-enable mysqli && docker-php-ext-configure intl \
    && docker-php-ext-install \
        intl \
    && docker-php-ext-enable intl

RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    --with-gd \
    --with-jpeg-dir \
    --with-png-dir \
    --with-zlib-dir

RUN apt update && apt install -y libc-client-dev libkrb5-dev && rm -r /var/lib/apt/lists/*

RUN docker-php-ext-configure imap --with-kerberos --with-imap-ssl && docker-php-ext-install imap

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www

USER $user

Any idea what could cause this?

Adam
  • 25,960
  • 22
  • 158
  • 247
  • 1
    What does `php -m` tell you? Is the module installed or not? If you're relying on a packaged extension, you don't need to install all those development libraries, just `apt-get install php7.2-gd` – miken32 Dec 03 '20 at 22:12
  • @miken32 `gd` is listed among the packages. I have also installed it through `RUN docker-php-ext-install ... gd ...`. I still receive `Call to undefined function Intervention\Image\Gd\imagejpeg()"` – Adam Dec 04 '20 at 07:36
  • Please, send us chunk of your code. imagejpeg() is global, not namespaced, so it could be issue within code. – Łukasz Jakubek Dec 04 '20 at 08:02
  • @ŁukaszJakubek I added the code section, although it simply boils down to using `imagejpeg`. – Adam Dec 04 '20 at 08:12
  • @ŁukaszJakubek also, this code is working when using Homestead and Vagrant - its just failing since I tried to switch to docker. – Adam Dec 04 '20 at 08:13
  • Right, so check `php -i` or `phpinfo()` output for `"JPEG Support": enabled` and `"libJPEG Version": [number]`, if it's missing, then it would be libs problem. I looked into my docker config, is for alpine, but maybe help. I have `docker-php-ext-configure gd` before `docker-php-ext-install` and in` docker-php-ext-configure gd` i have `--with-jpeg` but you have twice `--with-jpeg-dir`. – Łukasz Jakubek Dec 04 '20 at 08:26
  • @ŁukaszJakubek I just managed to get it working using `RUN docker-php-ext-install -j$(nproc) gd` – Adam Dec 04 '20 at 08:27

1 Answers1

2

This line was missing:

RUN docker-php-ext-install -j$(nproc) gd

So my final dockerfile looks like this:

FROM php:7.3-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    git \
    curl \
    libssl-dev \
    zlib1g-dev \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libonig-dev \
    libxml2-dev \
    libicu-dev \
    zip \
    unzip \
    libzip-dev \
    -y mariadb-client

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install zip mysqli soap pdo_mysql mbstring exif pcntl bcmath gd  && docker-php-ext-enable mysqli && docker-php-ext-configure intl \
    && docker-php-ext-install \
        intl \
    && docker-php-ext-enable intl


RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    --with-gd \
    --with-jpeg-dir \
    --with-png-dir \
    --with-zlib-dir

RUN docker-php-ext-install -j$(nproc) gd

RUN apt update && apt install -y libc-client-dev libkrb5-dev && rm -r /var/lib/apt/lists/*

RUN docker-php-ext-configure imap --with-kerberos --with-imap-ssl && docker-php-ext-install imap

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www

USER $user

FOund that missing line at Troubles with Docker + PHP7 + GD resulting in "Call to undefined function imagecreatefromjpeg()"

Adam
  • 25,960
  • 22
  • 158
  • 247