I’m trying to store a base64 image from a Signature field plugin (the plugin outputs a base64 text). But before i can store it to the private folder, i have to first convert it as a png image. This is how i normally do it if i just upload an image:
// Signature Field on FormBuilder $form['signature'] = [ '#type' => 'managed_file', '#title' => $this->t('Signature'), '#upload_location' => 'private://requestformfiles', '#upload_validators' => [ 'file_validate_extensions' => ['png jpg',], ], ]; // Pre-saving $image = $form_state->getValue('signature'); $file = File::load( $image[0] ); $file->setPermanent(); $file->save();
But with a base64 image, i have to do this first:
$image = $form_state->getValue('signature'); $data = base64_decode(preg_replace('#^data:image/w+;base64,#i', '', $image)); file_put_contents('storage/signature.png', $data);
My problem is with file_put_contents
. The save location is usually done in '#upload_location'
, but i don’t know how to do this in file_put_contents
.
Any help is appreciated!