Skip to content

Commit 01ea242

Browse files
committed
add crop mode toggle, update readme
1 parent 47981de commit 01ea242

File tree

3 files changed

+70
-46
lines changed

3 files changed

+70
-46
lines changed

readme.md

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44

55
Laravel File API is good way to handle files with Laravel Storage.
66

7-
### Install File API
7+
### Installation
88

9-
composer.json:
9+
1. Install File API
1010

11-
"require" : {
12-
"unisharp/laravel-fileapi" : "dev-master"
13-
},
14-
"repositories": {
15-
"type": "git",
16-
"url": "https://github.com/UniSharp/laravel-fileapi.git
17-
}
11+
composer require unisharp/laravel-fileapi
1812

19-
save it and then
13+
1. Set service provider in `config/app.php`
2014

21-
composer update
15+
Unisharp\FileApi\FileApiServiceProvider::class,
16+
17+
1. publish config file
18+
19+
php artisan vendor:publish --tag=fileapi_config
20+
21+
1. fill the path in config/fileapi.php, it will generate routes for your files.
2222

2323
### Initialize File API
2424

@@ -31,44 +31,21 @@ or
3131
$fa = new FileApi('/images'); # initialize it by giving a base path
3232

3333

34-
### Save to Storage By Giving Lravel Upload File
34+
### Save to Storage By Giving Uploaded File
3535

36-
* Normal Usage
37-
38-
get unique filename
36+
* Default Usage : get unique filename
3937

4038
$file = $fa->save(\Input::file('image')); // => wfj412.jpg
4139

42-
or input files is an array
43-
44-
$files = [];
45-
foreach (\Input::file('images') as $file) {
46-
$files []= $fa->save('images');
47-
}
48-
4940
* Custimize your upload file name
5041

51-
$file = $fa->save('image', 'custimized_filename'); // => custimized_filename.jpg
42+
$file = $fa->save(\Input::file('image'), 'custimized_filename'); // => custimized_filename.jpg
5243
5344

5445
### Get file fullpath (abstract path from Laravel Storage)
5546

5647
$fa->getPath('wfj412.jpg'); // => '/images/wfj412.jpg'
5748

58-
### Routing your files
59-
60-
All uploaded files cannot found in public folder, because FileApi use Laravel Storage to store them.
61-
You can write a routing rules to get it. it will response file content and use http cache by default.
62-
63-
Route::get('/upload/{filename}', function ($filename) {
64-
$fa = new FileApi();
65-
return $fa->getResponse($filename);
66-
});
67-
68-
and it can add headers array by optional
69-
70-
return $fa->getResponse($filename, ['Content-Disposition', 'attachement']);
71-
7249
### Parse File Path to URL
7350
if you store your file into cloud storage and you want to get url cloud site,
7451
you can use url() method to get it

src/FileApi.php

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
class FileApi
99
{
1010
protected $basepath;
11-
protected $thumb_sizes = ['S' => '96x96', 'M' => '256x256', 'L' => '480x480'];
11+
protected $default_sizes = ['S' => '96x96', 'M' => '256x256', 'L' => '480x480'];
12+
protected $thumb_sizes = null;
13+
protected $shouldCropThumb = false;
1214

1315
public function __construct($basepath = '/')
1416
{
@@ -46,6 +48,13 @@ public function thumbs($thumb_sizes = array())
4648
return $this;
4749
}
4850

51+
public function crop()
52+
{
53+
$this->shouldCropThumb = true;
54+
55+
return $this;
56+
}
57+
4958
public function save(UploadedFile $upload_file, $cus_name = null)
5059
{
5160
$file = $this->moveFile($upload_file, $cus_name);
@@ -151,7 +160,7 @@ private function moveFile($upload_file, $cus_name)
151160

152161
\File::delete($upload_file->getRealPath());
153162

154-
if (!is_null($img) && !empty($this->thumb_sizes)) {
163+
if (!is_null($img) && !empty($this->getThumbSizes())) {
155164
$this->saveThumb($img, $original_name, $suffix);
156165
}
157166

@@ -200,9 +209,9 @@ private function setTmpImage($upload_file)
200209

201210
private function saveThumb($img, $original_name, $suffix)
202211
{
203-
foreach ($this->thumb_sizes as $size_code => $size) {
204-
if (is_int($size_code) && $size_code < count($this->thumb_sizes)) {
205-
$size_name = $size;
212+
foreach ($this->getThumbSizes() as $size_code => $size) {
213+
if (is_int($size_code) && $size_code < count($this->getThumbSizes())) {
214+
$size_name = 'L';
206215
} else {
207216
$size_name = $size_code;
208217
}
@@ -211,7 +220,7 @@ private function saveThumb($img, $original_name, $suffix)
211220
$main_image = $original_name . '.' . $suffix;
212221
$tmp_filename = 'tmp/' . $main_image;
213222

214-
$tmp_thumb = $this->resizeThumb($img, $size);
223+
$tmp_thumb = $this->resizeOrCropThumb($img, $size);
215224

216225
// save tmp image
217226
\Storage::disk('local')->put($tmp_filename, \Storage::get($this->basepath . $main_image));
@@ -227,25 +236,34 @@ private function saveThumb($img, $original_name, $suffix)
227236
}
228237
}
229238

230-
private function resizeThumb($img, $size)
239+
private function resizeOrCropThumb($img, $size)
231240
{
232241
$width = imagesx($img);
233242
$height = imagesy($img);
234243
$arr_size = explode('x', $size);
235244
$thumb_width = (int)$arr_size[0];
236245
$thumb_height = (int)$arr_size[1];
237246

247+
if ($this->thumbShouldCrop()) {
248+
$img = $this->cropThumb($img, $width, $height, $thumb_width, $thumb_height);
249+
} else {
250+
$this->resizeThumb($width, $height, $thumb_width, $thumb_height);
251+
}
252+
238253
// create a new temporary thumbnail image
239254
$tmp_thumb = imagecreatetruecolor($thumb_width, $thumb_height);
240255

241-
$img = $this->cropThumb($img, $width, $height, $thumb_width, $thumb_height);
242-
243256
// copy and resize original image into thumbnail image
244257
imagecopyresized($tmp_thumb, $img, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
245258

246259
return $tmp_thumb;
247260
}
248261

262+
private function thumbShouldCrop()
263+
{
264+
return $this->shouldCropThumb;
265+
}
266+
249267
private function cropThumb($img, &$width, &$height, $thumb_width, $thumb_height)
250268
{
251269
$image_ratio = $height/$width;
@@ -281,4 +299,31 @@ private function cropThumb($img, &$width, &$height, $thumb_width, $thumb_height)
281299

282300
return $img;
283301
}
302+
303+
private function resizeThumb($width, $height, &$thumb_width, &$thumb_height)
304+
{
305+
$image_ratio = $height/$width;
306+
$thumb_ratio = $thumb_height/$thumb_width;
307+
308+
if ($image_ratio !== $thumb_ratio) {
309+
if ($image_ratio < $thumb_ratio) {
310+
$thumb_height = $thumb_width*$height/$width;
311+
} else if ($image_ratio > $thumb_ratio) {
312+
$thumb_width = $thumb_height*$width/$height;
313+
}
314+
}
315+
}
316+
317+
private function getThumbSizes()
318+
{
319+
$config = config('fileapi.default_thumbs');
320+
321+
if (!is_null($this->thumb_sizes)) {
322+
return $this->thumb_sizes;
323+
} else if (!is_null($config)) {
324+
return $config;
325+
} else {
326+
return $this->default_sizes;
327+
}
328+
}
284329
}

src/config/fileapi.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515

1616
'path' => ['/images/event/'],
1717

18+
'default_thumbs' => ['S' => '96x96', 'M' => '256x256', 'L' => '480x480'],
19+
1820
];

0 commit comments

Comments
 (0)