通过php问题输出图像
我意识到这可能是一个很小的问题,虽然我已经花了几个小时在网上爬行,但我似乎无法正确解决 我有一个文件image_get.php,它应该显示一个图像文件keepcalm.png,该文件与php文件位于同一目录中 我使用一个类来处理所有与图像相关的操作 image_get.php通过php问题输出图像,php,image,readfile,Php,Image,Readfile,我意识到这可能是一个很小的问题,虽然我已经花了几个小时在网上爬行,但我似乎无法正确解决 我有一个文件image_get.php,它应该显示一个图像文件keepcalm.png,该文件与php文件位于同一目录中 我使用一个类来处理所有与图像相关的操作 image_get.php <?php include "classes/image.class.php"; include "classes/database.class.php"; $database_sso = new
<?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;
}
}
?>
我得到的输出完全是胡言乱语,如图所示->
我错过了什么?
我已经尝试了上述的多次迭代,但对于如何进行我完全不知道。试试这个:
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 i、 e.从脚本图像_get.php执行位置http://niederrad.no-ip.org/portal/
如果是这种情况,您可能必须告诉readfile命令映像在服务器上的位置。我的意思是指定linux服务器上文件的绝对路径/home/USERNAME/public\u html/images/例如
如果映像如变量名称所示位于远程服务器上,则代码应如下所示:
// Output the contents of image
exit(file_get_contents('http://remote-site.com/path/to/'. $remoteImage));
假设您的PHP配置已将allow\u url\u fopen设置为true。问题在于BOM,我发现这很奇怪,因为我确定我已选中不使用BOM编码
Cheery提供了一个非常好的建议。您的内容类型标题不会发送到浏览器。这是函数,不是字符串!!标题“内容类型:图像/x-png”。至少应为$returnstring=readfile$remoteImage;标题“内容类型:图像/x-png”;你怎么寄$returnstring=header'Content-Type:image/x-png'.readfile$remoteImage;return$returnstring;那不行…我写的$returnstring=readfile$remoteImage;标题“内容类型:图像/x-png”;您应该运行该函数,它将生成http响应头,而不是返回它。如果我按那个顺序做,我会得到一个headers ready sent错误。如果我把标题放在'Content-Type:image/x-png';首先,我只得到一个空的图像图标。。看,这意味着你的代码乱七八糟,阅读这篇文章,在发送标题之前,浏览器不应该有任何输出-你打算从函数返回$returnstring吗???如果您不返回文件内容或不将其从函数中发送到浏览器,则不会显示该文件。谢谢您的回答。不幸的是,你的建议是我最初尝试的第一件事,文件在这里->请尝试URL。我已经检查了文件位置,文件_存在,这不是问题。下面Alexander对文件上的BOM提出了一个很好的观点。您是否尝试过在记事本++中打开文件,使用ANSI中的编码->编码保存文件,然后保存文件并重试?