Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading to S3 from Laravel Quality Lost

My application uploads images to s3. I use front-end rendering to get the colors of the image, but because uploading to s3 lowers the quality (jpeg'ish), I get more colors then desired.

enter image description here

$s3 = \Storage::disk('s3');
$s3->put('/images/file.jpg', '/images/file.jpg', 'public');

Is there way to prevent this quality loss? I noticed that if I upload the file directly using aws console website, the quality stays the same which is ideal.

Thank you!

like image 852
Matt Avatar asked Jun 27 '16 23:06

Matt


People also ask

What is the largest size file you can transfer to S3?

Individual Amazon S3 objects can range in size from a minimum of 0 bytes to a maximum of 5 TB. The largest object that can be uploaded in a single PUT is 5 GB. For objects larger than 100 MB, customers should consider using the Multipart Upload capability.

How many files can you upload to S3 at once?

Uploading Individual Files to S3 When you upload files to S3, you can upload one file at a time, or by uploading multiple files and folders recursively.


2 Answers

In the controller Action

public function uploadFileToS3(Request $request)
{
    $image = $request->file('image');
} 

Next we need to assign a file name to the uploaded file.You could leave this as the original filename, but in most cases you will want to change it to keep things consistent. Let’s change it to a timestamp, and append the file extension to it.

$imageFileName = time() . '.' . $image->getClientOriginalExtension();

Now get the image content as follows

$s3 = \Storage::disk('s3');
$filePath = '/images/file.jpg' . $imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');

For further info you can refer to this

like image 133
Nayantara Jeyaraj Avatar answered Sep 30 '22 08:09

Nayantara Jeyaraj


I am not familiar with Laravel but i am familiar with AWS S3, and i'm using aws-sdk-php.
As far as i know, neither AWS S3 nor php-sdk don't do something implicitly under the hood. So it must be something going wrong elsewhere in your project.
You can try use plain aws-sdk-php:

$s3 = S3Client::factory([
    'region'      => 'us-west-2',
    'credentials' => $credentials,
    'version'     => 'latest',
]);
$s3->putObject([
    'Bucket'     => 'myBucket',
    'Key'        => 'test/img.jpg',
    'SourceFile' => '/tmp/img.jpg',
]);

It works perfectly.

like image 27
cn007b Avatar answered Sep 30 '22 08:09

cn007b