I
I
Ilya Beloborodov2018-11-07 14:05:46
Yii
Ilya Beloborodov, 2018-11-07 14:05:46

How to save an image using AssetsManager?

There is this action

public function actionLoad($rest, $ext)
    {
        $response = Yii::$app->getResponse();
        $response->headers->set('Content-Type', 'image/' . $ext);
        $response->format = Response::FORMAT_RAW;
        $response->data = file_get_contents('http://example.com/' . $rest . '.' . $ext, true);
        return $response->send();
    }

Well, everything is clear here, I load a picture from another site and display it under my domain.
but when there are a lot of pictures, it takes a long time to load accordingly.
Question: is it possible to save these pictures in /web/assets and display if the picture with the same name is there? only crutches come to mind. Is there a normal implementation?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Beloborodov, 2018-11-07
@kowap

in short, so

$file = Yii::$app->cache->getOrSet($rest . '.' . $ext, function () use ($rest, $ext){
                return file_get_contents('http://example.com/' . $rest . '.' . $ext, true);
            });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question