Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

php - Troubles with Docker + PHP7 + GD resulting in "Call to undefined function imagecreatefromjpeg()"

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/
question from:https://stackoverflow.com/questions/47292221/troubles-with-docker-php7-gd-resulting-in-call-to-undefined-function-imagec

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...