简体   繁体   English

在Laravel中上传图片时无法创建缩略图

[英]Unable to create thumbnail while image is uploading in Laravel

I'm trying to create thumbnail of image while it is uploading. 我正在尝试上传图片时创建缩略图。 The problem is that the thumbnail isn't created at all. 问题在于根本没有创建缩略图。 Also is not saved in database. 也不保存在数据库中。

This is what I have added in my function 这就是我在函数中添加的内容

    $image = $request->file('image');

    if( $image && $image->isValid()){

        $imagename = str_random(20).'.'.$image->getClientOriginalExtension(); 

        $destinationPath = public_path('/uploads');
        $thumb_img = Image::make($image->getRealPath())->resize(100, 100);
        $thumb_img->save($destinationPath.'/'.$imagename,80);

        $image->move($destinationPath, $imagename);                    
    }

    $item->image = $filename;
    $item->image_thumb = $thumb_img;

It's saves only the original image both places - uploads dir and in database but nothing regarding the thumbnail. 它只保存原始图像的两个位置- uploads目录和数据库,但不保存缩略图。

I'm using Intervention package. 我正在使用干预包。

Nothing is saving because you override the same image twice. 没有什么可节省的,因为您两次覆盖同一图像。 Look what you have: 看一下你所拥有的:

First, you creating thumbnail and saves it into the /uploads 首先,您创建thumbnail并将其保存到/uploads

After this, you save the original into the same directory with same name eg overriding the thumb. 之后,您将原始文件保存到相同名称的相同目录中,例如覆盖缩略图。

You just need to make different name for the thumbnail: 您只需要为缩略图指定其他名称:

$thumb_img = Image::make($image)->resize(100, 100)->save($destinationPath.'/thumb_'.$imagename, 80);

Notice the prefix for the thumb thumb_ ... 注意thumb thumb_的前缀...

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

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