yii2: how to response image and let browser show it?

后端 未结 5 2082
攒了一身酷
攒了一身酷 2021-01-05 02:37

the same work can be done by follow code:

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

but now I really confused by Yii2\'s

5条回答
  •  滥情空心
    2021-01-05 02:49

    Finally, I did it by follow 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();
    

提交回复
热议问题