I am having trouble generating an image from PHP. My problem is that the file is generated perfectly with the structure I need, but when I open the image file I get the following message: "It seems that the format of this file is not supported." I am doing it with Laravel
I enclose code of how I generate the image and save it in the DB (the name of the image)
HTML:
<div class="col-md-5">
<div class="card-header">Firma</div>
<input type="color" class="js-color-picker color-picker">
<input type="range" class="js-line-range" min="1" max="72" value="1">
<label class="js-range-value">1</label>Px
<canvas class="js-paint paint-canvas" id="canvas" name="firma" width="400" height="300"></canvas>
<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">
</div>
Controller:
public function crearPaciente(Request $request){
$nombre = $request->get('nombre');
$apellidos = $request->get('apellidos');
$dni = $request->get('dni');
$fechaNacimiento = $request->get('fechaNacimiento');
$lugarNacimiento = $request->get('lugarNacimiento');
$direccion = $request->get('direccion');
$codigoPostal = $request->get('codigoPostal');
$telefonoFijo = $request->get('telefonoFijo');
$telefonoMovil = $request->get('telefonoMovil');
$compania = $request->get('compania');
//obtenemos la fecha de firma
$fecha = \Carbon\Carbon::now();
$fechaFinal = $fecha->toDateString();
$firma = $request->get('dataURL');
$this->generateImage($firma, $nombre, $apellidos);
$pacienteFirma = $nombre.$apellidos.".png";
$guardarFirma = str_replace(' ', '', $pacienteFirma);
$data = array('nombre'=>$nombre,
"apellidos"=>$apellidos,
"dni"=>$dni,
"fecha_nacimiento"=>$fechaNacimiento,
"lugar_nacimiento"=>$lugarNacimiento,
"direccion"=>$direccion,
"cod_postal"=>$codigoPostal,
"tlf1"=>$telefonoFijo,
"tlf2"=>$telefonoMovil,
"compania"=>$compania,
);
$consentimiento = array('fechaFirma' => $fechaFinal,
'firma' => $guardarFirma
);
\DB::table('pacientes')->insert($data);
\DB::table('lopds')->insert($consentimiento);
I capture the resquest of the canvas
$firma = $request->get('dataURL');
$this->generateImage($firma, $nombre, $apellidos);
function "generateImage"
public function generateImage($img, $nombre, $apellidos){
$cadenaNombre = $nombre.$apellidos;
$nombreApellidos = str_replace(' ', '', $cadenaNombre);
$path = "images/".$nombreApellidos.".png";
$imgTran1 = str_replace('data:image/png;base64,', '', $img);
$imgTran2 = str_replace(' ', '+', $img);
$data = base64_decode($imgTran2);
$success = file_put_contents($path, $data);
}
I have to say, that before transforming the image, the resulting canvas string is a base64 string, as you can see in the $imgTran1 variable.
Thanks in advance for the help, since I don't know what could be wrong so that the image is not displayed. When returning it in html again, it doesn't show either.
cheers and thank you again
The converted image has been used incorrectly, in the second conversion/replace the same initial image (
$img
) is used as the source, so itdata:image/png;base64
remains in the final image. (changes$img
to$imgTran1
)