Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit eb5e73e

Browse files
authored
Merge pull request #42 from joselfonseca/feature/24-render-file-endpoint
Add render Image support, support multipart form uploads
2 parents 12c49b5 + 88f59a1 commit eb5e73e

File tree

7 files changed

+422
-2
lines changed

7 files changed

+422
-2
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Assets;
4+
5+
use Illuminate\Http\Request;
6+
use App\Entities\Assets\Asset;
7+
use App\Http\Controllers\Controller;
8+
use Intervention\Image\Facades\Image;
9+
use Illuminate\Support\Facades\Storage;
10+
use Illuminate\Database\Eloquent\ModelNotFoundException;
11+
12+
/**
13+
* Class RenderFileController.
14+
*/
15+
class RenderFileController extends Controller
16+
{
17+
/**
18+
* @var \App\Entities\Assets\Asset
19+
*/
20+
protected $model;
21+
22+
/**
23+
* RenderFileController constructor.
24+
*
25+
* @param \App\Entities\Assets\Asset $model
26+
*/
27+
public function __construct(Asset $model)
28+
{
29+
$this->model = $model;
30+
}
31+
32+
/**
33+
* @param $uuid
34+
* @param \Illuminate\Http\Request $request
35+
* @return mixed
36+
*/
37+
public function show($uuid, Request $request)
38+
{
39+
try {
40+
$model = $this->model->byUuid($uuid)->firstOrFail();
41+
42+
return $this->renderFile($model, $request->get('width', null), $request->get('height', null));
43+
} catch (ModelNotFoundException $e) {
44+
return $this->renderPlaceholder($request->get('width', null), $request->get('height', null));
45+
}
46+
}
47+
48+
/**
49+
* @param $model
50+
* @param $width
51+
* @param $height
52+
* @return mixed
53+
*/
54+
public function renderFile($model, $width, $height)
55+
{
56+
$image = $this->makeFromPath($width, $height, $model->path);
57+
58+
return $image->response();
59+
}
60+
61+
/**
62+
* @param $width
63+
* @param $height
64+
* @return mixed
65+
*/
66+
public function renderPlaceholder($width, $height)
67+
{
68+
$image = Image::cache(function ($image) use ($width, $height) {
69+
$img = $image->canvas(800, 800, '#FFFFFF');
70+
$this->resize($img, $width, $height);
71+
72+
return $img;
73+
}, 10, true);
74+
75+
return $image->response();
76+
}
77+
78+
/**
79+
* @param $width
80+
* @param $height
81+
* @param $path
82+
* @return mixed
83+
*/
84+
protected function makeFromPath($width, $height, $path)
85+
{
86+
return Image::cache(function ($image) use ($path, $width, $height) {
87+
$img = $image->make(Storage::get($path));
88+
$this->resize($img, $width, $height);
89+
90+
return $img;
91+
}, 10, true);
92+
}
93+
94+
/**
95+
* @param $img
96+
* @param $width
97+
* @param $height
98+
* @return mixed
99+
*/
100+
protected function resize($img, $width, $height)
101+
{
102+
if (! empty($width) && ! empty($height)) {
103+
$img->resize($width, $height);
104+
} elseif (! empty($width)) {
105+
$img->resize($width, null, function ($constraint) {
106+
$constraint->aspectRatio();
107+
});
108+
} elseif (! empty($height)) {
109+
$img->resize(null, $height, function ($constraint) {
110+
$constraint->aspectRatio();
111+
});
112+
}
113+
114+
return $img;
115+
}
116+
}

app/Http/Controllers/Api/Assets/UploadFileController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public function store(Request $request)
7272
'url' => $request->get('url'),
7373
'user' => $request->user(),
7474
]);
75+
} elseif ($request->hasFile('file')) {
76+
$file = $request->file('file')->getRealPath();
77+
$asset = $this->uploadFromDirectFile([
78+
'mime' => $request->file('file')->getClientMimeType(),
79+
'content' => file_get_contents($file),
80+
'Content-Length' => $request->header('Content-Length'),
81+
'user' => $request->user(),
82+
]);
7583
} else {
7684
$body = ! (base64_decode($request->getContent())) ? $request->getContent() : base64_decode($request->getContent());
7785
$asset = $this->uploadFromDirectFile([

app/Transformers/Assets/AssetTransformer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public function transform(Asset $model)
2121
'type' => $model->type,
2222
'path' => $model->path,
2323
'mime' => $model->mime,
24+
'links' => [
25+
'full' => url('api/assets/'.$model->uuid),
26+
'thumb' => url('api/assets/'.$model->uuid.'?width=200&height=200'),
27+
],
2428
'created_at' => $model->created_at->toIso8601String(),
2529
];
2630
}

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"php": "^7.1.3",
1717
"dingo/api": "2.0.0-alpha2",
1818
"fideloper/proxy": "~4.0",
19+
"intervention/image": "^2.4",
20+
"intervention/imagecache": "^2.3",
1921
"laravel/framework": "5.7.*",
2022
"laravel/passport": "^7.0",
2123
"laravel/tinker": "~1.0",

0 commit comments

Comments
 (0)