/** * Handle Image Upload * * @throws \Exception * * @return ApiFileService */ private function handleImageUpload() { try { $config = new FlowConfig(); $request = new FlowRequest(); $basic = new Basic(); $config->setTempDir(storage_path() . '/tmp'); $config->setDeleteChunksOnSave(false); $totalSize = $request->getTotalSize(); $uploadFile = $request->getFile(); $fileName = md5($request->getFileName()); $extension = explode('.', $request->getFileName())[1]; $extraNumber = 1; if ($totalSize && $totalSize > 1024 * 1024 * 4) { throw new \Exception('File size exceeds 4MB', 400); } while ($this->isNameDuplicated($fileName . '.' . $extension, $this->location)) { $fileName = $fileName . $extraNumber; ++$extraNumber; } $fileName = $fileName . '.' . $extension; if ($basic->save($this->directory . $this->location . $fileName, $config, $request)) { $file = $this->handleImageSave($fileName, $this->location); return $file; } throw new \Exception('unable to save file', 500); } catch (\Exception $e) { throw new \Exception($e->getMessage(), $e->getCode()); } }
/** * Check if all chunks of a file being uploaded have been received * If yes, return the name of the reassembled temporary file * * @param UploadedFile $uploadedFile * * @return UploadedFile|null */ public function getFileFromChunks(UploadedFile $uploadedFile) { $filename = time() . '-' . $uploadedFile->getClientOriginalName(); $path = $this->tmpDir . DIRECTORY_SEPARATOR . $filename; if (FlowBasic::save($path, $this->tmpDir)) { return new UploadedFile($path, $uploadedFile->getClientOriginalName(), $uploadedFile->getClientMimeType()); } return null; }
protected function flowSave() { if (\Flow\Basic::save($this->getDestinationDir() . '/' . $this->request->getFileName(), $this->config, $this->request)) { $storage = $this->getDestinationDir(); return Response::json(['data' => $this->filename, 'message' => "File Uploaded {$storage}/{$this->filename}"], 200); } else { //Not sure why it needs a 404 return Response::json([], 404); } }
public function uploadFile(Request $request, $model_id = false, $r_id) { try { $this->model_id = $model_id; $this->r_id = $r_id; $model_class_path = $this->getClassName($request); $this->destination_path = $this->getImagePublicDestinationPath($request); $realPath = public_path() . '/' . $this->destination_path; File::makeDirectory($realPath, $mode = 0777, true, true); $this->model_class_path = $model_class_path; $this->config = new Config(array('tempDir' => storage_path('chunks_temp_folder'))); $this->filename = Input::get('flowFilename'); $flowRequest = new \Flow\Request(); if (\Flow\Basic::save(public_path($this->getPathWithFile()), $this->config, $flowRequest)) { $image = $this->saveImagable(); return Response::json(['data' => $image, 'message' => "File Uploaded {$this->filename}"], 200); } else { return Response::json([], 204); } } catch (\Exception $e) { throw new \Exception(sprintf("Error saving image %s", $e->getMessage())); } }
<?php require_once './vendor/autoload.php'; $config = new \Flow\Config(); $config->setTempDir('./chunks_temp_folder'); $request = new \Flow\Request(); if (\Flow\Basic::save('./' . $request->getIdentifier(), $config, $request)) { // file saved successfully and can be accessed at './final_file_destination' } else { // This is not a final chunk or request is invalid, continue to upload. }
public function addImages($file) { $config = new Config(array('tempDir' => storage_path('chunks_temp_folder'))); $filename = $file['flowTotalSize'] . '-' . $file['flowFilename']; $flowRequest = new \Flow\Request(); if (\Flow\Basic::save(public_path() . '/uploads/anuncios/' . $filename, $config, $flowRequest)) { $img = Image::make(public_path() . '/uploads/anuncios/' . $filename); $img->fit(500, 300, function ($constraint) { $constraint->upsize(); }); $img->save(); return 'Imagem salva com sucesso!'; } }
/** * Upload a file * @param string $index The form Index * @param string $folder the Folder to save the new file * @param string $newFilename the new filename (just filename no folder) * @param string $encodingFormat The Format the file to be encoded. jpg, png * @param array $size The Size to encode [$width, $height], [$width, null] * @return string The Path to the new file */ function zbase_file_upload_image($index, $folder, $newFilename, $encodingFormat = 'jpg', $size = []) { /** * Using angular flow.js * https://github.com/flowjs/flow-php-server * "flowChunkNumber" => "1" "flowChunkSize" => "1048576" "flowCurrentChunkSize" => "83167" "flowTotalSize" => "83167" "flowIdentifier" => "83167-Avatar2jpg" "flowFilename" => "Avatar2.jpg" "flowRelativePath" => "Avatar2.jpg" "flowTotalChunks" => "1" * */ $tmpFolder = zbase_tmp_path(); $newFile = $folder . str_replace(array('.png', '.jpg', '.gif', '.bmp', '.jpeg'), '.' . $encodingFormat, $newFilename); if (!empty(zbase_request_query_input('flowChunkNumber', false))) { if (\Flow\Basic::save($newFile, $tmpFolder)) { $im = \Image::make($newFile); } else { // zbase()->json()->setVariable('files', $_FILES); // zbase()->json()->setVariable('success', false); // zbase()->json()->setVariable('get', $_GET); // zbase()->json()->setVariable('post', $_POST); return zbase_abort(204); } } if (!empty($_FILES[$index]['tmp_name'])) { zbase_directory_check($folder, true); if (class_exists('\\Image')) { $im = \Image::make($_FILES[$index]['tmp_name']); } else { if (move_uploaded_file($_FILES[$index]['tmp_name'], $newFile)) { return $newFile; } } } if (!empty($im)) { if (!empty($size)) { $im->resize($size[0], $size[1], function ($constraint) { $constraint->aspectRatio(); }); } $im->encode($encodingFormat, 100)->save($newFile); return $newFile; } return false; }
<?php //Path to autoload.php from current location require_once './vendor/autoload.php'; $config = new \Flow\Config(); $config->setTempDir('./chunks_temp_folder'); $request = new \Flow\Request(); if (\Flow\Basic::save('./img/plannerprofiles/' . $request->getIdentifier(), $config, $request)) { // file saved successfully and can be accessed at './final_file_destination' } else { // This is not a final chunk or request is invalid, continue to upload. }