Laravel 5.7。我有一个用于处理图像上传的类:
use Intervention\Image\Facades\Image as ImageHandler;
class ImageUpload
{
/**
* Upload an image to the public folder
*
* @param \Illuminate\Http\UploadedFile $imageFile
* @return void
*/
public function upload($imageFile)
{
$this->extension = $imageFile->extension();
$this->filename = $this->getHashedFilename($imageFile);
$this->originalImage = ImageHandler::make($imageFile->getRealPath())->encode();
// Do other stuff...
$this->saveImage();
}
}
这在将上传的文件传递给它时有效。但我想重用代码来处理存储在文件系统上的图像。例如,我有一个占位符图像,它存储在resources/images. 我想将它传递给上面类的upload方法ImageUpload。但是我对文件的引用显然是一个字符串,而该upload方法需要一个UploadedFile. 所以当我尝试这样做时:
class FooBar
{
public function savePlaceholder()
{
$placeholderImage = asset('images/test.png');
$uploadHandler = new ImageUpload();
$uploadHandler->upload($placeholderImage);
}
}
我收到一个错误,因为我传递的是一个字符串而不是UploadedFile.
烙印99