3 namespace Oxbow\Http\Controllers;
5 use Illuminate\Filesystem\Filesystem as File;
6 use Illuminate\Http\Request;
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;
30 * Returns an image from behind the public-facing application.
31 * @param Request $request
32 * @return \Illuminate\Http\Response
34 public function getImage(Request $request)
36 $cacheTime = 60*60*24;
37 $path = storage_path() . '/' . $request->path();
38 $modifiedTime = $this->file->lastModified($path);
39 $eTag = md5($modifiedTime . $path);
40 $headerLastModified = gmdate('r', $modifiedTime);
41 $headerExpires = gmdate('r', $modifiedTime + $cacheTime);
44 'Last-Modified' => $headerLastModified,
45 'Cache-Control' => 'must-revalidate',
47 'Expires' => $headerExpires,
51 $browserModifiedSince = $request->header('If-Modified-Since');
52 $browserNoneMatch = $request->header('If-None-Match');
53 if($browserModifiedSince !== null && file_exists($path) && ($browserModifiedSince == $headerLastModified || $browserNoneMatch == $eTag)) {
54 return response()->make('', 304, $headers);
57 if(file_exists($path)) {
58 return response()->make(file_get_contents($path), 200, array_merge($headers, [
59 'Content-Type' => $this->file->mimeType($path),
60 'Content-Length' => filesize($path),
67 * Get all images, Paginated
69 * @return \Illuminate\Http\JsonResponse
71 public function getAll($page = 0)
74 $images = DB::table('images')->orderBy('created_at', 'desc')
75 ->skip($page*$pageSize)->take($pageSize)->get();
76 foreach($images as $image) {
77 $image->thumbnail = $this->getThumbnail($image, 150, 150);
79 return response()->json($images);
83 * Get the thumbnail for an image.
89 public function getThumbnail($image, $width = 220, $height = 220)
91 $explodedPath = explode('/', $image->url);
92 array_splice($explodedPath, 3, 0, ['thumbs-' . $width . '-' . $height]);
93 $thumbPath = implode('/', $explodedPath);
94 $thumbFilePath = storage_path() . $thumbPath;
95 if(file_exists($thumbFilePath)) {
100 $thumb = ImageTool::make(storage_path() . $image->url);
101 $thumb->fit($width, $height);
102 if(!file_exists(dirname($thumbFilePath))) {
103 mkdir(dirname($thumbFilePath), 0775, true);
105 $thumb->save($thumbFilePath);
106 return $thumbFilePath;
110 * Handles image uploads for use on pages.
111 * @param Request $request
112 * @return \Illuminate\Http\JsonResponse
114 public function upload(Request $request)
116 $imageUpload = $request->file('file');
117 $name = str_replace(' ', '-', $imageUpload->getClientOriginalName());
118 $imagePath = '/images/' . Date('Y-m-M') . '/';
119 $storagePath = storage_path(). $imagePath;
120 $fullPath = $storagePath . $name;
121 while(file_exists($fullPath)) {
122 $name = substr(sha1(rand()), 0, 3) . $name;
123 $fullPath = $storagePath . $name;
125 $imageUpload->move($storagePath, $name);
126 // Create and save image object
127 $this->image->name = $name;
128 $this->image->url = $imagePath . $name;
129 $this->image->save();
130 return response()->json($this->image);