提供图片以获取请求 [英] Serving Images to get a request

查看:205
本文介绍了提供图片以获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CodeIgniter rest控制器。我想要能够在客户端发出的GET请求上提供图像。

I am using CodeIgniter rest Controller. I want to be able to serve images on GET requests made by the client.

这是最好的选择,或者我应该提供一个链接到图像,让客户下载它自己?
如果我可以提供图像本身,比我应该怎么办?

Is this the best option or should i just provide a link to the image and let the client download it themselves? If I can serve the images itself, than how should I?

推荐答案

使CodeIgniter设置正确的标题,您可以在控制器中执行此操作:

To make CodeIgniter set the proper headers and such you could do this in your controller:

$this->output->set_content_type('jpeg')->set_output(file_get_contents('path_to_file'));

它会将图像内容作为文件输出(作为请求响应)。不需要视图。

And it will output (as request response) the image content as a file. No view required.

请注意,这是一个额外的开销,因为该文件是由PHP处理,而不只是web服务器(Apache / Nginx)。如果你需要在请求上有一些业务逻辑,比如日志记录或授权(尽管没有PHP也可以这样做),这只是有意义的。如果你只是输出图像,它是更好地链接到文件,并离开它的PHP。

Please note though that this is a bit of extra overhead as the file is processed by PHP instead of just the webserver (Apache/Nginx). It only makes sense if you need to have some business logic on the request, such as logging or authorization (though even that can be done without PHP). If you are just outputing the image it is therefor better to link straight to file and leave PHP out of it.

这篇关于提供图片以获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆