Skip to content

Commit 8960234

Browse files
Merge pull request #1 from gevorgmelkumyan/development
Development
2 parents 8f14106 + 958cc6b commit 8960234

File tree

7 files changed

+246
-14
lines changed

7 files changed

+246
-14
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
.idea
2+
3+
/vendor/

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 <Gevorg Melkumyan>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "gevorgmelkumyan/image-file",
3+
"description": "The lib contains ImageFile class that allows to create and save image files provided by base64 formatted strings\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[D\u001b[C\u001b[C\u001b[C\u001b[C\u001bencoded strings.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Gevorg Melkumyan",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=7.1.0"
14+
},
15+
"autoload": {
16+
"psr-0": {
17+
"GM": "src/"
18+
}
19+
}
20+
}

examples/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*
2+
!.gitignore
3+
!example.php

examples/example.php

+173
Large diffs are not rendered by default.

src/Error.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3-
namespace GM\ImageFile;
3+
namespace GM;
44

55
class Error {
66

77
const WRONG_FORMAT = 'The format of base64 encoded file is not an image.';
8+
const FILESTREAM_ERROR = 'Something went wrong when saving the file.';
89
}

src/ImageFile.php

+25-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?php
22

3-
namespace GM\ImageFile;
3+
namespace GM;
44

55
use \Exception;
6-
use \Error;
76

87
class ImageFile {
98

10-
private $base64String;
11-
private $imageFileString;
12-
private $defaultPath;
13-
private $type;
9+
protected $base64String;
10+
protected $imageFileString;
11+
protected $defaultPath;
12+
protected $type;
1413

1514
/**
1615
* ImageFile constructor.
@@ -27,18 +26,18 @@ public function __construct(string $base64String) {
2726
$content = explode(';base64,', $base64WithoutDataType[1]);
2827

2928
if ($content === false) {
30-
throw new Exception(Error::WRONG_IMAGE_FORMAT);
29+
throw new Exception(Error::WRONG_FORMAT);
3130
}
3231

3332
$this->type = $content[0];
3433
$this->base64String = $content[1];
3534
$this->imageFileString = base64_decode($this->base64String);
3635

3736
if ($this->imageFileString === false) {
38-
throw new Exception(Error::WRONG_IMAGE_FORMAT);
37+
throw new Exception(Error::WRONG_FORMAT);
3938
}
4039

41-
$this->defaultPath = '/images';
40+
$this->defaultPath = '';
4241
}
4342

4443
/**
@@ -53,12 +52,17 @@ public function getImageString() : string {
5352
/**
5453
* Store the image to the storage given by $path and give him a random name started by $prefix.
5554
*
56-
* @param null|string $path format: 'path/to/the/folder', default path: '/images'
55+
* @param null|string $path
56+
* format: 'path/to/my/folder/', default: ''
5757
* @param null|string $prefix
58+
* @param null|string $storageUrl
59+
* format: 'http{s}://mysite.com/storage/'
60+
* The function returns url to the image if this parameter is set.
61+
* Otherwise, it returns the full path to the image.
5862
* @return null|string
5963
* @throws Exception
6064
*/
61-
public function store(?string $path, ?string $prefix) : ?string {
65+
public function store(?string $path = null, ?string $prefix = null, ?string $storageUrl = null) : ?string {
6266

6367
$fileName = uniqid($prefix ?? 'IM') . '.' . $this->type;
6468
$fullPath = ($path ?? $this->defaultPath) . $fileName;
@@ -75,15 +79,23 @@ public function store(?string $path, ?string $prefix) : ?string {
7579

7680
fclose($fileStream);
7781

82+
if ($storageUrl) {
83+
return $storageUrl . $fileName;
84+
}
85+
7886
return $fullPath;
7987
}
8088

81-
private function validateBase64String(string $base64String) : void {
89+
/**
90+
* @param string $base64String
91+
* @throws Exception
92+
*/
93+
protected function validateBase64String(string $base64String) : void {
8294

8395
if (strpos($base64String, 'data:image/') === false ||
8496
strpos($base64String, ';base64,') === false) {
8597

86-
throw new Exception(Error::WRONG_IMAGE_FORMAT);
98+
throw new Exception(Error::WRONG_FORMAT);
8799
}
88100
}
89101

0 commit comments

Comments
 (0)