繁体   English   中英

PHP imagecreatefromjpeg() 从命令行工作,从 HTTP 失败

[英]PHP imagecreatefromjpeg() works from command line, fails from HTTP

我正在尝试使用 PHP 脚本使用 GD 图像函数缩放目录中的所有 JPG 图像。 该脚本在我从命令行执行时有效,但在通过 HTTP 调用时无效。 日志记录表明它在调用imagecreatefromjpeg()时失败。 GD 似乎已安装并正确配置,因为对getimagesize()的调用成功。 出于同样的原因,它似乎是一个有效的图像文件。

非常感谢任何帮助。

相关代码(为了可读性删除了一些代码):

while (false !== ($entry = readdir($fh))) {
    $pathinfo = pathinfo($entry);
    if ('jpg' === strtolower($pathinfo[ 'extension' ])) {
        $log->info(sprintf('Processing file "%s"', $entry));
        $srcName = implode(DIRECTORY_SEPARATOR, [$config[ 'stageDir' ], $pathinfo[ 'basename' ]]);
        if ( ! file_exists($srcName)) {
            $log->warning(sprintf('File "%s" not found', $srcName));
            continue;
        }

        if ( false === ( $info = getimagesize($srcName))) {
            $log->warning('This file is not a valid image');
        }
        var_dump($info);
        echo imagecreatefromjpeg($srcName) . "\n";

        if (false === ($srcImg = imagecreatefromjpeg($srcName))) {
            $log->warning(sprintf('Cannot read image file "%s"', $srcName));
            continue;
        }
    }
}

命令行输出:

C:\Apache24\htdocs\kiosk\public>php convert.php
C:\Apache24\htdocs\kiosk\public\convert.php:75:
array(7) {
  [0] =>
  int(3072)
  [1] =>
  int(2304)
  [2] =>
  int(2)
  [3] =>
  string(26) "width="3072" height="2304""
  'bits' =>
  int(8)
  'channels' =>
  int(3)
  'mime' =>
  string(10) "image/jpeg"
}
Resource id #26

通过 HTTP 执行时的日志文件:

[2020-01-01 07:22:50] gallery.INFO: Processing file "IMG_2748.JPG" [] []
[2020-01-01 07:22:50] gallery.WARNING: Cannot read image file "../staging\IMG_2748.JPG" [] []

Windows 10、Apache 2.4、PHP 7.2 以下是来自 php -i 的 GD 信息:

gd

GD Support => enabled
GD Version => bundled (2.1.0 compatible)
FreeType Support => enabled
FreeType Linkage => with freetype
FreeType Version => 2.8.1
GIF Read Support => enabled
GIF Create Support => enabled
JPEG Support => enabled
libJPEG Version => 9 compatible
PNG Support => enabled
libPNG Version => 1.6.34
WBMP Support => enabled
XPM Support => enabled
libXpm Version => 30512
XBM Support => enabled
WebP Support => enabled

Directive => Local Value => Master Value
gd.jpeg_ignore_warning => 1 => 1

在 PHP.net 站点上函数文档的评论部分找到了解决方案 它没有解释为什么它可以从命令行运行而不是从 HTTP 请求运行,也没有解释函数调用失败的原因。 但是,它确实提供了一种解决方法:

$image = @ImageCreateFromJpeg($image_name);
if (!$image)
{
    $image= imagecreatefromstring(file_get_contents($image_name));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM