简体   繁体   English

如何在Laravel中保存修改后的图像

[英]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. 我正在尝试在修改后的数据库中保存一张新照片,我有我的javascript(darkroomjs)用于裁剪照片,但新照片没有保存在数据库中。 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). 这将创建一个唯一的名称,并将其保存在您的storage/app而不是公共(这是您需要做的,而不是保存在public/目录本身)。

在此输入图像描述

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. 1)您可以通过对服务器进行ajax调用将此文件保存在服务器的临时文件夹中。

2) During you save other fields(POST request). 2)在保存其他字段(POST请求)期间。 You can simply move file from temp folder to your images folder and file name into DB. 您只需将文件从临时文件夹移动到图像文件夹,将文件名移动到DB即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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