Ich sende eine PNG-Bilddatei über Ajax an den Controller in base64. Ich habe bereits getestet und bin sicher, dass der Controller eine ID erhalten hat, diese aber nicht in einem öffentlichen Ordner speichern kann.
Hier ist mein Controller
public function postTest() {
$data = Input::all();
//get the base-64 from data
$base64_str = substr($data->base64_image, strpos($data->base64_image, ",")+1);
//decode base64 string
$image = base64_decode($base64_str);
$png_url = "product-".time().".png";
$path = public_path('img/designs/' . $png_url);
Image::make($image->getRealPath())->save($path);
// I've tried using
// $result = file_put_contents($path, $image);
// too but still not working
$response = array(
'status' => 'success',
);
return Response::json( $response );
}
Intervention Image erhält binäre Daten mithilfe der file_get_content-Funktion: Referenz: http://image.intervention.io/api/make
Ihr Controller sollte so aussehen:
public function postTest() {
$data = Input::all();
$png_url = "product-".time().".png";
$path = public_path().'img/designs/' . $png_url;
Image::make(file_get_contents($data->base64_image))->save($path);
$response = array(
'status' => 'success',
);
return Response::json( $response );
}
Dies ist ein einfacher Fehler.
Sie verwenden public_path nicht korrekt. Es sollte sein:
$path = public_path() . "/img/designs/" . $png_url;
Ich würde auch Ihre Methode zum Senden des Bildes vermeiden. Sehen Sie sich den richtigen Upload in einem Formular an und verwenden Sie die Methode Input :: file von Laravel.
$data = Input::all();
$png_url = "perfil-".time().".jpg";
$path = public_path() . "/img/designs/" . $png_url;
$img = $data['fileo'];
$img = substr($img, strpos($img, ",")+1);
$data = base64_decode($img);
$success = file_put_contents($path, $data);
print $success ? $png_url : 'Unable to save the file.';
$file = base64_decode($request['image']);
$folderName = 'public/uploads/';
$safeName = str_random(10).'.'.'png';
$destinationPath = public_path() . $folderName;
$success = file_put_contents(public_path().'/uploads/'.$safeName, $file);
print $success;
Meine Lösung ist:
public function postTest() {
$data = Input::all();
//get the base-64 from data
$base64_str = substr($data->base64_image, strpos($data->base64_image, ",")+1);
//decode base64 string
$image = base64_decode($base64_str);
Storage::disk('local')->put('imgage.png', $image);
$storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
echo $storagePath.'imgage.png';
$response = array(
'status' => 'success',
);
return Response::json( $response );
}
Ich habe es geschafft !!
Ich ersetzte $data->base64_image
in $_POST['base64_image']
und verwende dann $result = file_put_contents($path, $image);
Anstelle von Image::make($image->getRealPath())->save($path);
Aber das sieht nicht nach einem großen Weg aus. Ich habe einen anderen Weg, der eleganter aussieht, bitte sag es mir!
Tatsächlich gibt Input::all()
eine array
von Eingaben zurück, so dass Sie Folgendes haben:
$data = Input::all();
Jetzt ist Ihr $data
ein Array, kein Objekt. Sie versuchen also, als Bild auf das Bild zuzugreifen:
$data->base64_image
Also funktioniert es nicht. Sie sollten versuchen mit:
$image = $data['base64_image'];
Da es (base64_image
) von $_POST
aus zugänglich ist, funktioniert Input::file('base64_image')
nicht, da Input::file('base64_image')
das $_FILES
-Array überprüft und in Ihrem Fall nicht vorhanden ist.
was mache ich?
$file = base64_decode($request['profile_pic']);
$folderName = '/uploads/users/';
$safeName = str_random(10).'.'.'png';
$destinationPath = public_path() . $folderName;
file_put_contents(public_path().'/uploads/users/'.$safeName, $file);
//save new file path into db
$userObj->profile_pic = $safeName;
}