diff --git a/app/Http/Controllers/CategoriesController.php b/app/Http/Controllers/CategoriesController.php index 1dbe7488fa..0646ecc9e2 100755 --- a/app/Http/Controllers/CategoriesController.php +++ b/app/Http/Controllers/CategoriesController.php @@ -148,17 +148,38 @@ class CategoriesController extends Controller $category->require_acceptance = $request->input('require_acceptance', '0'); $category->checkin_email = $request->input('checkin_email', '0'); + $old_image = $category->image; + + + // Set the model's image property to null if the image is being deleted + if ($request->input('image_delete') == 1) { + $category->image = null; + } + + // If a new file is being uploaded, resize it and save it if ($request->file('image')) { $image = $request->file('image'); - $file_name = str_random(25).".".$image->getClientOriginalExtension(); - $path = public_path('uploads/categories/'.$file_name); - Image::make($image->getRealPath())->resize(200, null, function ($constraint) { - $constraint->aspectRatio(); - $constraint->upsize(); - })->save($path); + $file_name = $category->id.'-'.str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension(); + + if ($image->getClientOriginalExtension()!='svg') { + Image::make($image->getRealPath())->resize(500, null, function ($constraint) { + $constraint->aspectRatio(); + $constraint->upsize(); + })->save(app('categories_upload_path').$file_name); + } else { + $image->move(app('categories_upload_path'), $file_name); + } $category->image = $file_name; - } elseif ($request->input('image_delete')=='1') { - $category->image = null; + + } + + // If there is a new file being uploaded + if ((($request->file('image')) && (isset($old_image)) && ($old_image!='')) || ($request->input('image_delete') == 1)) { + try { + unlink(app('categories_upload_path').$old_image); + } catch (\Exception $e) { + \Log::error($e); + } } diff --git a/app/Http/Transformers/AssetModelsTransformer.php b/app/Http/Transformers/AssetModelsTransformer.php index 53f5997ed6..8c94f00577 100644 --- a/app/Http/Transformers/AssetModelsTransformer.php +++ b/app/Http/Transformers/AssetModelsTransformer.php @@ -28,7 +28,7 @@ class AssetModelsTransformer 'id' => (int) $assetmodel->manufacturer->id, 'name'=> e($assetmodel->manufacturer->name) ] : null, - 'image' => ($assetmodel->image!='') ? url('/').'/uploads/models/'.e($assetmodel->image) : null, + 'image' => ($assetmodel->image!='') ? app('models_upload_url').e($assetmodel->image) : null, 'model_number' => e($assetmodel->model_number), 'depreciation' => ($assetmodel->depreciation) ? [ 'id' => (int) $assetmodel->depreciation->id, diff --git a/app/Http/Transformers/CategoriesTransformer.php b/app/Http/Transformers/CategoriesTransformer.php index 121e73803e..0c77917d42 100644 --- a/app/Http/Transformers/CategoriesTransformer.php +++ b/app/Http/Transformers/CategoriesTransformer.php @@ -25,7 +25,7 @@ class CategoriesTransformer $array = [ 'id' => (int) $category->id, 'name' => e($category->name), - 'image' => ($category->image) ? e(url('/').'/uploads/categories/'.e($category->image)) : null, + 'image' => ($category->image) ? app('categories_upload_url').e($category->image) : null, 'type' => e($category->category_type), 'eula' => ($category->getEula()) ? true : false, 'checkin_email' => ($category->checkin_email =='1') ? true : false, diff --git a/app/Http/Transformers/CompaniesTransformer.php b/app/Http/Transformers/CompaniesTransformer.php index 107ed59b17..d8f89ea3b0 100644 --- a/app/Http/Transformers/CompaniesTransformer.php +++ b/app/Http/Transformers/CompaniesTransformer.php @@ -25,7 +25,7 @@ class CompaniesTransformer $array = [ 'id' => (int) $company->id, 'name' => e($company->name), - 'image' => ($company->image) ? e(url('/').'/uploads/companies/'.e($company->image)) : null, + 'image' => ($company->image) ? app('companies_upload_url').e($company->image) : null, "created_at" => Helper::getFormattedDateObject($company->created_at, 'datetime'), "updated_at" => Helper::getFormattedDateObject($company->updated_at, 'datetime'), "assets_count" => (int) $company->assets_count, diff --git a/app/Http/Transformers/DepartmentsTranformer.php b/app/Http/Transformers/DepartmentsTranformer.php index 00a8746e8b..dd51e73aa8 100644 --- a/app/Http/Transformers/DepartmentsTranformer.php +++ b/app/Http/Transformers/DepartmentsTranformer.php @@ -25,7 +25,7 @@ class DepartmentsTransformer $array = [ 'id' => (int) $department->id, 'name' => e($department->name), - 'image' => ($department->image) ? e(url('/').'/uploads/departments/'.e($department->image)) : null, + 'image' => ($department->image) ? app('departments_upload_url').e($department->image) : null, 'company' => ($department->company) ? [ 'id' => (int) $department->company->id, 'name'=> e($department->company->name) diff --git a/app/Http/Transformers/LocationsTransformer.php b/app/Http/Transformers/LocationsTransformer.php index 636c039a91..96fd25fee6 100644 --- a/app/Http/Transformers/LocationsTransformer.php +++ b/app/Http/Transformers/LocationsTransformer.php @@ -33,7 +33,7 @@ class LocationsTransformer $array = [ 'id' => (int) $location->id, 'name' => e($location->name), - 'image' => ($location->image) ? e(url('/').'/uploads/locations/'.e($location->image)) : null, + 'image' => ($location->image) ? app('locations_upload_url').e($location->image) : null, 'address' => e($location->address), 'city' => e($location->city), 'state' => e($location->state), diff --git a/app/Http/Transformers/ManufacturersTransformer.php b/app/Http/Transformers/ManufacturersTransformer.php index 93bcf36d24..7ef0ea54a6 100644 --- a/app/Http/Transformers/ManufacturersTransformer.php +++ b/app/Http/Transformers/ManufacturersTransformer.php @@ -26,7 +26,7 @@ class ManufacturersTransformer 'id' => (int) $manufacturer->id, 'name' => e($manufacturer->name), 'url' => e($manufacturer->url), - 'image' => ($manufacturer->image) ? e(url('/').'/uploads/manufacturers/'.e($manufacturer->image)) : null, + 'image' => ($manufacturer->image) ? app('manufacturers_upload_url').e($manufacturer->image) : null, 'support_url' => e($manufacturer->support_url), 'support_phone' => e($manufacturer->support_phone), 'support_email' => e($manufacturer->support_email), diff --git a/app/Http/Transformers/SuppliersTransformer.php b/app/Http/Transformers/SuppliersTransformer.php index bd3aac350b..0070f7c677 100644 --- a/app/Http/Transformers/SuppliersTransformer.php +++ b/app/Http/Transformers/SuppliersTransformer.php @@ -25,7 +25,7 @@ class SuppliersTransformer $array = [ 'id' => (int) $supplier->id, 'name' => e($supplier->name), - 'image' => ($supplier->image) ? e(url('/').'/uploads/suppliers/'.e($supplier->image)) : null, + 'image' => ($supplier->image) ? app('suppliers_upload_url').e($supplier->image) : null, 'address' => ($supplier->address) ? e($supplier->address) : null, 'address2' => ($supplier->address2) ? e($supplier->address2) : null, 'city' => ($supplier->city) ? e($supplier->city) : null,