简体   繁体   English

在Laravel中编辑和复制图像[无法将图像数据写入路径]

[英]Edit and copy image in Laravel [ Can't write image data to path ]

I have created the following 4 model and its table structure for my project: 我为项目创建了以下4个模型及其表结构:

Media.php To upload images in app Media.php 在应用程序中上传图像

Medias table structure 媒体表结构

id | id | path 路径

Example of path:uploads/images/media/food-1542110154.jpg 路径示例:uploads / images / media / food-1542110154.jpg

Post.php Create post Post.php 创建帖子

Posts table structure 帖子表结构

id | id | title | 标题| content 内容

FeaturedImage.php Featured image for post FeaturedImage.php 特色图片发布

Posts table structure 帖子表结构

id | id | post_id| POST_ID | path 路径

Post model and FeaturedImage model are in a one-to-one relationship 帖子模型和FeaturedImage模型是一对一的关系

UploadImage.php To resize the uploaded image and move it to another directory. UploadImage.php 调整上载图像的大小并将其移动到另一个目录。 This model doesn't have migration and controller 该模型没有迁移和控制器

Code snippet from PostsController.php to create the post 来自PostsController.php代码片段来创建帖子

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');
}

Code snippet from UploadImage.php 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;
 }  
}

In my Laravel app, I'm trying to edit dimension of the already uploaded image with method written in UploadImage.php and save edited image in post directory and save its path in featured_images table. 在我的Laravel应用程序,我想编辑写在法已上传的图片的尺寸UploadImage.php并保存编辑后的图像在post目录,并保存其路径featured_images表。

But I've been getting **Can't to write image data to path ** error. 但是我一直在**无法将图像数据写入路径**错误。

I would be very thankful if anyone could point out the mistakes that I've made. 如果有人能指出我犯的错误,我将非常感谢。

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. 由于我浏览了几乎所有与此类错误有关的帖子,因此对我没有帮助。

Tweaked few lines in my UploadImage.php model and got it solved. 在我的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;
} 

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

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