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 \yii\web\Response.
What I confused is like that:
create a controller and action to provide picture
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!
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With