Call to undefined function imagecreatefromjpeg() 解决方案
如果在代码中使用提示:imagecreatefrompng(): is not a valid PNG file
,表示需要安装jpeg库。
直接运行函数,提示:
[root@server gd]# php -r 'imagecreatefromjpeg();'
PHP Fatal error: Uncaught Error: Call to undefined function imagecreatefromjpg() in Command line code:1
Stack trace:
#0 {main}
thrown in Command line code on line 1
Fatal error: Uncaught Error: Call to undefined function imagecreatefromjpg() in Command line code:1
Stack trace:
#0 {main}
thrown in Command line code on line 1
或者检查:php --re=gd | grep jpeg
下载jpeg拓展库
[root@server ~]# wget http://www.ijg.org/files/jpegsrc.v9c.tar.gz
[root@server ~]# tar xvf jpegsrc.v9c.tar.gz
[root@server ~]# cd jpeg-9c/
编译jpeg库:
[root@server jpeg-9c]# ./configure --prefix=/usr/local/phpjpeglib --enable-shared --enable-static
进入php的gd库拓展目录:
[root@server ~]# cd /root/php-7.4.10/ext/gd
找到phpize路径:
一般在/usr/local/php/bin/phpize或者/usr/bin/phpize
[root@server gd]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902
找到php-config路径/usr/local/php/bin/php-config
在gd目录(非php源码根目录),只需要使用新的配置编译(关键点是!必须使用php-config选项):
[root@server ~]# cd /root/php-7.4.10/ext/gd
[root@server ~]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-jpeg=/usr/local/phpjpeglib --with-freetype=/usr/local/freetype
[root@server gd]# make && make install
以下表示按安装成功:
/bin/sh /root/php-7.4.10/ext/gd/libtool --mode=install cp ./gd.la /root/php-7.4.10/ext/gd/modules
cp ./.libs/gd.so /root/php-7.4.10/ext/gd/modules/gd.so
cp ./.libs/gd.lai /root/php-7.4.10/ext/gd/modules/gd.la
PATH="$PATH:/sbin" ldconfig -n /root/php-7.4.10/ext/gd/modules
----------------------------------------------------------------------
Libraries have been installed in:
/root/php-7.4.10/ext/gd/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
Don't forget to run 'make test'.
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20190902/
Installing header files: /usr/local/php/include/php/
最后在php.ini文件启用gd拓展(注意php.ini是不是你正在使用的ini),然后重启php-fpm,完成!
[root@server gd]# php --re=gd | grep jpeg
Entry [ gd.jpeg_ignore_warning <ALL> ]
Function [ <internal:gd> function imagecreatefromjpeg ] {
Function [ <internal:gd> function imagejpeg ] {
Function [ <internal, deprecated:gd> function jpeg2wbmp ] {
温馨提示
这里的不是全部重新编译,使用phpize,之前已经编译完成的模块等,不会受到一丁点影响。
验证码需要用到 freetype 所以gd的选项,要一起编译。
题外话
我centos 8安装的时候,总是不成功,后面鬼使神差在php目录编译了一下:[root@server gd]# ./configure --with-php-config=/usr/local/php/bin/php-config
然后再进入gd编译,可以了。。。
有一些环境,只能够通过重新编译php解决,暂时找不到原因,php7.4.10参考编译参数:
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --with-iconv-dir --with-zlib --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --with-gettext --disable-fileinfo --enable-opcache --with-pear --enable-maintainer-zts --with-ldap=shared --without-gdbm --enable-pcntl --enable-gd --enable-fileinfo --with-jpeg=/usr/local/phpjpeglib --with-freetype=/usr/local/freetype
哎,还是对编译安装不够精通。