[英]Silex cant display images as response
我无法将任何回复显示为图片。 我已经尝试过silex方法和纯PHP,像这样
$file = '1465797900_yoda.png';
if (!file_exists('images/'.$file)) {
return $app->abort(404, 'The image was not found.');
}
$stream = function () use ($file) {
readfile($file);
};
return $app->stream($stream, 200, array('Content-Type' => 'image/png'));
或纯PHP:
$file = 'images/1465797900_yoda.png';
$type = 'image/png';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
echo readfile($file);
如果我使用方法1,则响应为空白图片,但方法2返回随机轨迹,如下所示:
J ...'T |֢1 Kc Zu # ?9 G < !4# D xU á_PW Fܽ !rŨ%。 gs.u6.?0._o。> IEND.B`。
纯PHP不会有问题,只有Silex。 感谢帮助。
我写了以下内容,用于从文件夹中提供图片:
public function getpic( $id ) {
$path = '/assets/userpics/' . $id;
if ( is_file( $path ) ) {
return new BinaryFileResponse( $path );
}
//make some error stuff
}
在HTML中,您可以在其中调用图片使用或通过js加载图片
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.