Transforming image upload to Base64 - Standard

Asked

Viewed 1,252 times

0

I’m having trouble getting the data from my View and turns it into the base 64, in the controller I’m doing like this:

 public function store(ImagemRequest $resquest){
    dd($resquest->all()); //Nest dd aparece ex0.1
    $file = base64_encode($resquest->file('BLOB'));

    dd($file);
    $imagem = Produto::create([
        'CdProduto' => $resquest->CdProduto,
        'NmImagem' => $resquest->NmImagem,
        'DscImagem' => $resquest->DscImagem,
        'BLOB' => $file,
        'FlgPrincipal' => $resquest->FlgPrincipal
    ]);

    session()->flash('flash_message', 'Imagem salva com sucesso');

    $imagens =  Imagem::where('CdProduto', $resquest->CdProduto);
    $produto = Produto::find($resquest->CdProduto);


    if(Request::wantsJson()){
        return $imagem;
    }else{
        return view('Imagem.listImagem', compact('imagens', 'produto'));
    }
}

Example

array:6 [▼
"_token" => "RiIW0uXbqk1cZ5FGSKTZzQHlDtUoK1sSN2ImHjfW"
  "CdProduto" => "1"
  "NmImagem" => "eqweqwe"
  "DscImagem" => "qweqwe"
  "FlgPrincipal" => "0"
  "BLOB" => "12822161_1045978208829439_1942486736_n.jpg"
]

My form looks like this:

    {!! Form::open(['route' => 'imagens.store', 'id' => 'imagens-form']) !!}
        <div class="box-body">
{!! Form::text('CdProduto', $produto->CdProduto, ['class' => 'form-control hidden']) !!}
<div class="form-group">
    {!! Form::label('nmTipoProduto', 'Nome da imagem') !!}
    {!! Form::text('NmImagem', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
    {!! Form::label('dscTipoProduto', 'Descrição da imagem') !!}
    {!! Form::text('DscImagem', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
    {!! Form::label('flgPrincipal', 'Principal ?') !!}
    {!! Form::radio('FlgPrincipal', '1', false, ['class' => 'form-control minimal']) !!}
    {!! Form::label('flgPrincipal', 'Sim') !!}
    {!! Form::radio('FlgPrincipal', '0', true, ['class' => 'form-control minimal']) !!}
    {!! Form::label('flgPrincipal', 'Não') !!}
</div>
<div class="form-group">
    {!! Form::file('BLOB') !!}
    <img id="blah" src="#" alt="" />
</div>
<br>
<div class="form-group">
    {!! Form::submit($submitButton, ['class' => 'btn btn-success']) !!}
    {!! link_to_route('imagens.index', 'Voltar', $produto->CdProduto, ['class' =>  'btn btn-warning' ]) !!}
</div>
</div>     
{!! Form::close()  !!}

I want to save my image in Base64 but in the field Blob is only coming the name.

1 answer

2


To turn a file of any kind, coming from a input type file using the classe Request present in the

public function store(Request $request) 
{
    $file = $request->file('BLOB');
    $name = $file->getPathName();
    $mime = \Storage::mimeType($name);
    $file = base64_encode(file_get_contents($name));
    $src  = 'data: '.$mime.';base64,'.$file;
}

the value of the variable $src is what you need. In your case as is an image to make it easy to open this image as it was placed in the code.

Observing: If the image is fixed in the extent jpg:

$src = 'data: image/jpeg;base64,'.$file;

To load the image:

<img src="<?php echo $src; ?>" border="0" />

References

  • Since you are using Laravel, it would be nice to use the function of the framework itself to get mimetype. Storage::mimeType($filename); because the people at Zend like to keep changing the name methods, so if the change occurs, just update the framework.

  • @Pedrosoares, truth, the edition has been added! correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.