49

I'm having troubles when trying to create an image using imagecreatefromjpeg using this Dockerfile to generate the container:

FROM  php:7.1-apache

RUN apt-get update && \
    apt-get install -y -qq git \
        libjpeg62-turbo-dev \
        apt-transport-https \
        libfreetype6-dev \
        libmcrypt-dev \
        libpng12-dev \
        libssl-dev \
        zip unzip \
        nodejs \
        npm \
        wget \
        vim

RUN pecl install redis && docker-php-ext-enable redis
RUN docker-php-ext-install -j$(nproc) iconv mcrypt zip pdo pdo_mysql gd bcmath

COPY ./containers/yii.conf /etc/apache2/sites-available/000-default.conf

RUN for mod in rewrite headers; do a2enmod $mod; done && service apache2 restart

WORKDIR /var/www/html/

GD was correctly installed (libjpeg too - both appearing in php -i and phpinfo()) but imagecreatefromjpeg does not works and I don't know why.


I also ran apt install libjpeg-dev libpng-dev libfreetype6-dev trying to ~force~ reinstallation (or reconfiguration) but seems doesn't succeded (yes, I also restart the container).

root@e8db647c96c4:/var/www/html# php -i | grep -i GD
/usr/local/etc/php/conf.d/docker-php-ext-gd.ini,
gd
GD Support => enabled
GD Version => bundled (2.1.0 compatible)
gd.jpeg_ignore_warning => 1 => 1
root@e8db647c96c4:/var/www/html# 

root@e8db647c96c4:/var/www/html# docker-php-ext-enable gd

warning: gd (gd.so) is already loaded!

root@e8db647c96c4:/var/www/html# 

I've tried apt install libgd2-xpm-dev* and apparently it doesn't solves the problem.


Solved

I was missing to put

RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) gd

into my Dockerfile.


Full revised Dockerfile:

FROM  php:7.1-apache

RUN apt-get update && \
    apt-get install -y -qq git \
        libjpeg62-turbo-dev \
        apt-transport-https \
        libfreetype6-dev \
        libmcrypt-dev \
        libpng12-dev \
        libssl-dev \
        zip unzip \
        nodejs \
        npm \
        wget \
        vim

RUN pecl install redis && docker-php-ext-enable redis
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install -j$(nproc) iconv mcrypt zip pdo pdo_mysql gd bcmath

COPY ./containers/yii.conf /etc/apache2/sites-available/000-default.conf

RUN for mod in rewrite headers; do a2enmod $mod; done && service apache2 restart

WORKDIR /var/www/html/
Community
  • 1
  • 1
John Murowaniecki
  • 613
  • 1
  • 5
  • 9
  • 2
    The `docker-php-ext-configure gd` command was what I needed to sold a similar error. Thanks! – Jessedc Mar 11 '18 at 07:03
  • 6
    Please move your solution to its own answer, thank you. – Cœur Jun 18 '18 at 02:46
  • 7
    The new version of PHP 7.4 does not have the options `--with-freetype-dir` and `--with-jpeg-dir` anymore. Instead, I used the options `--with-freetype` and `--with-jpeg` and my testsuite turned green again. – Ryangr0 Dec 02 '19 at 17:26

4 Answers4

33

PHP 7.4 (Alpine)

If anybody is struggling to enable JPEG support in GD with PHP 7.4, here's what I had to do in order to be able to use imagecreatefromjpeg() function. My example is based on Alpine 3.10, if you're using other distribution adjust it to your needs.

First install dependencies, in my case beside JPEG I need support for PNG files.

apk add jpeg-dev libpng-dev

After that we can run docker-php-ext-configure command to configure our gd with JPEG support. Notice that flag --with-jpeg-dir was changed to --with-jpeg and we don't need to provide flag to enable PNG. More you can read in PHP 7.4 Changelog in GD section.

docker-php-ext-configure gd --with-jpeg

Directly after that let's run docker-php-ext-install to install GD itself.

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

FULL EXAMPLE

FROM php:7.4-fpm-alpine3.10

RUN apk add jpeg-dev libpng-dev \
    && docker-php-ext-configure gd --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd
Tomasz
  • 4,847
  • 2
  • 32
  • 41
12

For PHP 5.6

FROM php:5.6-apache

RUN apt-get update && apt-get install -y \ 
libfreetype6-dev libjpeg62-turbo-dev \ 
libgd-dev libpng12-dev
RUN docker-php-ext-configure gd \ 
--with-freetype-dir=/usr/include/ \ 
--with-jpeg-dir=/usr/include/
RUN docker-php-ext-install gd

If still not working, can re-install the container.

docker rm <container id> 
docker-compose build --pull
docker-compose up
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Jin
  • 665
  • 1
  • 7
  • 15
  • In your above example of the contents of the Dockerfile, Is the path `/usr/include/` inside the container or on the host machine? I believe that a Dockerfile can only refer to paths within it? https://stackoverflow.com/questions/31448821/how-to-write-data-to-host-file-system-from-docker-container (thanks) – therobyouknow Jun 13 '18 at 16:22
  • Correct, the path is inside the container. – Jin Jun 19 '18 at 00:42
  • 3
    If you receive the error `Package 'libpng12-dev' has no installation candidate` ( which was dropped some time ago ) you can change `libpng12-dev` to `libpng-dev` to make it work – mastef Dec 18 '18 at 11:33
3

Updated Version PHP 7.4 + Apache:

Dockerfile

FROM php:7.4-apache

RUN apt-get update -y && apt-get install -y sendmail libpng-dev libfreetype6-dev libjpeg62-turbo-dev libgd-dev libpng-dev

RUN docker-php-ext-install pdo pdo_mysql 

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

RUN docker-php-ext-install gd

...
mleister
  • 1,697
  • 2
  • 20
  • 46
0

Add these Commands

docker-php-ext-configure gd --with-freetype --with-jpeg 
docker-php-ext-install -j$(nproc) gd 

Working full Dockerfile:

FROM php:7.4-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /src/

# Set working directory
WORKDIR /src

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg-dev \
    libwebp-dev \
    libxpm-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql exif pcntl

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

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN composer install --prefer-source --no-interaction

COPY . /src

RUN chmod 777 -R /src/storage

# Expose port 9000 and start php-fpm server
EXPOSE 9000

CMD ["php-fpm"]