Reputation: 12373
In a PHP application I have images being served as such:
<img src="http://www.example.com/image/view/93029420" />
On the backend it does something like this:
$query = "SELECT img_url FROM images WHERE image_id = '93029420'";
$result = DB.findOne($query);
$contents = file_get_contents($result['img_url'] );
header('Content-Type: image/png');
echo $contents;
exit();
The image url being stored in the db is to a cdn such as amazon. So it might be like https://images.mybucket.s3.com/slippers.png . Would it be better display the image with that approach or use a redirect like:
$query = "SELECT img_url FROM images WHERE image_id = '93029420'";
$result = DB.findOne($query);
header('Location: ' $result['img_url'] );
exit();
Now the only reason I imagine this approach being better is because it this post less processing on the server to download the image. Would this be correct or incorrect and is there a better approach?
Upvotes: 3
Views: 122
Reputation: 26699
The point of CDN is to allow parallel downloads; also some CDNs enable downloading of the assets from the geographically nearest location. Your approach makes the CDN useless, as the user always download the image from your server, so you are using it just as a remote file storage.
In your current approach, user have to wait for your server to download the image, and then to download from your server. Usually it would be slower compared to user downloading from the remote server directly (assuming that your server does not have much faster connection both to the remote server and to the user)
If you want the file to be downloaded from your server, at least you can cache it, so it's not downloaded every time.
Upvotes: 1