Yii2: how to respond to an image and show to the browser?

the same work can be done using the following code:

header('Content-Type:image/jpeg'); readfile('a.jpg'); 

but now I'm really confused by Yii2 \yii\web\Response.


What I'm confused is as follows:

  • create a controller and action to provide the image

    See below

     class ServerController extends \yii\web\Controller { public function actionIndex($name) { // how to response } } 
  • Access http://example.com/index.php?r=server/index&name=foo.jpg

thanks for the answer!

+5
source share
5 answers

I do it like this. I added another function just for setting the headers. You can also move this function in the helper:

 $this->setHttpHeaders('csv', 'filename', 'text/plain'); /** * Sets the HTTP headers needed by file download action. */ protected function setHttpHeaders($type, $name, $mime, $encoding = 'utf-8') { Yii::$app->response->format = Response::FORMAT_RAW; if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE") == false) { header("Cache-Control: no-cache"); header("Pragma: no-cache"); } else { header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Pragma: public"); } header("Expires: Sat, 26 Jul 1979 05:00:00 GMT"); header("Content-Encoding: {$encoding}"); header("Content-Type: {$mime}; charset={$encoding}"); header("Content-Disposition: attachment; filename={$name}.{$type}"); header("Cache-Control: max-age=0"); } 

I also found how yii2 does it, look here (scroll down) https://github.com/yiisoft/yii2/blob/48ec791e4aca792435ef1fdce80ee7f6ef365c5c/framework/captcha/CaptchaAction.php

+3
source

Finally, I did this with the following codes:

 $response = Yii::$app->getResponse(); $response->headers->set('Content-Type', 'image/jpeg'); $response->format = Response::FORMAT_RAW; if ( !is_resource($response->stream = fopen($imgFullPath, 'r')) ) { throw new \yii\web\ServerErrorHttpException('file access failed: permission deny'); } return $response->send(); 
+12
source

in yii2, you can return a response object from the yii \ web \ Response class in action. so you can return your own answer.

for example, the displayed image in yii2:

 public function actionIndex() { \Yii::$app->response->format = yii\web\Response::FORMAT_RAW; \Yii::$app->response->headers->add('content-type','image/png'); \Yii::$app->response->data = file_get_contents('file.png'); return \Yii::$app->response; } 

FORMAT_RAW : data will be processed as response content without any conversion. No additional HTTP header will be added.

+4
source

Yii2 Method:

 Yii::$app->response->setDownloadHeaders($filename); 
+2
source

Yii2 already has a built-in function for sending files . Thus, you do not need to set the response format, and the type of content will be detected automatically (you can cancel it if you want):

 function actionDownload() { $imgFullPath = 'picture.jpg'; return Yii::$app->response->sendFile($imgFullPath); } 

..

If the file is temporarily created for the current upload action, you can use the AFTER_SEND event to delete the file:

 function actionDownload() { $imgFullPath = 'picture.jpg'; return Yii::$app->response ->sendFile($imgFullPath) ->on(\yii\web\Response::EVENT_AFTER_SEND, function($event) { unlink($event->data); }, $imgFullPath); } 
0
source

Source: https://habr.com/ru/post/1210474/


All Articles