0

What is the best way to load images via a controller in laravel

Hey Everyone, I'm working on a function to save all images from an external source, to reduce their size and eventually show them again via a new link. Everything works perfectly, only I asked myself if it was possible to load the images faster from the Laravel application. The moment I load it from the external source it takes 60 milliseconds and when it is loaded from the Laravel application 400 milliseconds.

public function index($id)
{
    $filelocation = 'img/';

    if (file_exists($filelocation . '' . $id)) {
        $im = file_get_contents($filelocation . '' . $id);
        header('content-type: image');
        echo $im;
    } else {

        define('WEBSERVICE', 'http://api.resmush.it/ws.php?img=');
        $s = 'http://www.example.com/hires/' . $id;
        $o = json_decode(file_get_contents(WEBSERVICE . $s));

        if (isset($o->error)) {
            die('Error');
        }
        $ch = curl_init($o->dest);
        $fp = fopen($filelocation . '' . $id, 'wb');
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);
        // Load image
        $im = file_get_contents($filelocation . '' . $id);
        header('content-type: image');
        echo $im;

    }

Is there a good way to speed up the application, without using a more powerful server? (

All feedback is welcome :)

Daansk44
  • 497
  • 2
  • 4
  • 21
  • I tried a similar solution and after some testing I gave it up. The process of opening/reading file is way too demanding. I don't think this can be speeded up. Can't you just link to the source file? – Peter Matisko Dec 23 '18 at 20:40
  • Some additional information: https://stackoverflow.com/questions/30682421/how-to-protect-image-from-public-view-in-laravel-5 – Peter Matisko Dec 23 '18 at 20:43
  • `Thank you for your response`. The problem is that it needs to be loaded directly from this server. So we use the `controller` to do this. It checks if the file exists and if not it will load it to the server. – Daansk44 Dec 23 '18 at 20:45
  • One more link. The selected best answer is the best solution in my opinion: https://laracasts.com/discuss/channels/general-discussion/how-to-protect-images-and-files – Peter Matisko Dec 23 '18 at 20:45
  • 1
    Do you need the file to be protected with login? – Peter Matisko Dec 23 '18 at 20:46
  • No, but it needs to check if it is downloaded in the first place (from the external source). So it is loaded from the Laravel application. And you have to be able to load your image with the IMG tag `https://application.example.nl/serve-image/load/10000200_SR_Y2.jpg` – Daansk44 Dec 23 '18 at 20:49
  • 1
    Try this: Image::make($pathToFile)->response() and check the loading time, it should be faster than your original solution. – Peter Matisko Dec 23 '18 at 20:54
  • I change it to your code. It went from 400 milliseconds to 363. At least something :) – Daansk44 Dec 23 '18 at 21:09
  • Sorry, I expected more :) I think it opens the file in a similar way than your code. Think of using symbolic links. – Peter Matisko Dec 23 '18 at 21:11

0 Answers0