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 :)