【问题标题】:output image via php problems通过php问题输出图像
【发布时间】:2014-10-30 17:49:14
【问题描述】:

我知道这可能是一个非常微不足道的问题,尽管我在过去的几个小时里一直在爬网,但我似乎无法解决这个问题..

我有一个 image_get.php 文件,它应该显示一个与 php 文件位于同一目录中的图像文件“keepcalm.png”。

我正在使用一个类来处理所有与图像相关的操作。

image_get.php

<?php
   include "classes/image.class.php"; 
   include "classes/database.class.php";
   $database_sso = new database('SSO'); //will be needed later and is dummy for now
   $testimage = new image($database_sso, "1"); //parameters are dummy for now
   $testimage->show_image();
?>

image.class.php

<?php

class image{
   private $database_image;     //Image Database containing all images
   private $imageid;            //ID of Image in Database
   private $filepath;           //Path to image file


//PUBLIC FUNCTIONS  
   public function __construct($database, $imageid){
     $this->database_image = $database;     //Right now this is just a placeholder
     $this->imageid = $imageid;         //Right now this is just a placeholder

   }

    public function show_image(){

    $remoteImage = $this->get_local_link_from_id($this->imageid);  // Right now this returns "keepcalm.png"

    header('Content-Type: image/x-png')
    $returnstring = readfile($remoteImage);

}
 //PRIVATE FUNCTIONS
private function get_local_link_from_id($imageid){

    $local_link = "keepcalm.png"; //dummy for now
    return $local_link;
}


}



?>

我得到的输出是完全乱码,可以在这里看到 -> http://niederrad.no-ip.org/portal/image_get.php

我错过了什么? 我已经尝试了上述的很多迭代,并且完全不知道应该如何进行..

【问题讨论】:

  • 您的带有 content-type 的标头不会发送到浏览器。这是函数,不是字符串!! header('Content-Type: image/x-png')。至少应该是$returnstring = readfile($remoteImage); header('Content-Type: image/x-png');
  • 你会怎么寄? $returnstring = header('Content-Type: image/x-png').readfile($remoteImage);返回$返回字符串;那行不通……
  • 我写的! $returnstring = readfile($remoteImage); header('Content-Type: image/x-png');你应该运行这个函数,它会生成http响应头,而不是返回它。
  • 嗯。如果我按该顺序执行此操作,则会收到“标头已发送”错误。如果我把 header('Content-Type: image/x-png');首先我只得到一个空的图像图标.. 见niederrad.no-ip.org/portal/image_get.php
  • 这意味着你的代码是一团糟,阅读这个(在发送标头之前应该没有输出到浏览器) - stackoverflow.com/questions/8028957/… 你打算从函数返回$returnstring吗? ??如果您不返回文件的内容或不从函数内将其发送到浏览器 - 将不会显示。

标签: php image readfile


【解决方案1】:

试试这个:

public function show_image() {

    // this returns "keepcalm.png"
    $remoteImage = $this->get_local_link_from_id($this->imageid);

    // Set Header
    header("Content-Type: image/png"); // More info: http://stackoverflow.com/a/2086406/2332336

    // Output the contents of image
    readfile($remoteImage);
    exit;
}

如果这对您不起作用,则意味着 PHP 无法找到本地图像 keepcalm.png 可能是因为这里不存在:http://niederrad.no-ip.org/portal/keepcalm.png(即来自脚本(image_get.php)执行位置http://niederrad.no-ip.org/portal/

如果是这种情况,您可能需要告诉readfile() 命令图像在您的服务器上的位置。我的意思是指定文件的绝对路径(例如在 linux 服务器 /home/USERNAME/public_html/images/ 上)

如果图像在远程服务器上(正如您的变量名称所暗示的那样),那么您的代码应如下所示:

    // Output the contents of image
    exit(file_get_contents('http://remote-site.com/path/to/'. $remoteImage));

假设您的 PHP 配置已将 allow_url_fopen 设置为 true。

【讨论】:

  • 感谢您的回答。不幸的是,您的建议是我最初尝试的第一件事,文件在这里niederrad.no-ip.org/portal/keepcalm.png -> 请尝试 URL。
  • 我已经用 file_exists() 检查了文件位置,这不是问题..
  • Alexander(下)在文件中对BOM 提出了一个很好的观点。您是否尝试过在Notepad++ 中打开文件并使用Encoding -&gt; Encode in ANSI 保存它,然后保存文件并重试?
【解决方案2】:

问题出在 BOM 上,我觉得这很奇怪,因为我确定我已经检查过不使用 BOM 进行编码...

How to fix "Headers already sent" error in PHP

来自 Cheery 的一个非常好的建议。

【讨论】:

    猜你喜欢
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2010-11-04
    • 1970-01-01
    相关资源
    最近更新 更多