簡體   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