使用:Laravel 5.2
, Intervention 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}";
}
在我发布表单后,我的第一个图像得到正确保存,但之后它抛出一个错误:
由于未知错误,文件"myfile.jpg"未上传。
What i've try
我的第一个想法是
time()
函数不够具体,文件具有相同的名称。所以我把time()
改成了microtime(true)
我对图像大小做了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;
}
试试这个方案
$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);
我认为你必须有两个对象-从另一个角度来看,它不能很好地工作。