- $imageUpload->move($storagePath, $storageName);
- // Create and save image object
- $this->image->name = $name;
- $this->image->url = $imagePath . $storageName;
- $this->image->created_by = Auth::user()->id;
- $this->image->updated_by = Auth::user()->id;
- $this->image->save();
- $this->image->thumbnail = $this->getThumbnail($this->image, 150, 150);
- return response()->json($this->image);
+
+ return response()->json($image);
+ }
+
+ /**
+ * Generate a sized thumbnail for an image.
+ * @param $id
+ * @param $width
+ * @param $height
+ * @param $crop
+ * @return \Illuminate\Http\JsonResponse
+ */
+ public function getThumbnail($id, $width, $height, $crop)
+ {
+ $this->checkPermission('image-create-all');
+ $image = $this->imageRepo->getById($id);
+ $thumbnailUrl = $this->imageRepo->getThumbnail($image, $width, $height, $crop == 'false');
+ return response()->json(['url' => $thumbnailUrl]);
+ }
+
+ /**
+ * Update image details
+ * @param integer $imageId
+ * @param Request $request
+ * @return \Illuminate\Http\JsonResponse
+ */
+ public function update($imageId, Request $request)
+ {
+ $this->validate($request, [
+ 'name' => 'required|min:2|string'
+ ]);
+ $image = $this->imageRepo->getById($imageId);
+ $this->checkOwnablePermission('image-update', $image);
+ $image = $this->imageRepo->updateImageDetails($image, $request->all());
+ return response()->json($image);
+ }
+
+ /**
+ * Deletes an image and all thumbnail/image files
+ * @param EntityRepo $entityRepo
+ * @param Request $request
+ * @param int $id
+ * @return \Illuminate\Http\JsonResponse
+ */
+ public function destroy(EntityRepo $entityRepo, Request $request, $id)
+ {
+ $image = $this->imageRepo->getById($id);
+ $this->checkOwnablePermission('image-delete', $image);
+
+ // Check if this image is used on any pages
+ $isForced = ($request->has('force') && ($request->get('force') === 'true') || $request->get('force') === true);
+ if (!$isForced) {
+ $pageSearch = $entityRepo->searchForImage($image->url);
+ if ($pageSearch !== false) {
+ return response()->json($pageSearch, 400);
+ }
+ }
+
+ $this->imageRepo->destroyImage($image);
+ return response()->json(trans('components.images_deleted'));