upload image

This commit is contained in:
Fayyazi
2019-07-20 15:13:18 +04:30
parent a0b7caada0
commit eb83d7e253
2 changed files with 74 additions and 13 deletions
+73 -12
View File
@@ -250,6 +250,10 @@ class TestResultController extends Controller
}
$TestResult['imagesPath'] = $ImagesPath;
}
else {
$TestResult['imagesPath'] = [];
}
return \MessageResponse::data($TestResult);
}
@@ -261,6 +265,14 @@ class TestResultController extends Controller
return $path;
}
public function hasFile($key)
{
if (is_array($file = $this->file($key))) $file = head($file);
return $file instanceof \SplFileInfo;
}
public function update(StoreTestResultRequest $request, TestResult $TestResult)
{
$TestResultData = [
@@ -322,20 +334,59 @@ class TestResultController extends Controller
}
/////////////////////////////////////////////////////////////////////Images
$images = '';
if (count($TestResult->TitleImage) && $request->imgs != null)
// if (count($TestResult->TitleImage) && $request->imgs != null)
// {
// $images = $TestResult->TitleImage;
// foreach ($images as $key => $image) {
// Storage::disk('public')->delete(static::storage_path($image->path));
// Uploadable::where('gallery_id', $image->id)->where('uploadable_id', $TestResult->id)->where('uploadable_type', get_class($TestResult))->delete();
// Gallery::where('id', $image->id)->delete();
// }
// }
//
// if ($request->imgs != null) {
// foreach ($request->imgs as $key => $img) {//
//
// $Image = $img;
// $InterventionImage = Image::make($Image);
// $Mime = explode('/', $InterventionImage->mime());
//
// $ImageName = time().$key.'.'.$Mime[1];
// $Path = 'uploads'.'/TestResults'.'/'.$TestResult->id .'/'.$ImageName;
//
// Storage::disk('public')->makeDirectory('TestResults'.'/'.$TestResult->id);
// $InterventionImage->save(public_path($Path));
//
// //--------------------------------------------------------
// $GalleryObj = Gallery::create(['user_id' => auth('api')->user()->id, 'path' => $Path]);
// $Uploadable = Uploadable::create(['uploadable_id' => $TestResult->id, 'uploadable_type' => 'App\TestResult', 'gallery_id' => $GalleryObj->id, 'image_role' => 'TitleImage']);
// //--------------------------------------------------------
//
// }//
// }
if (count($TestResult->TitleImage))
{
$images = $TestResult->TitleImage;
$imageExist = [];
$imagesDeleted = [];
foreach ($images as $key => $image) {
Storage::disk('public')->delete(static::storage_path($image->path));
Uploadable::where('gallery_id', $image->id)->where('uploadable_id', $TestResult->id)->where('uploadable_type', get_class($TestResult))->delete();
Gallery::where('id', $image->id)->delete();
$imageExist[] = '/'.$image->path;
if (!in_array('/'.$image->path, $request->imgs)) {
$imagesDeleted[] = $image->path;
}
}
}
if ($request->imgs != null) {
foreach ($request->imgs as $key => $img) {//
if ($request->imgs != null) {
$request_imgs = array_diff($request->imgs, $imageExist);
foreach ($request_imgs as $key => $img) {//
$Image = $img;
$InterventionImage = Image::make($Image);
$Mime = explode('/', $InterventionImage->mime());
@@ -345,13 +396,23 @@ class TestResultController extends Controller
Storage::disk('public')->makeDirectory('TestResults'.'/'.$TestResult->id);
$InterventionImage->save(public_path($Path));
//--------------------------------------------------------
$GalleryObj = Gallery::create(['user_id' => auth('api')->user()->id, 'path' => $Path]);
$Uploadable = Uploadable::create(['uploadable_id' => $TestResult->id, 'uploadable_type' => 'App\TestResult', 'gallery_id' => $GalleryObj->id, 'image_role' => 'TitleImage']);
//--------------------------------------------------------
//--------------------------------------------------------
$GalleryObj = Gallery::create(['user_id' => auth('api')->user()->id, 'path' => $Path]);
$Uploadable = Uploadable::create(['uploadable_id' => $TestResult->id, 'uploadable_type' => 'App\TestResult', 'gallery_id' => $GalleryObj->id, 'image_role' => 'TitleImage']);
//--------------------------------------------------------
}
}//
}
foreach ($imagesDeleted as $key => $imageDeleted) {
$Gallery = Gallery::where('path', $imageDeleted)->findOrFail();
if ($Gallery) {
Storage::disk('public')->delete(static::storage_path($imageDeleted));
Uploadable::where('gallery_id', $Gallery->id)->where('uploadable_id', $TestResult->id)->where('uploadable_type', get_class($TestResult))->delete();
$Gallery->delete();
}
}
}
return \MessageResponse::success(null,trans('messages.update'));
}