3 namespace Oxbow\Http\Controllers;
5 use Illuminate\Filesystem\Filesystem as File;
6 use Illuminate\Http\Request;
7 use Illuminate\Support\Facades\Auth;
8 use Intervention\Image\Facades\Image as ImageTool;
9 use Illuminate\Support\Facades\DB;
10 use Oxbow\Http\Requests;
13 class ImageController extends Controller
19 * ImageController constructor.
23 public function __construct(Image $image, File $file)
25 $this->image = $image;
27 parent::__construct();
31 * Returns an image from behind the public-facing application.
32 * @param Request $request
33 * @return \Illuminate\Http\Response
35 public function getImage(Request $request)
37 $cacheTime = 60 * 60 * 24;
38 $path = storage_path() . '/' . $request->path();
39 $modifiedTime = $this->file->lastModified($path);
40 $eTag = md5($modifiedTime . $path);
41 $headerLastModified = gmdate('r', $modifiedTime);
42 $headerExpires = gmdate('r', $modifiedTime + $cacheTime);
45 'Last-Modified' => $headerLastModified,
46 'Cache-Control' => 'must-revalidate',
48 'Expires' => $headerExpires,
52 $browserModifiedSince = $request->header('If-Modified-Since');
53 $browserNoneMatch = $request->header('If-None-Match');
54 if ($browserModifiedSince !== null && file_exists($path) && ($browserModifiedSince == $headerLastModified || $browserNoneMatch == $eTag)) {
55 return response()->make('', 304, $headers);
58 if (file_exists($path)) {
59 return response()->make(file_get_contents($path), 200, array_merge($headers, [
60 'Content-Type' => $this->file->mimeType($path),
61 'Content-Length' => filesize($path),
68 * Get all images, Paginated
70 * @return \Illuminate\Http\JsonResponse
72 public function getAll($page = 0)
75 $images = DB::table('images')->orderBy('created_at', 'desc')
76 ->skip($page * $pageSize)->take($pageSize)->get();
77 foreach ($images as $image) {
78 $image->thumbnail = $this->getThumbnail($image, 150, 150);
80 $hasMore = count(DB::table('images')->orderBy('created_at', 'desc')
81 ->skip(($page + 1) * $pageSize)->take($pageSize)->get()) > 0;
82 return response()->json([
89 * Get the thumbnail for an image.
95 public function getThumbnail($image, $width = 220, $height = 220)
97 $explodedPath = explode('/', $image->url);
98 array_splice($explodedPath, 4, 0, ['thumbs-' . $width . '-' . $height]);
99 $thumbPath = implode('/', $explodedPath);
100 $thumbFilePath = public_path() . $thumbPath;
102 // Return the thumbnail url path if already exists
103 if (file_exists($thumbFilePath)) {
107 // Otherwise create the thumbnail
108 $thumb = ImageTool::make(public_path() . $image->url);
109 $thumb->fit($width, $height);
111 // Create thumbnail folder if it does not exist
112 if (!file_exists(dirname($thumbFilePath))) {
113 mkdir(dirname($thumbFilePath), 0775, true);
117 $thumb->save($thumbFilePath);
122 * Handles image uploads for use on pages.
123 * @param Request $request
124 * @return \Illuminate\Http\JsonResponse
126 public function upload(Request $request)
128 $this->checkPermission('image-create');
129 $imageUpload = $request->file('file');
130 $name = str_replace(' ', '-', $imageUpload->getClientOriginalName());
131 $storageName = substr(sha1(time()), 0, 10) . '-' . $name;
132 $imagePath = '/uploads/images/' . Date('Y-m-M') . '/';
133 $storagePath = public_path() . $imagePath;
134 $fullPath = $storagePath . $storageName;
135 while (file_exists($fullPath)) {
136 $storageName = substr(sha1(rand()), 0, 3) . $storageName;
137 $fullPath = $storagePath . $storageName;
139 $imageUpload->move($storagePath, $storageName);
140 // Create and save image object
141 $this->image->name = $name;
142 $this->image->url = $imagePath . $storageName;
143 $this->image->created_by = Auth::user()->id;
144 $this->image->updated_by = Auth::user()->id;
145 $this->image->save();
146 $this->image->thumbnail = $this->getThumbnail($this->image, 150, 150);
147 return response()->json($this->image);
151 * Update image details
153 * @param Request $request
154 * @return \Illuminate\Http\JsonResponse
156 public function update($imageId, Request $request)
158 $this->checkPermission('image-update');
159 $this->validate($request, [
160 'name' => 'required|min:2|string'
162 $image = $this->image->findOrFail($imageId);
163 $image->fill($request->all());
165 return response()->json($this->image);
169 * Deletes an image and all thumbnail/image files
171 * @return \Illuminate\Http\JsonResponse
173 public function destroy($id)
175 $this->checkPermission('image-delete');
176 $image = $this->image->findOrFail($id);
179 $folder = public_path() . dirname($image->url);
180 $fileName = basename($image->url);
183 foreach (glob($folder . '/*') as $file) {
185 $thumbName = $file . '/' . $fileName;
186 if (file_exists($file)) {
189 // Remove thumb folder if empty
190 if (count(glob($file . '/*')) === 0) {
196 // Delete file and database entry
197 unlink($folder . '/' . $fileName);
200 // Delete parent folder if empty
201 if (count(glob($folder . '/*')) === 0) {
204 return response()->json('Image Deleted');