【问题标题】:PHP unable to show jpg image, shows black screen with small rectangle insidePHP无法显示jpg图像,显示黑屏,里面有小矩形
【发布时间】:2019-07-23 13:44:06
【问题描述】:

试图在任何浏览器中打开一个 jpeg 图像,它不会显示,显示一个黑屏,其中有一个白色的小矩形。

我尝试在 php.ini 中设置错误显示并增加内存大小。 jpg 文件只有 219 KB。

ini_set('memory_limit', '16M');
ini_set('memory_limit', -1);

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

但是,没有弹出错误,并且无法在 IE、chrome 或 firefox 上打开。 我还在 MIME.types 下的 IIS 管理器下进行了检查,以检查扩展名“jpeg、jpg、jpe”是否已在此处列出并且它们是。以下是我的 PHP 代码:

$file = 'test.jpg';
header('Content-type: image/jpeg');
$image = file_get_contents($file);
echo $image; 
//readme($file) <-- also fails

我们将不胜感激任何有关这方面的指导。

【问题讨论】:

  • 同样的事情发生在我的项目中,成功后上传的图片提供了url,打开它时,它显示如上图。 .net 中的 API 构建和图像使用 okhttp 多部分从 Android 上传。知道为什么吗?

标签: php jpeg


【解决方案1】:

需要在第一行加分号

$file = 'test.jpg';
header('Content-type: image/jpeg');
$image = file_get_contents($file);
echo $image; 
//readme($file) <-- also fails

【讨论】:

  • 这是一个错字,在我的代码行中,我确实在末尾有分号,但我仍然只得到一个黑屏。
  • 请检查图片的路径,或者.htaccess config
  • 我复制了路径并将其复制并粘贴到 Windows 资源管理器中,然后它就打开了。我使用 agent ransack 搜索了 .htaccess 文件,并在 inetpub 中名为 kanboard 的子文件夹下找到了它,这是我们使用的任务板管理器应用程序。我们使用的是 Windows r2 2012 服务器,.htaccess 文件还会在哪里?
【解决方案2】:

发现了问题,在执行这段代码之前,不知何故有一个回声调用了一个空白空间,必须在最顶部使用ob_start(),在执行标头之前使用ob_end_clean(),以允许jpeg图像显示。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,解决了这个问题,通过在 header 之后添加下面的 ob clean while 循环

    $file = 'test.jpg';
    header('Content-type: image/jpeg');
    /*It will clean output buffer*/
    while (ob_get_level() && @ob_end_clean()) {
            ;
    }
    $image = file_get_contents($file);
    echo $image; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-26
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多