Skip to content

Commit e45328f

Browse files
committed
add compression function to image
1 parent 5046bde commit e45328f

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

readme.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
* Handle files with Laravel Storage.
66
* Load files through Laravel routing instead of public path.
7-
* Save images with thumbs, sizes are customisable.
7+
* Save images with thumbs, compressed image, and sizes are customisable.
88

99
## Installation
1010

@@ -56,6 +56,12 @@ in `config/fileapi.php`
5656
'default_thumbs' => ['S' => '96x96', 'M' => '256x256', 'L' => '480x480'],
5757
```
5858

59+
1. set default image compress quality
60+
61+
```php
62+
'compress_quality' => 90,
63+
```
64+
5965
1. choose whether you want to enable upload directly by url(api)
6066

6167
```php
@@ -109,27 +115,28 @@ $fa_article = new FileApi('/images/article/'); # initiate another instance
109115
```php
110116
$file = $fa
111117
->thumbs([
112-
'S' => '150x100',
113-
'M' => '300x200',
114-
'L' => '450x300'
115-
])
118+
'S' => '150x100',
119+
'M' => '300x200',
120+
'L' => '450x300'
121+
])
116122
->save(\Input::file('main_image'));
117123
```
118124

119125
* make cropped thumbs
120126

121-
```php
122-
$file = $fa->crop()->save(\Input::file('main_image'));
123-
```
127+
```php
128+
$file = $fa->crop()->save(\Input::file('main_image'));
129+
```
124130

125131
### Get image url
126132

127133
```php
128134
$fa->get('wfj412.jpg'); // => get image url of 'L' size
129135
$fa->get('wfj412.jpg', 'M'); // => get image url of 'M' size
130-
$fa->get('wfj412.jpg', 'full); // => get image url of full size
136+
$fa->get('wfj412.jpg', 'full'); // => get image url of full size
137+
$fa->get('wfj412.jpg', 'CP'); // => get image url of compressed one
131138
```
132-
139+
133140
### Delete image and thumbs
134141

135142
```php

src/FileApi.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class FileApi
1313
protected $default_sizes = ['S' => '96x96', 'M' => '256x256', 'L' => '480x480'];
1414
protected $thumb_sizes = null;
1515
protected $shouldCropThumb = false;
16+
protected $compress_quality = 90;
1617

1718
public function __construct($basepath = DIRECTORY_SEPARATOR)
1819
{
@@ -388,4 +389,22 @@ private function getThumbSizes()
388389
return $this->default_sizes;
389390
}
390391
}
392+
393+
private function saveCompress($img, $original_name, $suffix)
394+
{
395+
$compress_name = $this->basepath . $original_name . '_CP.' . $suffix;
396+
$main_image = $original_name . '.' . $suffix;
397+
$tmp_filename = 'tmp/' . $main_image;
398+
399+
$tmp_path = \Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
400+
401+
// save thumbnail image
402+
imagejpeg($img, $tmp_path . $tmp_filename, config('fileapi.compress_quality', $this->compress_quality));
403+
404+
$tmp_file = \Storage::disk('local')->get($tmp_filename);
405+
\Storage::put($compress_name, $tmp_file);
406+
407+
// remove tmp image
408+
\Storage::disk('local')->delete($tmp_filename);
409+
}
391410
}

src/config/fileapi.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
'default_thumbs' => ['S' => '96x96', 'M' => '256x256', 'L' => '480x480'],
2020

21+
'compress_quality' => 90,
22+
2123
/*
2224
|--------------------------------------------------------------------------
2325
| Enable Api upload
@@ -34,4 +36,4 @@
3436

3537
'middlewares' => [], // middlewares that wrap the api upload route
3638

37-
];
39+
];

0 commit comments

Comments
 (0)