laravel - 在 laravel 存储中保存原始图像和转换为 webp
问题描述
当我在我的项目中存储图像时,我正在尝试将原始 jpg 图像和转换后的 webp 图像保存到我的存储中。
public function store($data)
{
if (!$data->hasFile('fileName')) {
return response()->json(['upload_file_not_found'], 400);
}
$allowedfileExtension = ['jpg', 'jpeg'];
$files = $data->file('fileName');
$errors = [];
$images = [];
foreach ($data->fileName as $mediaFiles) {
$extension = $mediaFiles->getClientOriginalExtension();
$check = in_array($extension, $allowedfileExtension);
if ($check) {
// $path = $mediaFiles->store('public/images');
$name = $mediaFiles->getClientOriginalName();
//store image file into directory and db
$image = new Image();
$image->title = $name;
// $image->path = substr($path, 7);
$image->description = $data->description;
$image->author_id = $data->author_id;
$image->save();
//put image to storage wih unique folder
$path = $mediaFiles->storeAs('public/images/article-images/'.$image->id, $name);
//try to convert to webp and add to storage
$image = InterventionImage::make($mediaFiles)->encode('webp', 90)->resize(200, 250)->save('public/images/' . $name . '.webp');
//
//update images path in database
$image->update(['path' => substr($path, 7)]);
//add to attach the article id
if ($data->article_id) {
$image->articles()->attach($data->article_id);
}
array_push($images, $image);
} else {
return response()->json(['invalid_file_format'], 422);
}
}
return $images;
}
我正在使用干预库,但是当我尝试将转换后的图像保存到我的存储时,我收到错误“消息”:“无法将图像数据写入路径(public/images/newimage.jpg.webp)”, 有人可以帮忙吗有了这个或有任何其他建议如何做到这一点?
解决方案
如果有人遇到同样的问题,我会这样做
public function newStore($data)
{
if (!$data->hasFile('fileName')) {
return response()->json(['upload_file_not_found'], 400);
}
$allowedfileExtension = ['jpg', 'jpeg'];
$files = $data->file('fileName');
$errors = [];
$images = [];
foreach ($data->fileName as $mediaFiles) {
$extension = $mediaFiles->getClientOriginalExtension();
$check = in_array($extension, $allowedfileExtension);
if ($check) {
// $path = $mediaFiles->store('public/images');
$name = $mediaFiles->getClientOriginalName();
//getting the name of the file without extension
$filename = pathinfo($name, PATHINFO_FILENAME);
//store image file into directory and db
$image = new Image();
$image->title = $name;
// $image->path = substr($path, 7);
$image->description = $data->description;
$image->author_id = $data->author_id;
$image->save();
//put image to storage in unique folder
$path = $mediaFiles->storeAs('public/images/article-images/' . $image->id, $name);
//imege conversion in webp format and move to right folder
$interventionImage = InterventionImage::make($mediaFiles)->stream("webp", 100);
Storage::disk('local')->put($filename . '.webp', $interventionImage, 'public');
Storage::move($filename . '.webp', 'public/images/article-images/' . $image->id . '/' . $filename . '.webp');
//update images path in database
$image->update(['path' => substr($path, 7)]);
//add to attach the article id
if ($data->article_id) {
$image->articles()->attach($data->article_id);
}
array_push($images, $image);
} else {
return response()->json(['invalid_file_format'], 422);
}
}
return $images;
}
推荐阅读
- react-native - 反应原生显示状态栏而不是全屏
- c++ - 为什么下面的代码会显示No output?
- html - 如何在 MS Access 中使用 VBA 在没有类、ID、仅标签名称输入的情况下单击 HTML 按钮
- angular - 使用 ngModel 将 MatRadioGroup 绑定到枚举
- unity3d - 未找到 UnityEngine.XR.WSA.WorldManager 命名空间,它现在位于何处?
- typescript - 将属性键作为打字稿中的通用参数传递
- java - 何时使用@Service 注解?
- python - 如果代码包含 %s 格式,如何发出警告
- sql - 在 BQ 中计算时间戳差异(以秒为单位)
- php - PHP 7:警告:期望参数 1 是字符串,给定数组