更新页面 - 根据视频 ID 更改图像名称 Laravel
Update page - Change image name follow video id Laravel
我想根据 Laravel 项目中的视频 ID 更改图像名称。如何解决?
这里是控制器的代码
$old_video = Video::find($id);
//video thumbnail uploaded
if ($request->hasFile('image_path') != '') {
$video_themnull = $request->file('image_path');
$video_themnull_name = uniqid() . Str::random('10') . '.' . $video_themnull->getClientOriginalExtension();
$video_themnull_resize = Image::make($video_themnull->getRealPath());
$video_themnull_resize->resize(400, 200);
if ($video_themnull->isValid()) {
if (isset($old_video->image_path)) {
$files_old = $old_video->image_path;
if ( file_exists($files_old)) {
unlink($files_old);
}
}
$video_themnull_resize->save(public_path('video/themnull/' . $video_themnull_name));
$image_path = 'public/video/themnull/' . $video_themnull_name;
}
}
在您的代码中
$video_themnull_name = uniqid() . Str::random('10') . '.' . $video_themnull->getClientOriginalExtension();
您创建了随机文件名,如果您想使用您的文件名添加视频 ID,那么您应该将 $old_video->id 添加到您的文件名创建中。所以请像下面这样重写上面的代码
$video_themnull_name = $old_video->id. Str::random('10') . '.' . $video_themnull->getClientOriginalExtension();
并在 if ($video_themnull->isValid()) condition
中添加以下代码
old_video->image_path = $image_path;
$old_video->save();
我想根据 Laravel 项目中的视频 ID 更改图像名称。如何解决?
这里是控制器的代码
$old_video = Video::find($id);
//video thumbnail uploaded
if ($request->hasFile('image_path') != '') {
$video_themnull = $request->file('image_path');
$video_themnull_name = uniqid() . Str::random('10') . '.' . $video_themnull->getClientOriginalExtension();
$video_themnull_resize = Image::make($video_themnull->getRealPath());
$video_themnull_resize->resize(400, 200);
if ($video_themnull->isValid()) {
if (isset($old_video->image_path)) {
$files_old = $old_video->image_path;
if ( file_exists($files_old)) {
unlink($files_old);
}
}
$video_themnull_resize->save(public_path('video/themnull/' . $video_themnull_name));
$image_path = 'public/video/themnull/' . $video_themnull_name;
}
}
在您的代码中
$video_themnull_name = uniqid() . Str::random('10') . '.' . $video_themnull->getClientOriginalExtension();
您创建了随机文件名,如果您想使用您的文件名添加视频 ID,那么您应该将 $old_video->id 添加到您的文件名创建中。所以请像下面这样重写上面的代码
$video_themnull_name = $old_video->id. Str::random('10') . '.' . $video_themnull->getClientOriginalExtension();
并在 if ($video_themnull->isValid()) condition
中添加以下代码old_video->image_path = $image_path;
$old_video->save();