Skip to content

Commit b2154eb

Browse files
committed
add thumbnail upload
1 parent fb0b433 commit b2154eb

File tree

4 files changed

+184
-24
lines changed

4 files changed

+184
-24
lines changed

src/FileApi.php

Lines changed: 112 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class FileApi
99
{
1010
protected $basepath;
11+
protected $thumb_sizes = [];
1112

1213
public function __construct($basepath = '/')
1314
{
@@ -22,6 +23,12 @@ public function __construct($basepath = '/')
2223
$this->basepath = $basepath;
2324
}
2425

26+
public function thumbs($thumb_sizes)
27+
{
28+
$this->thumb_sizes = $thumb_sizes;
29+
return $this;
30+
}
31+
2532
public function save(UploadedFile $upload_file, $cus_name = null)
2633
{
2734
$file = $this->moveFile($upload_file, $cus_name);
@@ -31,42 +38,104 @@ public function save(UploadedFile $upload_file, $cus_name = null)
3138
private function moveFile($upload_file, $cus_name)
3239
{
3340
$suffix = $upload_file->getClientOriginalExtension();
34-
$filename = uniqid() . '.' . $suffix;
35-
if (!empty($cus_name)) {
36-
$filename = $cus_name . '.' .$suffix;
37-
}
3841

39-
switch (\File::mimeType($upload_file)) {
40-
case 'image/jpeg':
41-
case 'image/jpg':
42-
$img = imagecreatefromjpeg($upload_file->getRealPath());
43-
$exif = read_exif_data($upload_file->getRealPath());
44-
if (isset($exif['Orientation'])) {
45-
switch ($exif['Orientation']) {
46-
case 8:
47-
$img = imagerotate($img, 90, 0);
48-
break;
49-
case 3:
50-
$img = imagerotate($img, 180, 0);
51-
break;
52-
case 6:
53-
$img = imagerotate($img, -90, 0);
54-
break;
55-
}
56-
}
57-
58-
imagejpeg($img, $upload_file->getRealPath());
42+
if (empty($cus_name)) {
43+
$original_name = uniqid();
44+
} else {
45+
$original_name = $cus_name;
5946
}
47+
$filename = $original_name . '.' .$suffix;
48+
49+
$img = $this->setTmpImage($upload_file);
6050

6151
\Storage::put(
6252
$this->basepath . $filename,
6353
file_get_contents($upload_file->getRealPath())
6454
);
6555

6656
\File::delete($upload_file->getRealPath());
57+
58+
if (!is_null($img) && !empty($this->thumb_sizes)) {
59+
$this->saveThumb($img, $original_name, $suffix);
60+
}
61+
6762
return $filename;
6863
}
6964

65+
public function saveThumb($img, $original_name, $suffix)
66+
{
67+
foreach ($this->thumb_sizes as $size) {
68+
$width = imagesx($img);
69+
$height = imagesy($img);
70+
71+
$arr_size = explode('x', $size);
72+
$thumb_width = (int)$arr_size[0];
73+
$thumb_height = (int)$arr_size[1];
74+
75+
$thumb_name = $this->basepath . $original_name . '_' . $size . '.' . $suffix;
76+
$main_image = $original_name . '.' . $suffix;
77+
$tmp_filename = 'tmp/' . $main_image;
78+
79+
// create a new temporary thumbnail image
80+
$tmp_thumb = imagecreatetruecolor($thumb_width, $thumb_height);
81+
82+
// copy and resize original image into thumbnail image
83+
imagecopyresized($tmp_thumb, $img, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
84+
85+
// save tmp image
86+
\Storage::disk('local')->put($tmp_filename, \Storage::get($this->basepath . $main_image));
87+
$tmp_path = \Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
88+
89+
// save thumbnail image
90+
imagepng($tmp_thumb, $tmp_path . $tmp_filename);
91+
$tmp_file = \Storage::disk('local')->get($tmp_filename);
92+
\Storage::put($thumb_name, $tmp_file);
93+
94+
// remove tmp image
95+
\Storage::disk('local')->delete($tmp_filename);
96+
}
97+
}
98+
99+
public function setTmpImage($upload_file)
100+
{
101+
$image_types = array('image/png', 'image/gif', 'image/jpeg', 'image/jpg');
102+
103+
if (in_array(\File::mimeType($upload_file), $image_types)) {
104+
switch (\File::mimeType($upload_file)) {
105+
case 'image/png':
106+
$img = imagecreatefrompng($upload_file->getRealPath());
107+
break;
108+
case 'image/gif':
109+
$img = imagecreatefromgif($upload_file->getRealPath());
110+
break;
111+
case 'image/jpeg':
112+
case 'image/jpg':
113+
default:
114+
$img = imagecreatefromjpeg($upload_file->getRealPath());
115+
$exif = read_exif_data($upload_file->getRealPath());
116+
if (isset($exif['Orientation'])) {
117+
switch ($exif['Orientation']) {
118+
case 8:
119+
$img = imagerotate($img, 90, 0);
120+
break;
121+
case 3:
122+
$img = imagerotate($img, 180, 0);
123+
break;
124+
case 6:
125+
$img = imagerotate($img, -90, 0);
126+
break;
127+
}
128+
}
129+
}
130+
131+
imagepng($img, $upload_file->getRealPath());
132+
133+
return $img;
134+
} else {
135+
return null;
136+
}
137+
}
138+
70139
public function getPath($filename)
71140
{
72141
if (mb_substr($this->basepath, -1, 1, 'utf8') != '/') {
@@ -122,4 +191,23 @@ public function getResponse($filename, $headers = [])
122191
abort(404);
123192
}
124193
}
194+
195+
public function drop($filename)
196+
{
197+
// Cut original file name
198+
$dot = strpos($filename, '.');
199+
$origin_name = substr($filename, 0, $dot);
200+
201+
// Find all images in basepath
202+
$allFiles = \Storage::files($this->basepath);
203+
$files = array_filter($allFiles, function ($file) use ($origin_name)
204+
{
205+
return preg_match('/^(.*)'.$origin_name.'(.*)$/', $file);
206+
});
207+
208+
// Delete original image and thumbnails
209+
foreach ($files as $file) {
210+
\Storage::delete($file);
211+
}
212+
}
125213
}

src/FileapiServiceProvider.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php namespace Unisharp\FileApi;
2+
3+
use Illuminate\Support\ServiceProvider;
4+
5+
6+
/**
7+
* Class FileapiServiceProvider
8+
* @package Unisharp\Fileapi
9+
*/
10+
class FileApiServiceProvider extends ServiceProvider {
11+
12+
/**
13+
* Bootstrap the application services.
14+
*
15+
* @return void
16+
*/
17+
public function boot()
18+
{
19+
include __DIR__ . '/route.php';
20+
21+
$this->publishes([
22+
__DIR__ . '/config/fileapi.php' => config_path('fileapi.php', 'config'),
23+
], 'fileapi_config');
24+
25+
}
26+
27+
/**
28+
* Register the service provider.
29+
*
30+
* @return void
31+
*/
32+
public function register()
33+
{
34+
35+
}
36+
}

src/config/fileapi.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| File paths
8+
|--------------------------------------------------------------------------
9+
|
10+
| 'path' => ['/images/event/']
11+
|
12+
| Gets file from '/storage/image/event/' directory.
13+
|
14+
*/
15+
16+
'path' => ['/images/event/'],
17+
18+
];

src/route.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
$paths = config('fileapi.path');
4+
5+
if (!empty($paths)) {
6+
foreach ($paths as $path) {
7+
Route::get($path.'{filename}', function ($filename) use ($path) {
8+
try {
9+
$file = \Storage::get($path . $filename);
10+
return response($file, 200)->header('Content-Type', \Storage::mimeType($file));
11+
} catch (\League\Flysystem\FileNotFoundException $e) {
12+
abort(404);
13+
}
14+
});
15+
}
16+
}
17+
18+

0 commit comments

Comments
 (0)