title: How to cache images from url in Laravel
body: <p>I have a Laravel 4 application that displays images from another PHP application that is located on a remote server.</p>
  
  <p>I would like to know what is the best solution to cache the images from the remote server on my local Laravel App.</p>
  
  <p>Please note that it's <em>almost impossible</em> to copy/past the entire images directory from the remote server, since there are more than 150k images that are regurlary updated (every minutes) and there is a database correlation too (filename with column value in database).</p>
  
  <p>I was about to use <code>Rsync</code> to sync the two directories (distant with local), but I also need to resize images before displaying them and organize the images subdirectories in a different way than it is on the remote server.</p>
  
  <p>First I have installed the <a href="http://image.intervention.io/" rel="nofollow">Intervention Image Class</a> package on Laravel and I have created a <code>Route</code>:</p>
  
  <pre><code>Route::get('photos/item/{size}/{reference}/{photo}', 'ImgController@showImg');
  </code></pre>
  
  <p>And in the <code>ImgController</code>:</p>
  
  <pre><code>public function showImg( $size, $reference, $photo )
  {
      switch ( $size ) {
         case '300x225':
            $jpg = Image::make( 'http://myremoteapp.com/uploads/' . $reference . '/' . $photo )-&gt;resize( 300, 225 )-&gt;response( 'jpg' );
         break;
      }
      return $jpg;
  }
  </code></pre>
  
  <p>This works but it doesn't keep the images in the browser's cache and it also creates a performance issue, since the images have to be downloaded and resized every time the page is opened.</p>
  
  <p>I have heard about the <a href="https://github.com/Intervention/imagecache" rel="nofollow">Intervention Image Cache</a> but I am not sure if it works with images taken from URLs.   </p>
  
  <p>Any advice and suggestions on how to resolve this in a proper way would be greatly appreciated.</p>