src/HasMedia/HasMediaTrait.php
public function addMediaFromBase64(string $base64data, ...$allowedMimeTypes): FileAdder
    {
        // strip out data uri scheme information (see RFC 2397)
        if (strpos($base64data, ';base64') !== false) {
            [$_, $base64data] = explode(';', $base64data);
            [$_, $base64data] = explode(',', $base64data);
        }

        // strict mode filters for non-base64 alphabet characters
        if (base64_decode($base64data, true) === false) {
            throw InvalidBase64Data::create();
        }

        // decoding and then reencoding should not change the data
        if (base64_encode(base64_decode($base64data)) !== $base64data) {
            throw InvalidBase64Data::create();
        }

        $binaryData = base64_decode($base64data);

        // temporarily store the decoded data on the filesystem to be able to pass it to the fileAdder
        $tmpFile = tempnam(sys_get_temp_dir(), 'medialibrary');
        file_put_contents($tmpFile, $binaryData);

        $this->guardAgainstInvalidMimeType($tmpFile, $allowedMimeTypes);

        $file = app(FileAdderFactory::class)->create($this, $tmpFile);

        return $file;
    }
public function addMediaFromBase64(string $base64data, ...$allowedMimeTypes): FileAdder
    {
        // strip out data uri scheme information (see RFC 2397)
        if (strpos($base64data, ';base64') !== false) {
            [$_, $base64data] = explode(';', $base64data);
            [$_, $base64data] = explode(',', $base64data);
        }

        // strict mode filters for non-base64 alphabet characters
        if (base64_decode($base64data, true) === false) {
            throw InvalidBase64Data::create();
        }

        // decoding and then reencoding should not change the data
        if (base64_encode(base64_decode($base64data)) !== $base64data) {
            throw InvalidBase64Data::create();
        }

        $binaryData = base64_decode($base64data);

        // temporarily store the decoded data on the filesystem to be able to pass it to the fileAdder
        $tmpFile = tempnam(sys_get_temp_dir(), 'medialibrary');
        file_put_contents($tmpFile, $binaryData);

        $this->guardAgainstInvalidMimeType($tmpFile, $allowedMimeTypes);

        $file = app(FileAdderFactory::class)->create($this, $tmpFile);

        return $file;
    }
public function addMediaFromBase64 ( string $ base64data , ... $ allowedMimeTypes ) : FileAdder { // strip out data uri scheme information (see RFC 2397) if ( strpos ( $ base64data , ';base64' ) !== false ) { [ $ _ , $ base64data ] = explode ( ';' , $ base64data ) ; [ $ _ , $ base64data ] = explode ( ',' , $ base64data ) ; } // strict mode filters for non-base64 alphabet characters if ( base64_decode ( $ base64data , true ) === false ) { throw InvalidBase64Data :: create ( ) ; } // decoding and then reencoding should not change the data if ( base64_encode ( base64_decode ( $ base64data ) ) !== $ base64data ) { throw InvalidBase64Data :: create ( ) ; } $ binaryData = base64_decode ( $ base64data ) ; // temporarily store the decoded data on the filesystem to be able to pass it to the fileAdder $ tmpFile = tempnam ( sys_get_temp_dir ( ) , 'medialibrary' ) ; file_put_contents ( $ tmpFile , $ binaryData ) ; $ this -> guardAgainstInvalidMimeType ( $ tmpFile , $ allowedMimeTypes ) ; $ file = app ( FileAdderFactory :: class ) -> create ( $ this , $ tmpFile ) ; return $ file ; }
Add a base64 encoded file to the medialibrary.

@param string $base64data
@param string|array ...$allowedMimeTypes

@throws InvalidBase64Data
@throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded

@return \Spatie\MediaLibrary\FileAdder\FileAdder
Add a base64 encoded file to the medialibrary .