简体   繁体   中英

How can I save a modified image in Laravel

I'm trying to save a new photo in database which is modified, I have my javascript ( darkroomjs) for cropping photos, but the new photo doesn't save in database. I'd like to save my new photo instead of the original photo.

$profile_images = $request['profilefiles'];
$profile_images = explode(";;", $profile_images);
array_shift($profile_images);
$image = "";

foreach ($profile_images as $key => $value) {
    $image_parts = explode(";base64,", $value);
    $image_type_aux = explode("image/", $image_parts[0]);
    $image_type = $image_type_aux[1];
    $image_base64 = base64_decode($image_parts[1]);

    $destinationPath = public_path('images/model/');
    $hardPath = str_random(10) . '.' . $image_type;
    $filename = $destinationPath . $hardPath;
    file_put_contents($filename, $image_base64);

    $image = $hardPath;
}

$model->title = $request['title'];
$model->slug = Slugify::slugify($request['title']);
$model->phone = $request['phone'];
$model->external_link = $request['external_link'];
$model->email = $request['email'];
$model->description = $request['description'];
$model->category = $request['category'];
$model->instagram = $request['instagram'];
$model->category_id = $request['category_id'];
$model->badges_id = $request['badges_id'];
$model->height = $request['height'];
$model->boost = $request['boost'];
$model->waist = $request['waist'];
$model->hips = $request['hips'];
$model->shoes = $request['shoes'];
$model->hair = $request['hair'];
$model->eyes = $request['eyes'];
$model->dress = $request['dress'];
$model->publish = $publish;
$model->age = date('Y-m-d', strtotime($request['dob']));
$model->metatitle = $request['title'];
$model->metadescription = substr(strip_tags($request['description']), 0, 160);

if ($image != "") {
    var_dump($image);
    $model->image = $image;
}
$model->upload_pdf = $upload_pdf;
$model->save();

You can simply

$path = Storage::put('images/model', $image_base64);

This will create you a unique name and save it in your storage/app rather than public (which is what you need to do rather than saving in public/ directory itself).

在此输入图像描述

Above image shows callback to get edited image.

1) You can save this file somewhere in temp folder at server by making ajax call to server.

2) During you save other fields(POST request). You can simply move file from temp folder to your images folder and file name into DB.

Ref : https://github.com/MattKetmo/darkroomjs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM