Skip to content

Commit

Permalink
Merge pull request #1 from gevorgmelkumyan/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
gevorgmelkumyan committed Sep 2, 2018
2 parents 8f14106 + 958cc6b commit 8960234
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea

/vendor/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 <Gevorg Melkumyan>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "gevorgmelkumyan/image-file",
"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.",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Gevorg Melkumyan",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.1.0"
},
"autoload": {
"psr-0": {
"GM": "src/"
}
}
}
3 changes: 3 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!.gitignore
!example.php
173 changes: 173 additions & 0 deletions examples/example.php

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/Error.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace GM\ImageFile;
namespace GM;

class Error {

const WRONG_FORMAT = 'The format of base64 encoded file is not an image.';
const FILESTREAM_ERROR = 'Something went wrong when saving the file.';
}
38 changes: 25 additions & 13 deletions src/ImageFile.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php

namespace GM\ImageFile;
namespace GM;

use \Exception;
use \Error;

class ImageFile {

private $base64String;
private $imageFileString;
private $defaultPath;
private $type;
protected $base64String;
protected $imageFileString;
protected $defaultPath;
protected $type;

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

if ($content === false) {
throw new Exception(Error::WRONG_IMAGE_FORMAT);
throw new Exception(Error::WRONG_FORMAT);
}

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

if ($this->imageFileString === false) {
throw new Exception(Error::WRONG_IMAGE_FORMAT);
throw new Exception(Error::WRONG_FORMAT);
}

$this->defaultPath = '/images';
$this->defaultPath = '';
}

/**
Expand All @@ -53,12 +52,17 @@ public function getImageString() : string {
/**
* Store the image to the storage given by $path and give him a random name started by $prefix.
*
* @param null|string $path format: 'path/to/the/folder', default path: '/images'
* @param null|string $path
* format: 'path/to/my/folder/', default: ''
* @param null|string $prefix
* @param null|string $storageUrl
* format: 'http{s}://mysite.com/storage/'
* The function returns url to the image if this parameter is set.
* Otherwise, it returns the full path to the image.
* @return null|string
* @throws Exception
*/
public function store(?string $path, ?string $prefix) : ?string {
public function store(?string $path = null, ?string $prefix = null, ?string $storageUrl = null) : ?string {

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

fclose($fileStream);

if ($storageUrl) {
return $storageUrl . $fileName;
}

return $fullPath;
}

private function validateBase64String(string $base64String) : void {
/**
* @param string $base64String
* @throws Exception
*/
protected function validateBase64String(string $base64String) : void {

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

throw new Exception(Error::WRONG_IMAGE_FORMAT);
throw new Exception(Error::WRONG_FORMAT);
}
}

Expand Down

0 comments on commit 8960234

Please sign in to comment.