Hi @snapey and @sergiu17
I have tried to upload by resizing the image When I try it, I got the below error.
{message: "Unable to find file ().", exception: "Intervention\Image\Exception\NotReadableException",…}
exception: "Intervention\Image\Exception\NotReadableException"
file: "D:\laragon\www\projects\indeed-it-solutions\2021\sameera-photography-3\vendor\intervention\image\src\Intervention\Image\Gd\Decoder.php"
line: 20
message: "Unable to find file ()."
trace: [{,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…}, {,…},…]
Here is my full source code.
public function store(Request $request)
{
$receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request));
// check if the upload is success, throw exception or return response you need
if ($receiver->isUploaded() === false) {
throw new UploadMissingFileException();
}
// receive the file
$save = $receiver->receive();
// check if the upload has finished (in chunk mode it will send smaller files)
if ($save->isFinished()) {
$fileName = $this->createFilename($save->getFile());
// save the file and return any response you need, current example uses `move` function. If you are
// not using move, you need to manually delete the file by unlink($save->getFile()->getPathname())
$imageThumbnail = $request['file'];
$imageThumbnailName = time().'.'.$imageThumbnail->getClientOriginalExtension();
$destinationPath = public_path('/uploads/gallery-thumbnail');
$img = Image::make($request['file']);
$img->resize(null,600, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})->save($destinationPath.'/'.$imageThumbnailName);
$result = $this->saveFile($save->getFile(),$fileName);
$data = new GalleryImage();
$data->image = $fileName;
$data->thumbnail = $imageThumbnailName;
$data->gallery_id=$request->gallery;
$data->save();
}
return response('success');
// we are in chunk mode, lets send the current progress
/** @var AbstractHandler $handler */
$handler = $save->handler();
}
protected function saveFile(UploadedFile $file,$filename)
{
$fileName = $filename;
// Group files by mime type
$mime = str_replace('/', '-', $file->getMimeType());
// Group files by the date (week
$dateFolder = date("Y-m-W");
// Build the file path
$filePath = "upload/{$mime}/{$dateFolder}/";
$finalPath =public_path('uploads/');
// move the file name
$file->move($finalPath, $fileName);
return response()->json( $fileName);
}
/**
* Create unique filename for uploaded file
* @param UploadedFile $file
* @return string
*/
protected function createFilename(UploadedFile $file)
{
$extension = $file->getClientOriginalExtension();
$filename = str_replace(".".$extension, "", $file->getClientOriginalName()); // Filename without extension
// Add timestamp hash to name of the file
$filename .= "_" . md5(time()) . "." . $extension;
return $filename;
}