Skip to content

Instantly share code, notes, and snippets.

@ydenissov
Created October 30, 2022 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ydenissov/48b8344d25ac61812240860dc747fc07 to your computer and use it in GitHub Desktop.
Save ydenissov/48b8344d25ac61812240860dc747fc07 to your computer and use it in GitHub Desktop.
Upload image to selectel cdn
<?php
// TODO: Разбить на методы
public function uploadFile(Request $request)
{
// Загружаем фото на хранилище присваивая начальную папку равную домену
$uuid = Str::uuid()->toString();
$domain = $request->getHttpHost();
$patient_id = $request->get('patient_id');
$directory = $request->get('patient_id') . '/' . date("Y-m-d", time()) . '/' . $uuid;
$file = $request->file('patient_file');
$file_name = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$mime_type = $file->getClientMimeType();
$file_size = $file->getSize();
$comment = $request->get('comment');
$file_title = $request->get('file_title');
$file_date = $request->get('file_date');
$file_type = $request->get('file_type');
if (($file_title === '') || ($file_title === null)) {
$file_title = $file_name;
}
if (($file_date === '') || ($file_date === null)) {
$file_date = Carbon::now();
}
try {
Storage::disk('selectel')->makeDirectory($domain . '/' . $directory);
$url = Storage::disk('selectel')->put($domain . '/' . $directory, $file, 'public');
} catch (\Exception $exception) {
return 'Error Selectel: ' . $exception->getMessage();
}
try {
$image_resize = Image::make($file->getRealPath());
$image_resize->resize(null, 300, function ($constraint) {
$constraint->aspectRatio();
});
Storage::disk('local')->makeDirectory('public/thumbs/' . $domain . '/' . $directory);
$image_resize->save(storage_path() . '/app/public/thumbs/' . $domain . '/' . $directory . '/' . $file_name, 100);
// Storage::disk('local')->put('public/thumbs/' . $domain . '/' . $directory . '/' . $file_name, $image_resize);
$thumb_path = Storage::disk('local')->url('public/thumbs/' . $domain . '/' . $directory . '/' . $file_name);
} catch (\Exception $exception) {
return 'Thumb create Error: ' . $exception->getMessage();
}
try {
$image_patient = new PatientImage();
$image_patient->file_name = $file_name;
$image_patient->title = $file_title;
$image_patient->type = $file_type;
$image_patient->full_path = $url;
$image_patient->thumb_path = $thumb_path;
$image_patient->mime_type = $mime_type;
$image_patient->patient_id = $patient_id;
$image_patient->comment = $comment;
$image_patient->storage_name = 'Selectel';
$image_patient->file_size = $file_size;
$image_patient->date_photo = Carbon::parse($file_date)->format("Y-m-d");
$image_patient->save();
} catch (\Exception $exception) {
return 'Save to DB Error: ' . $exception->getMessage();
}
return 'Ok';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment