在 Laravel 中编辑和复制图像 [无法将图像数据写入路径]
Edit and copy image in Laravel [ Can't write image data to path ]
我为我的项目创建了以下 4 个模型及其 table 结构:
Media.php 在应用程序中上传图片
Medias table structure
编号 |路径
路径示例:uploads/images/media/food-1542110154.jpg
Post.php 创建post
Posts table structure
编号 |标题 |内容
FeaturedImage.php post
的特色图片
Posts table structure
编号 | post_id|路径
Post model and FeaturedImage model are in a one-to-one relationship
UploadImage.php 调整上传图片的大小,移动到其他目录。此模型没有迁移和控制器
来自 PostsController.php
的代码片段,用于创建 post
use App\UploadImage;
use App\Media;
class PostController extends Controller
{
private $imagePath= "uploads/images/post/";
public function store(Request $request)
{
$post = new Post;
$post->title = $request->title;
$post->content = $request->content;
$post->save();
$media = Media::find($request->featured);
if (!File::exists($this->imagePath)) {
File::makeDirectory($this->imagePath);
}
$upload = new UploadImage;
$image= $upload->uploadSingle($this->banner, $media->path, 400,300);
$post->image()->save(new FeaturedImage([
'path' => $image
]));
}
Session::flash('success', 'Post created sucessfully !');
return redirect()->route('post.index');
}
来自 UploadImage.php
的代码片段
use Intervention\Image\Facades\Image;
use Spatie\LaravelImageOptimizer\Facades\ImageOptimizer;
use Illuminate\Database\Eloquent\Model;
class UploadImage extends Model
{
public function uploadSingle($savePath, $image,$width,$height)
{
Image::make(public_path($image))->fit($width, $height)->save($savePath);
ImageOptimizer::optimize($savePath);
return $savePath;
}
}
在我的 Laravel 应用程序中,我尝试使用 UploadImage.php
中编写的方法编辑已上传图像的尺寸,并将编辑后的图像保存在 post
目录中并保存其路径在 featured_images
table.
但我一直收到 **Can't to write image data to path ** 错误。
如果有人能指出我所犯的错误,我将不胜感激。
Please do not mark this as duplicate content. As I've been gone through almost all posts related to this kind of error and they have been no help to me.
调整了我的 UploadImage.php 模型中的几行并解决了问题。
public function uploadSingle($savePath, $image,$width,$height)
{
$filename = basename($image);
$location = $savePath . $filename;
Image::make($image)->save($location);
ImageOptimizer::optimize($location);
return $location;
}
我为我的项目创建了以下 4 个模型及其 table 结构:
Media.php 在应用程序中上传图片
Medias table structure
编号 |路径
路径示例:uploads/images/media/food-1542110154.jpg
Post.php 创建post
Posts table structure
编号 |标题 |内容
FeaturedImage.php post
的特色图片Posts table structure
编号 | post_id|路径
Post model and FeaturedImage model are in a one-to-one relationship
UploadImage.php 调整上传图片的大小,移动到其他目录。此模型没有迁移和控制器
来自 PostsController.php
的代码片段,用于创建 post
use App\UploadImage;
use App\Media;
class PostController extends Controller
{
private $imagePath= "uploads/images/post/";
public function store(Request $request)
{
$post = new Post;
$post->title = $request->title;
$post->content = $request->content;
$post->save();
$media = Media::find($request->featured);
if (!File::exists($this->imagePath)) {
File::makeDirectory($this->imagePath);
}
$upload = new UploadImage;
$image= $upload->uploadSingle($this->banner, $media->path, 400,300);
$post->image()->save(new FeaturedImage([
'path' => $image
]));
}
Session::flash('success', 'Post created sucessfully !');
return redirect()->route('post.index');
}
来自 UploadImage.php
use Intervention\Image\Facades\Image;
use Spatie\LaravelImageOptimizer\Facades\ImageOptimizer;
use Illuminate\Database\Eloquent\Model;
class UploadImage extends Model
{
public function uploadSingle($savePath, $image,$width,$height)
{
Image::make(public_path($image))->fit($width, $height)->save($savePath);
ImageOptimizer::optimize($savePath);
return $savePath;
}
}
在我的 Laravel 应用程序中,我尝试使用 UploadImage.php
中编写的方法编辑已上传图像的尺寸,并将编辑后的图像保存在 post
目录中并保存其路径在 featured_images
table.
但我一直收到 **Can't to write image data to path ** 错误。
如果有人能指出我所犯的错误,我将不胜感激。
Please do not mark this as duplicate content. As I've been gone through almost all posts related to this kind of error and they have been no help to me.
调整了我的 UploadImage.php 模型中的几行并解决了问题。
public function uploadSingle($savePath, $image,$width,$height)
{
$filename = basename($image);
$location = $savePath . $filename;
Image::make($image)->save($location);
ImageOptimizer::optimize($location);
return $location;
}