3 namespace Oxbow\Http\Controllers;
5 use Illuminate\Filesystem\Filesystem as File;
6 use Illuminate\Http\Request;
8 use Illuminate\Support\Facades\Auth;
9 use Intervention\Image\Facades\Image as ImageTool;
10 use Illuminate\Support\Facades\DB;
11 use Oxbow\Http\Requests;
14 class ImageController extends Controller
20 * ImageController constructor.
24 public function __construct(Image $image, File $file)
26 $this->image = $image;
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, 3, 0, ['thumbs-' . $width . '-' . $height]);
99 $thumbPath = implode('/', $explodedPath);
100 $thumbFilePath = storage_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(storage_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 $imageUpload = $request->file('file');
129 $name = str_replace(' ', '-', $imageUpload->getClientOriginalName());
130 $imagePath = '/images/' . Date('Y-m-M') . '/';
131 $storagePath = storage_path(). $imagePath;
132 $fullPath = $storagePath . $name;
133 while(file_exists($fullPath)) {
134 $name = substr(sha1(rand()), 0, 3) . $name;
135 $fullPath = $storagePath . $name;
137 $imageUpload->move($storagePath, $name);
138 // Create and save image object
139 $this->image->name = $name;
140 $this->image->url = $imagePath . $name;
141 $this->image->created_by = Auth::user()->id;
142 $this->image->updated_by = Auth::user()->id;
143 $this->image->save();
144 $this->image->thumbnail = $this->getThumbnail($this->image, 150, 150);
145 return response()->json($this->image);