Laravel-medialibrary: Add base64 support for `addMedia`

Created on 5 Jun 2016  路  15Comments  路  Source: spatie/laravel-medialibrary

Most helpful comment

I'd accept a PR that adds a addMediaFromBase64 to the package. You can some cues from the addMediaFromUrl method. Please make sure you include a test 馃槃

All 15 comments

Please provide some info on what you'd expect the medialibrary to do. If possible add some code that you'd like to work and state why you need this.

I'm using cropit in my project and with this plugin users pick a photo and crop it.
This plugin create base64 code for this image also other popular similiar tools do the same.
So i should save this image to my media library easily but i cant, only i can do is decode this code and after save it with file_put_contents function

Here is my quick solution for this. I'm calling saveFileFromBase64() method when checking upload from request if base64 found.

/**
 * Get file content from base64 string.
 *
 * @param $data
 * @return string
 */
private function getBase64Content($data)
{
    list($type, $data) = explode(';', $data);
    list(, $data) = explode(',', $data);
    $data = base64_decode($data);

    return $data;
}

/**
 * Saves temporary file from base64 string.
 *
 * @param $filename
 * @param $data
 * @return File
 */
private function saveFileFromBase64($filename, $data)
{
    $data = $this->getBase64Content($data);
    file_put_contents($filename, $data);
    $file = new File($filename);

    return $file;
}

I'd accept a PR that adds a addMediaFromBase64 to the package. You can some cues from the addMediaFromUrl method. Please make sure you include a test 馃槃

@freekmurze it should be placed in addMediaFromUrl because v3 don't have addMediaFromRequest method or there are any other reasons to place it there? Base64 is not an URL and will come from request. Or you just meant to make one more public extra method to work only with base64 images?

I have problem with picture extensions because before sending my picture to AddMedia i should save it to somewhere and i think i should use temporary folder for this.But i cant save temporary file with extension(not sure).

$tmpFile = tempnam(sys_get_temp_dir(), 'media-library');
file_put_contents($tmpFile, $stream);
$filename = basename(parse_url($url, PHP_URL_PATH));

This code snippet from addMediaFromUrl function in this example you get extension from url but if i save my picture to temporary folder i cant send you a image path with extension for this media library save pictures without extensions.

We should find best way to do this.
And i thought maybe we can use this code in AddMedia i find this code from this package.
https://github.com/Intervention/image

/**
 * Determines if current source data is base64 encoded
 *
 * @return boolean
 */
public function isBase64()
{
    if (!is_string($this->data)) {
        return false;
    }
    return base64_encode(base64_decode($this->data)) === $this->data;
}

@taliptako You can get an extension from example I've provided above:

list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
list(, $type) = explode(':', $type);
list(, $extension) = explode('/', $type);
$data = base64_decode($data);
  • $data - file content.
  • $extension - file extension.
  • $type - file mime/type.

But there will be some issues with files which has mime/type like application/octet-stream or other non concrete types.

@taliptako I've updated an example. But I think there could be better ways to get file extensions already implemented in Intervention image library.

@a-komarev Thank you i will use your example until the new feature.

@taliptako Do you plan to make a PR for this feature?

@a-komarev Actually no :) Should i close the issue ?

@taliptako :sob:

I'll close this for now. Reopen if somebody would like to work on a PR for this.

This has now been implemented and successfully merged btw :-).

Yup, functionality was added to v5.2

Was this page helpful?
0 / 5 - 0 ratings