【问题标题】:imagecreatefromjpeg and similar functions are not working in PHPimagecreatefromjpeg 和类似的函数在 PHP 中不起作用
【发布时间】:2012-10-31 13:25:37
【问题描述】:

我已经对此进行了搜索,过去问题中提供的解决方案对我来说完全无法理解。每当我运行像imagecreatefromjpeg 这样的函数时,我都会得到这个:

致命错误:调用未定义的函数 imagecreatefromjpeg() ...

我正在安装新的 PHP;我上次安装从来没有这个问题。我不明白发生了什么。

【问题讨论】:

  • 是否启用/安装了 GD?
  • 确保您不仅安装了GD2,而且还激活了您的php.ini
  • 我该怎么做?

标签: php gd


【解决方案1】:

【讨论】:

  • 哎呀,该链接中的解决方案运行良好。我一直在摆弄这个并且无法让它工作,不知道它是如此简单。感谢您找到这个!
【解决方案2】:

在 Ubuntu/Mint(基于 Debian)中

$ sudo apt-get install php5-gd

【讨论】:

  • 和 /etc/init.d/apache2 重启
  • @ChrisV 仅当您使用 Apache 时。
  • 在 Ubuntu 14 中,imageantialias 缺失:bugs.launchpad.net/ubuntu/+source/php5/+bug/74647
  • 对于较新的版本,请使用 php-gd 或 php7.0-gd
  • 如果您使用的是 php-fpm,只需重新启动 php-fpm 就足够了。使用它的众多优点之一,因为 php-fpm 通常重启速度比 Web 服务器快得多。
【解决方案3】:

即使安装了GD,你仍然可以获得Fatal error: Call to undefined function imagecreatefromjpeg()

解决方案是安装/启用 jped lib:

Ubuntu 上:

    apt-get install libjpeg-dev
    apt-get install libfreetype6-dev

CentOS 上:

    yum install libjpeg-devel
    yum install freetype-devel

如果从源代码编译添加--with-jpeg-dir --with-freetype-dir--with-jpeg-dir=/usr --with-freetype-dir=/usr

更多详情请查看https://www.tectut.com/2015/10/solved-call-to-undefined-function-imagecreatefromjpeg/

【讨论】:

    【解决方案4】:

    您必须启用库 GD2。

    找到你的(正确的)php.ini 文件

    找到这一行:;extension=php_gd2.dll,去掉前面的分号。

    该行应如下所示:

    extension=php_gd2.dll
    

    然后重启 apache 就可以了。

    【讨论】:

    • 请注意,因为 php.ini 是一个大文件。此行位于Dynamic Extensions 类别下(大致在文档的中间)
    • 对于使用 PHP 8 或更高版本的任何人,请注意,它将改为 extension=gd
    【解决方案5】:

    如果您像我一样使用PHP Docker images 之一作为基础,则需要使用与上面讨论的不同的指令添加 gd 扩展。

    对于php:7.4.1-apache 图像,您需要在Dockerfile 中添加:

    RUN apt-get update && \
        apt-get install -y zlib1g-dev libpng-dev libjpeg-dev
    
    RUN docker-php-ext-configure gd --with-jpeg && \
        docker-php-ext-install gd
    

    这些开发包是编译 GD php 扩展所必需的。对我来说,这导致在 PHP 中使用 PNG 和 JPEG 支持激活 GD。

    【讨论】:

    • 谢谢保罗,它对我来说就像一个魅力! \o/ :rocket:
    • 是的,这就是解决方案
    【解决方案6】:
    sudo apt-get install phpx.x-gd 
    sudo service apache2 restart
    

    x.x 是 php 版本。

    【讨论】:

      【解决方案7】:

      对于 Ubuntu 上的 php 7:

      sudo apt-get install php7.0-gd

      【讨论】:

        【解决方案8】:

        安装php5-gd需要重启apache

        【讨论】:

          【解决方案9】:

          在 CentOS、RedHat 等中使用以下命令。不要忘记重新启动 Apache。因为必须加载 PHP 模块。

          yum -y install php-gd
          service httpd restart
          

          【讨论】:

            【解决方案10】:

            之后
            systemctl restart php-fpm
            Gd 库有效。

            【讨论】:

              【解决方案11】:

              如前所述,您可能需要安装 GD 库。

              在 shell 上,首先检查你的 php 版本:
              php -v

              然后相应地安装。在我的系统(Linux-Ubuntu)中,它是 php 版本 7.0:
              sudo apt-get install php7.0-gd

              重启您的网络服务器:
              systemctl restart apache2

              您现在应该已经安装并启用了 GD 库。

              【讨论】:

                【解决方案12】:

                在安装 PHP Version 8.0.11 之后,我花了很多时间试图在 Windows 上解决这个问题。

                我创建了这个phpinfo.php 文件来检查是否加载了GD:

                <?php
                if (extension_loaded('gd')) {
                  print 'gd loaded';
                } else {
                  print 'gd NOT loaded';
                }
                
                phpinfo(); ?>
                

                如果它返回gd NOT loaded,请确保在您的php.ini 文件中您未选中以下内容:

                ; - Many DLL files are located in the extensions/ (PHP 4) or ext/ (PHP 5+)
                ;   extension folders as well as the separate PECL DLL download (PHP 5+).
                ;   Be sure to appropriately set the extension_dir directive.
                ;
                extension=gd
                

                在 Windows 上,它们将成为extension_dir 的部分,您需要取消选中以下选项:

                ; Directory in which the loadable extensions (modules) reside.
                ; http://php.net/extension-dir
                ;extension_dir = "./"
                ; On windows:
                extension_dir = "ext"
                

                您现在应该在运行 phpinfo.php 文件时看到 gd loaded,并且任何函数调用(如 imagecreatefromjpeg())现在都应该按预期工作。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-11-17
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-10-08
                  相关资源
                  最近更新 更多