gpt4 book ai didi

mysql - 通过 Laravel API 使用干预/图像上传/调整大小

转载 作者:行者123 更新时间:2023-11-29 05:50:09 25 4
gpt4 key购买 nike

我正在尝试使用公开创建的路径(即上传/图像)将图像上传到数据库,但不幸的是,它没有显示任何错误,并且图像没有上传或保存到目录中。

Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Image;

class ImagesController extends Controller
{
public function index()
{
return view('image');
}

public function upload(Request $request)
{
$this->validate($request, [
'images' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);

$image = $request->file('images');
$input['imagename'] = time() . '.' . $image->getClientOriginalExtension();

$destinationPath = public_path('/uploads/images');
$img = Image::make($image->getRealPath());
$img->resize(100, 100, function ($constraint) {
$constraint->aspectRatio();
})->save($destinationPath . '/' . $input['imagename']);

$destinationPath = public_path('/images');
$image->move($destinationPath, $input['imagename']);
$this->postImage->add($input);

if (!empty($input['imagename'])) {
return response()->json([
'success' => true,
'data' => $input['imagename']->toArray()
]);
}

return true;
}
}

路线

Route::post('upload', 'ImagesController@upload');

模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Image extends Model
{
protected $fillable = ['image'];
}

enter image description here

最佳答案

尝试采用这种风格然后运行

php artisan storage:link 

此外,将您的 Controller 更改为此

public function upload(Request $request)
{

$img = new Image();

$validation = $request->validate([
'title' => 'string',
'image' => 'required|file|image|mimes:jpeg,png,gif,webp|max:2048'
]);
$file = $validation['image']; // get the validated file
$extension = $file->getClientOriginalExtension();
$filename = 'mm-image-' . time() . '.' . $extension;
$path = $file->storeAs('/uploads/images', $filename);
$img->image = $request->image;
$img->title = $path;

dd($img);


if (!empty($img)){
return response()([
'success' => true,
'data' => $img->toArray()
]);;

}
}

关于mysql - 通过 Laravel API 使用干预/图像上传/调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55199205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com