+ /**
+ * Get all images, Paginated
+ * @param int $page
+ * @return \Illuminate\Http\JsonResponse
+ */
+ public function getAll($page = 0)
+ {
+ $pageSize = 30;
+ $images = DB::table('images')->orderBy('created_at', 'desc')
+ ->skip($page * $pageSize)->take($pageSize)->get();
+ foreach ($images as $image) {
+ $image->thumbnail = $this->getThumbnail($image, 150, 150);
+ }
+ $hasMore = count(DB::table('images')->orderBy('created_at', 'desc')
+ ->skip(($page + 1) * $pageSize)->take($pageSize)->get()) > 0;
+ return response()->json([
+ 'images' => $images,
+ 'hasMore' => $hasMore
+ ]);
+ }
+
+ /**
+ * Get the thumbnail for an image.
+ * @param $image
+ * @param int $width
+ * @param int $height
+ * @return string
+ */
+ public function getThumbnail($image, $width = 220, $height = 220)
+ {
+ $explodedPath = explode('/', $image->url);
+ array_splice($explodedPath, 4, 0, ['thumbs-' . $width . '-' . $height]);
+ $thumbPath = implode('/', $explodedPath);
+ $thumbFilePath = public_path() . $thumbPath;
+
+ // Return the thumbnail url path if already exists
+ if (file_exists($thumbFilePath)) {
+ return $thumbPath;
+ }
+
+ // Otherwise create the thumbnail
+ $thumb = ImageTool::make(public_path() . $image->url);
+ $thumb->fit($width, $height);
+
+ // Create thumbnail folder if it does not exist
+ if (!file_exists(dirname($thumbFilePath))) {
+ mkdir(dirname($thumbFilePath), 0775, true);
+ }
+
+ //Save Thumbnail
+ $thumb->save($thumbFilePath);
+ return $thumbPath;
+ }
+