Skip to content

Instantly share code, notes, and snippets.

@slider23
Last active August 29, 2015 14:10
Show Gist options
  • Save slider23/e8b6ee67434607ba4d60 to your computer and use it in GitHub Desktop.
Save slider23/e8b6ee67434607ba4d60 to your computer and use it in GitHub Desktop.
laravel dropzone example
view
<script src="/vendor/dropzone/dropzone.js"></script>
<?= breadcrumbs(['Список фотогалерей' => route("gallery"), $gallery->title => route("gallery.view", [$gallery->slug]), "Добавить фото"=>""]) ?>
<h1>Добавление фотографий</h1>
<form action="<?= route('gallery.upload')?>" class="dropzone" id="photogallery">
<p>Перетащите сюда фотографии или кликните чтобы выбрать их для загрузки.</p>
</form>
<script>
Dropzone.options.photogallery = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 20, // MB
url: "/gallery/<?=$gallery->id?>/upload"
};
</script>
controller
public function upload($gallery_id)
{
$gallery = Gallery::find($gallery_id);
if( ! $gallery) throw new NotFoundHttpException;
$photo = new GalleryPhoto();
$photo->user_id = Auth::id();
$photo->gallery_id = $gallery_id;
$file = Input::file('file');
$destinationPath = public_path()."/uploads/gallery/$gallery->folder";
$baseFilename = time()."A".str_random(4);
$extension = $file->getClientOriginalExtension();
$originalFilename = $baseFilename."_original.".$extension;
$mediumFilename = $baseFilename."_medium.".$extension;
$thumbFilename = $baseFilename."_thumb.".$extension;
$photo->filename_original = $originalFilename;
$upload_success = Input::file('file')->move($destinationPath, $originalFilename);
$imagePath = $destinationPath."/".$originalFilename;
if( $upload_success ) {
$img = Image::make($imagePath);
if($img->width() > $img->height()){
if($img->width()>999) {
$img->resize(999, null, function ($constraint) {
$constraint->aspectRatio();
});
}
$img->save($destinationPath."/".$mediumFilename);
$img->resize(330, null, function ($constraint) {
$constraint->aspectRatio();
});
$img->save($destinationPath."/".$thumbFilename);
}else{
if($img->height()>700) {
$img->resize(null, 700, function ($constraint) {
$constraint->aspectRatio();
});
}
$img->save($destinationPath."/".$mediumFilename);
$img->resize(null, 330, function ($constraint) {
$constraint->aspectRatio();
});
$img->save($destinationPath."/".$thumbFilename);
}
$photo->filename_medium = $mediumFilename;
$photo->filename_thumb = $thumbFilename;
$photo->save();
return Response::json('success', 200);
} else {
return Response::json('error', 400);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment