无法以 2 种不同的格式调整同一文件的大小。干预图像 - Laravel

Can't resize same file in 2 seperate formats . Intervention Image - Laravel

使用:Laravel 5.2Intervention Image http://image.intervention.io/

我有一个可以上传图片的表格。此图像会调整大小并存储在我的服务器上。这一切都很完美,但我最近需要制作第二个图像尺寸。所以我做了以下事情:

控制器

// Resize uploaded thumbnail and return the temporary file path
        $thumbnail = $request->file('thumbnail');
        $thumbnail_url = $this->storeTempImage($thumbnail,350,230);
        $video_thumbnail_url = $this->storeTempImage($thumbnail,1140,640);

StoreTempImage 方法

  public function storeTempImage($uploadedImage, $width, $height) 
    {
        //resize and save image to temporary storage
        $originalName = $uploadedImage->getClientOriginalName();
        $name = time() . $originalName;
        $uploadedImage->move('images/temp/', $name);

        Image::make("images/temp/{$name}")->fit($width, $height, function ($constraint) {
            $constraint->upsize();
        })->save("images/temp/{$name}");

        return "images/temp/{$name}";
    }

在我 post 表单后我的第一张图片被正确保存,但之后它抛出错误:

The file "myfile.jpg" was not uploaded due to an unknown error.

我试过的

  1. 我的第一个想法是 time() 函数不够具体,并且文件具有相同的名称。所以我将 time() 更改为 microtime(true)

  2. 我为图像尺寸做了 2 种不同的方法

两种解决方案均无效并抛出相同的错误。

试试这个解决方案

    $thumbnail = $request->file('thumbnail');
    $thumbnail_url = $this->storeTempImage($thumbnail,350,230);
    $video_thumbnail_url = $this->storeTempImage(clone $thumbnail,1140,640);
    $video_thumbnail_url = $this->storeTempImage($thumbnail,1140,640);

您将 OBJECT 发送到 storeTempImage() - 并且您在行

中修改了该对象
$uploadedImage->move('images/temp/', $name);

我认为您必须有 2 个对象 - 换句话说,它运行不佳。

实际上您使用的是同一个对象来创建和保存图像,您应该这样做:

public function storeTempImage($uploadedImage, $width, $height) 
{
    // Creating Image Intervention Instance
    $img = Image::make($uploadedImage);
    $originalName = $uploadedImage->getClientOriginalName();

    // Include the below line if you want to store this image, else leave it
    $uploadedImage->move('images/temp/', time() . $originalName);

    // Cropping the image according to given params
    $new_img = $img->fit($width, $height, function ($constraint) {
        $constraint->upsize();
    })->save("images/temp/" . time() . $originalName);

    return $new_img;
}

希望对您有所帮助。