-1

I have a Laravel application that is live where initially, all images are encoded into base64 strings and are stored normally on the database (I mean the encoded string), but right-now, I am encountering some speed issue, so in an attempt to optimize my application, I decided to converting all my base64 encoded strings that are stored in the database into a normal (png or jpeg) file in the Laravel storage and only store the file path to the database.

So my challenge in getting this to play is that I can really get to write a script that loops through all of these records (as they are over 1000 rows as of today) and perform that operation for them all.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kingsley Akindele
  • 311
  • 1
  • 2
  • 13
  • https://stackoverflow.com/questions/17810501/php-get-base64-img-string-decode-and-save-as-jpg-resulting-empty-image. It's a matter of removing the data part at the beginning, removing all spaces and doing a base64 decode. – user3532758 Aug 10 '20 at 08:01
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Aug 10 '20 at 10:28
  • A [note regarding begging](https://stackoverflow.com/questions/61296052/how-to-append-or-attach-a-pivot-relationship-to-a-model) was added to your previous question in April. – halfer Aug 10 '20 at 10:39

1 Answers1

1

Rough idea:

Generating random file name:

$filename = "prefix_".uniqid() . ".png";

Decode your base64 value:

$data = base64_decode($data);

Store it using Storage:

Storage::disk('public')->put($filename, $data);

Get URL and store it into your database:

Storage::disk('public')->path($filename);

Put these into your loop to save all the images.

erkeni
  • 45
  • 6