Laravel and Cloudfront: Image caching issue - laravel

If I load this image from Cloudfront into my browser it shows me the correct and latest version:
https://d8z37rm9cmbl3.cloudfront.net/1/groupings/extras/27/FS_27_0003.JPG
However, when I load it programmatically in my Laravel app from my Forge server, it shows an old cached version of the image (which in fact no longer exists on AWS). The call is exactly the same within the program and the app's URL is:
http://imagetester.artlookonline.com/img
The code for the procedure is very simple and uses Intervention Image (but not their cache):
public function serveDirectImage() {
$img = Image::make('https://d8z37rm9cmbl3.cloudfront.net/1/groupings/extras/27/FS_27_0003.JPG');
// create response and add encoded image data
$response = Response::make($img->encode('jpg', 70));
// set content-type etc header
$response->header('Content-Type', 'image/png')
->header("Cache-control", "no-store");
// output
return $response;
}

Related

Laravel Api Postman Upload Image Return Null

$files = $request->file('images'); // return {}
$_FILES['images']; // return {"name":"sample-passport.jpg","type":"image\/jpeg","tmp_name":"D:\\xampp\\tmp\\php4AD9.tmp","error":0,"size":264295}
Have you tried to remove the Content-Type header? According to this Github issue, it seems to be a problem.
So, I set up a new Laravel installation to test this out and it's working fine on my side. Of course, there's no authorisation whatsoever but this shouldn't impact the result too much.
routes/api.php
Route::post('/profile/upload_image', function (Request $request) {
dd($request->file('image'));
});
Postman configs
Your post input attribute type change file after upload you will get a response.enter image description here

RuntimeException laravel 5.8 RuntimeException This driver does not support creating temporary URLs

RuntimeException This driver does not support creating temporary URLs.
I am trying to generating Temp Url for every request Laravel version 5.8 below code trying showing an error.
This driver does not support creating temporary URLs.
$url = "66.jpeg";
$url = Storage::disk('public')->url($url);
$url = Storage::disk('public')->temporaryUrl(
'66.jpeg', now()->addMinutes(5)
);
From my knowledge, temporaryUrl is a method used on a drivers such as s3 to create a temporary url for a privately stored asset.
If you would like to set a temporary url for a file, it may help to use Cache to temporarily store the path.
Cache can set a key/value for a set amount of time. A url can be create which links to an endpoint. Then endpoint can then be created which returns the contents of that file:
// Creating temp file index in cache
$image = '66.jpg';
Cache::put('/temp/' . $image, 300); // 5 minutes
Now in, for example, TempController.php (visiting http://example.com/temp/66.jpg):
public function show($image)
{
if (Cache::get('/temp/' . $image) && ! Storage::disk('public')->exists($image)) {
// not in cache or do not exist, maybe redirect...
};
return Storage::disk('public')->get($image);
}
This is a proof of concept however I hope this helps.

Get mime type of image from storage?

I'm getting a file from storage using:
$image = Storage::get('test.jpg');
How can I get it's mime type?
I've tried:
$mime = Storage::mimeType($image);
With no luck.
I need the mime type so I can return the image as a response:
$response = Response::make($image, 200);
$response->header('Content-Type', $mime);
return $response;
When using the Storage facade in a controller, you can return the image as a response in Laravel 5.7. The response() function will automatically set the headers required rather than attempting to do it yourself.
// Option #1
return Storage::response('test.jpg'); // Local storage
// Option #2
return Storage::disk('s3')->response('test.jpg'); // If you are using an S3 driver
// Option #3
return Storage::cloud()->response('test.jpg'); // Also S3 driver unless otherwise configured
This is not documented, but can be found by looking at the underlying Laravel code:
/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php
If you still need to get the mime type for any reason, you should be able to retrieve it by calling:
$mimeType = Storage::mimeType('test.jpg');

Laravel Multiple Image Issue

I need to download multiple images in a Laravel response download function without Zipper because I need to show images on a new page in Angular. Thanks.
If I understand you correctly you want images served by server?
public function get($path)
{
$file = Storage::disk('s3')->get($pathToImage);
return response($file, 200)->header('Content-Type', 'image/png');
}

Adding watermark on image from databases in Laravel 5.3

I am trying to add watermark on image from database in Laravel using Intervention Image package. In my database table I am saving the path of the image. I am using Accessors in my model to get access to the field of the image path but I am getting this error:
Method insert does not exist.
Here is my model:
Here is my blade:
public function getFilePathAttribute($value){
$img = Image::make(public_path($value)); //your image I assume you have in public directory
$img->insert(public_path('watermark.png'), 'bottom-right', 10, 10); //insert watermark in (also from public_directory)
$img->save(public_path($value)); //save created image (will override old image)
return $value; //return value
}
It is better to do it on upload so you do it Once not always when trying to access the image path from DB (less proccess) FYI: this will save already watermarked image

Resources