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 return response()->json($images);
84 * Get the thumbnail for an image.
90 public function getThumbnail($image, $width = 220, $height = 220)
92 $explodedPath = explode('/', $image->url);
93 array_splice($explodedPath, 3, 0, ['thumbs-' . $width . '-' . $height]);
94 $thumbPath = implode('/', $explodedPath);
95 $thumbFilePath = storage_path() . $thumbPath;
96 if(file_exists($thumbFilePath)) {
100 //dd($thumbFilePath);
101 $thumb = ImageTool::make(storage_path() . $image->url);
102 $thumb->fit($width, $height);
103 if(!file_exists(dirname($thumbFilePath))) {
104 mkdir(dirname($thumbFilePath), 0775, true);
106 $thumb->save($thumbFilePath);
107 return $thumbFilePath;
111 * Handles image uploads for use on pages.
112 * @param Request $request
113 * @return \Illuminate\Http\JsonResponse
115 public function upload(Request $request)
117 $imageUpload = $request->file('file');
118 $name = str_replace(' ', '-', $imageUpload->getClientOriginalName());
119 $imagePath = '/images/' . Date('Y-m-M') . '/';
120 $storagePath = storage_path(). $imagePath;
121 $fullPath = $storagePath . $name;
122 while(file_exists($fullPath)) {
123 $name = substr(sha1(rand()), 0, 3) . $name;
124 $fullPath = $storagePath . $name;
126 $imageUpload->move($storagePath, $name);
127 // Create and save image object
128 $this->image->name = $name;
129 $this->image->url = $imagePath . $name;
130 $this->image->created_by = Auth::user()->id;
131 $this->image->updated_by = Auth::user()->id;
132 $this->image->save();
133 return response()->json($this->image);