Skip to content

Instantly share code, notes, and snippets.

@Sharifur
Created April 10, 2022 16:14
Show Gist options
  • Save Sharifur/f0b3774987f8128bfd8373c4ba779a25 to your computer and use it in GitHub Desktop.
Save Sharifur/f0b3774987f8128bfd8373c4ba779a25 to your computer and use it in GitHub Desktop.
dropzone js with chunk file support with php laravel
Dropzone.options.placeholderfForm = {
dictDefaultMessage: "{{__('Drag or Select Your Image')}}",
maxFiles: 50,
maxFilesize: 30720, //MB
chunking: true,
chunkSize: 10000000, // 10MB
acceptedFiles: 'image/*,video/*,.mp4,.avi,.flv,.mov',
success: function (file, response) {
if (file.previewElement) {
return file.previewElement.classList.add("dz-success");
}
$('#load_all_media_images').trigger('click');
$('.media-uploader-image-list li:first-child').addClass('selected');
},
error: function (file, message) {
if (file.previewElement) {
file.previewElement.classList.add("dz-error");
if ((typeof message !== "String") && message.error) {
message = message.error;
}
for (let node of file.previewElement.querySelectorAll("[data-dz-errormessage]")) {
node.textContent = message.errors.file[0];
}
}
}
};
package used to handle chunk upload
https://github.com/pionl/laravel-chunk-upload
// controller code example
use Illuminate\Http\UploadedFile;
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException;
use Pion\Laravel\ChunkUpload\Handler\AbstractHandler;
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
use Pion\Laravel\ChunkUpload\Exceptions\UploadFailedException;
public function upload_media_file(Request $request)
{
// create the file receiver
$receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request));
// dd($receiver);
// 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()) {
// dd($save->getFile());
// dd($this->createFilename($save->getFile()));
MediaHelper::insert_media_image($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())
// return $this->saveFile($save->getFile());
}
//
// // we are in chunk mode, lets send the current progress
// /** @var AbstractHandler $handler */
// $handler = $save->handler();
//
// return response()->json([
// "done" => $handler->getPercentageDone(),
// 'status' => true
// ]);
// $this->validate($request, [
// 'file' => 'nullable'
// ]);
// MediaHelper::insert_media_image($request);
}
public static function insert_media_image($file,$type='admin'){
// if ($request->hasFile('file')) {
$image = $file;
$image_dimension = getimagesize($image);
$image_extenstion = $image->extension();
$image_size_for_db = $image->getSize();
$image_name_with_ext = $image->getClientOriginalName();
$image_name = pathinfo($image_name_with_ext, PATHINFO_FILENAME);
$image_name = strtolower(Str::slug($image_name));
$image_db = $image_name . time() . '.' . $image_extenstion;
$folder_path = 'assets/uploads/media-uploader/';
$image_width = 0;
//check if it is video
$isVideo = true;
if( in_array($image_extenstion,['mp4','avi','flv']) ){
//write function to store video
}else{
$isVideo = false;
//it's an image
$image_width = $image_dimension[0];
$image_height = $image_dimension[1];
$image_dimension_for_db = $image_width . ' x ' . $image_height . ' pixels';
$image_grid = 'grid-' . $image_db;
$image_large = 'large-' . $image_db;
$image_thumb = 'thumb-' . $image_db;
$image_semi_large = 'semi-large-' . $image_db;
$image_box = 'box-' . $image_db;
$resize_grid_image = Image::make($image)->resize(350, null, function ($constraint) {
$constraint->aspectRatio();
});
$resize_large_image = Image::make($image)->resize(740, null, function ($constraint) {
$constraint->aspectRatio();
});
$resize_semi_large_image = Image::make($image)->resize(530, 350, function ($constraint) {
$constraint->aspectRatio();
});
$image_box_image = Image::make($image)->resize(527, 427, function ($constraint) {
$constraint->aspectRatio();
});
$resize_thumb_image = Image::make($image)->resize(200, 200);
}
$image->move($folder_path, $image_db);
MediaUpload::create([
'title' => $image_name_with_ext,
'size' => formatBytes($image_size_for_db),
'path' => $image_db,
'dimensions' => $image_dimension_for_db ?? '',
'type' => $type,
// 'mime_type' => $image_extenstion,
'user_id' => auth($type)->id(),
]);
if ($image_width > 150 && !$isVideo) {
$resize_thumb_image->save($folder_path . $image_thumb);
$resize_grid_image->save($folder_path . $image_grid);
$resize_large_image->save($folder_path . $image_large);
$resize_semi_large_image->save($folder_path . $image_semi_large);
$image_box_image->save($folder_path . $image_box);
}
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment