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

Commit dcf8993

Browse files
committed
Add render Image support, support multipart form uploads
1 parent 12c49b5 commit dcf8993

File tree

7 files changed

+418
-2
lines changed

7 files changed

+418
-2
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
* @package App\Http\Controllers\Api\Assets
16+
*/
17+
class RenderFileController extends Controller
18+
{
19+
/**
20+
* @var \App\Entities\Assets\Asset
21+
*/
22+
protected $model;
23+
24+
/**
25+
* RenderFileController constructor.
26+
*
27+
* @param \App\Entities\Assets\Asset $model
28+
*/
29+
public function __construct(Asset $model)
30+
{
31+
$this->model = $model;
32+
}
33+
34+
/**
35+
* @param $uuid
36+
* @param \Illuminate\Http\Request $request
37+
* @return mixed
38+
*/
39+
public function show($uuid, Request $request)
40+
{
41+
try {
42+
$model = $this->model->byUuid($uuid)->firstOrFail();
43+
return $this->renderFile($model, $request->get('width', null), $request->get('height', null));
44+
} catch (ModelNotFoundException $e) {
45+
return $this->renderPlaceholder($request->get('width', null), $request->get('height', null));
46+
}
47+
}
48+
49+
/**
50+
* @param $model
51+
* @param $width
52+
* @param $height
53+
* @return mixed
54+
*/
55+
public function renderFile($model, $width, $height)
56+
{
57+
$image = $this->makeFromPath($width, $height, $model->path);
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+
return $img;
72+
}, 10, true);
73+
return $image->response();
74+
}
75+
76+
/**
77+
* @param $width
78+
* @param $height
79+
* @param $path
80+
* @return mixed
81+
*/
82+
protected function makeFromPath($width, $height, $path)
83+
{
84+
return Image::cache(function($image) use ($path, $width, $height){
85+
$img = $image->make(Storage::get($path));
86+
$this->resize($img, $width, $height);
87+
return $img;
88+
}, 10, true);
89+
}
90+
91+
/**
92+
* @param $img
93+
* @param $width
94+
* @param $height
95+
* @return mixed
96+
*/
97+
protected function resize($img, $width, $height)
98+
{
99+
if (! empty($width) && ! empty($height)) {
100+
$img->resize($width, $height);
101+
} elseif (! empty($width)) {
102+
$img->resize($width, null, function ($constraint) {
103+
$constraint->aspectRatio();
104+
});
105+
} elseif (! empty($height)) {
106+
$img->resize(null, $height, function ($constraint) {
107+
$constraint->aspectRatio();
108+
});
109+
}
110+
return $img;
111+
}
112+
}

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)