]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/ImageController.php
Integrated TinyMCE
[bookstack] / app / Http / Controllers / ImageController.php
1 <?php
2
3 namespace Oxbow\Http\Controllers;
4
5 use Illuminate\Filesystem\Filesystem as File;
6 use Illuminate\Http\Request;
7
8 use Intervention\Image\Facades\Image as ImageTool;
9 use Illuminate\Support\Facades\DB;
10 use Oxbow\Http\Requests;
11 use Oxbow\Image;
12
13 class ImageController extends Controller
14 {
15     protected $image;
16     protected $file;
17
18     /**
19      * ImageController constructor.
20      * @param Image $image
21      * @param File $file
22      */
23     public function __construct(Image $image, File $file)
24     {
25         $this->image = $image;
26         $this->file = $file;
27     }
28
29     /**
30      * Returns an image from behind the public-facing application.
31      * @param Request $request
32      * @return \Illuminate\Http\Response
33      */
34     public function getImage(Request $request)
35     {
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);
42
43         $headers = [
44             'Last-Modified' => $headerLastModified,
45             'Cache-Control' => 'must-revalidate',
46             'Pragma' => 'public',
47             'Expires' => $headerExpires,
48             'Etag' => $eTag
49         ];
50
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);
55         }
56
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),
61             ]));
62         }
63         abort(404);
64     }
65
66     /**
67      * Get all images, Paginated
68      * @param int $page
69      * @return \Illuminate\Http\JsonResponse
70      */
71     public function getAll($page = 0)
72     {
73         $pageSize = 25;
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);
78         }
79         return response()->json($images);
80     }
81
82     /**
83      * Get the thumbnail for an image.
84      * @param $image
85      * @param int $width
86      * @param int $height
87      * @return string
88      */
89     public function getThumbnail($image, $width = 220, $height = 220)
90     {
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)) {
96             return $thumbPath;
97         }
98
99         //dd($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);
104         }
105         $thumb->save($thumbFilePath);
106         return $thumbFilePath;
107     }
108
109     /**
110      * Handles image uploads for use on pages.
111      * @param Request $request
112      * @return \Illuminate\Http\JsonResponse
113      */
114     public function upload(Request $request)
115     {
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;
124         }
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);
131     }
132
133
134 }