我正在尝试使用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网站上的功能文档的注释部分中找到a solution。它没有说明为什么它可以从命令行运行,但不能从HTTP请求中运行,也没有说明函数调用失败的原因。但是,它确实提供了一种解决方法。