Reputation: 431
I have an image that I get in the form of a base64 string from an API response. Initially - I was saving the string directly in my database in a mediumtext
field. But it seems to be eating up too much space.
Is there a way I could save the image as a flat file - store it's link in my image field in the database - and retrieve it using a query again ?
I'm using PHP on the server side - and AWS. I need this as I'm running out of DB space.
Also - I know we could save a base64 string by decoding it to an image and saving it on the server - but how can I retrieve and encode the same?
Upvotes: 0
Views: 1250
Reputation: 2307
Take this, using pair functions base64_encode/base64_decode:
$encoded = 'SomeBase64EncodedString';
$decoded = base64_decode($encoded);
// Now $decoded is the binary content of image, save it anywhere you want.
// Example: file_put_content('/tmp/image.png', $decoded);
// When you want to retrieve the base64 encoded string again
$decoded = file_get_contents('/tmp/image.png');
$encoded = base64_encode($decoded);
// Now you have $encoded, as a base64 encoded string. Do as you please with it.
Upvotes: 1