Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit a146861

Browse files
committed
ImageResizer With Intervention Image & Filesystem Storage
1 parent f51ba95 commit a146861

14 files changed

+1099
-0
lines changed

composer.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "iconscout/laravel-image-resizer",
3+
"description": "Laravel 5.5 Package for Image uploading, auto-resizing and retriving library.",
4+
"type": "library",
5+
"require": {
6+
"php": ">=7.0",
7+
"intervention/image": "^2.4",
8+
"guzzlehttp/guzzle": "^6.3"
9+
},
10+
"require-dev": {
11+
"phpunit/phpunit": "^6.5",
12+
"phpspec/phpspec": "^4.2"
13+
},
14+
"license": "MIT",
15+
"authors": [
16+
{
17+
"name": "Arpan Rank",
18+
"email": "arpan@iconscout.com"
19+
},
20+
{
21+
"name": "Tarun Manguliya",
22+
"email": "tarun@iconscout.com"
23+
}
24+
],
25+
"minimum-stability": "dev",
26+
"autoload": {
27+
"psr-4": {
28+
"Iconscout\\ImageResizer\\": "src"
29+
}
30+
},
31+
"extra": {
32+
"laravel": {
33+
"providers": [
34+
"Iconscout\\ImageResizer\\ImageResizerServiceProvider"
35+
],
36+
"aliases": {
37+
"ImageResizer": "Iconscout\\ImageResizer\\ImageResizerFacade"
38+
}
39+
}
40+
}
41+
}

src/Config.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Iconscout\ImageResizer;
4+
5+
class Config
6+
{
7+
protected $settings = [];
8+
9+
protected $fallback;
10+
11+
public function __construct(array $config)
12+
{
13+
$this->settings = $config;
14+
}
15+
16+
public function get($key, $default = null)
17+
{
18+
if (! array_key_exists($key, $this->settings)) {
19+
return $this->getDefault($key, $default);
20+
}
21+
22+
return $this->settings[$key];
23+
}
24+
25+
public function has($key)
26+
{
27+
if (array_key_exists($key, $this->settings)) {
28+
return true;
29+
}
30+
31+
return $this->fallback instanceof Config ? $this->fallback->has($key) : false;
32+
}
33+
34+
protected function getDefault($key, $default)
35+
{
36+
if (! $this->fallback) {
37+
return $default;
38+
}
39+
40+
return $this->fallback->get($key, $default);
41+
}
42+
43+
public function setFallback(Config $fallback)
44+
{
45+
$this->fallback = $fallback;
46+
return $this;
47+
}
48+
}

src/ConfigAwareTrait.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Iconscout\ImageResizer;
4+
5+
trait ConfigAwareTrait
6+
{
7+
protected $config;
8+
9+
protected function setConfig($config)
10+
{
11+
$this->config = $config ? Util::ensureConfig($config) : new Config;
12+
}
13+
14+
public function getConfig()
15+
{
16+
return $this->config;
17+
}
18+
19+
protected function prepareConfig(array $config)
20+
{
21+
$config = new Config($config);
22+
23+
$config->setFallback($this->getConfig());
24+
25+
return $config;
26+
}
27+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Iconscout\ImageResizer\Exceptions;
4+
5+
use Exception as BaseException;
6+
7+
class InvalidInputException extends BaseException
8+
{
9+
/**
10+
* @var string
11+
*/
12+
protected $url;
13+
14+
/**
15+
* Constructor.
16+
*
17+
* @param string $url
18+
* @param int $code
19+
* @param BaseException $previous
20+
*/
21+
public function __construct($url, $code = 0, BaseException $previous = null)
22+
{
23+
$this->url = $url;
24+
parent::__construct('File having invalid url: ' . $this->getUrl(), $code, $previous);
25+
}
26+
27+
/**
28+
* Get the url which was found.
29+
*
30+
* @return string
31+
*/
32+
public function getUrl()
33+
{
34+
return $this->url;
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Iconscout\ImageResizer\Exceptions;
4+
5+
use Exception as BaseException;
6+
7+
class InvalidTypeException extends BaseException
8+
{
9+
/**
10+
* @var string
11+
*/
12+
protected $type;
13+
14+
/**
15+
* Constructor.
16+
*
17+
* @param string $type
18+
* @param int $code
19+
* @param BaseException $previous
20+
*/
21+
public function __construct($type, $code = 0, BaseException $previous = null)
22+
{
23+
$this->type = $type;
24+
parent::__construct('Invalid imageresizer type: ' . $this->getType(), $code, $previous);
25+
}
26+
27+
/**
28+
* Get the type which was found.
29+
*
30+
* @return string
31+
*/
32+
public function getType()
33+
{
34+
return $this->type;
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Iconscout\ImageResizer\Exceptions;
4+
5+
use Exception as BaseException;
6+
7+
class TooLongFileNameException extends BaseException
8+
{
9+
/**
10+
* @var string
11+
*/
12+
protected $filename;
13+
14+
/**
15+
* Constructor.
16+
*
17+
* @param string $filename
18+
* @param int $code
19+
* @param BaseException $previous
20+
*/
21+
public function __construct($filename, $code = 0, BaseException $previous = null)
22+
{
23+
$this->filename = $filename;
24+
parent::__construct('File having too long filename: ' . $this->getFileName(), $code, $previous);
25+
}
26+
27+
/**
28+
* Get the filename which was found.
29+
*
30+
* @return string
31+
*/
32+
public function getFileName()
33+
{
34+
return $this->filename;
35+
}
36+
}

src/ImageFile.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
namespace Iconscout\ImageResizer;
4+
5+
class ImageFile
6+
{
7+
/**
8+
* Mime type
9+
*
10+
* @var string
11+
*/
12+
public $mime;
13+
14+
/**
15+
* Original Name given by user of current file
16+
*
17+
* @var string
18+
*/
19+
public $originalname;
20+
21+
/**
22+
* Filename of current file (without extension)
23+
*
24+
* @var string
25+
*/
26+
public $filename;
27+
28+
/**
29+
* File extension of current file
30+
*
31+
* @var string
32+
*/
33+
public $extension;
34+
35+
/**
36+
* File dimensions of current file
37+
*
38+
* @var string
39+
*/
40+
public $dimensions;
41+
42+
/**
43+
* Full Location of current file
44+
*
45+
* @var string
46+
*/
47+
public $fullpath;
48+
49+
50+
public function __construct(string $path = null)
51+
{
52+
if (! empty($path)) {
53+
$this->setFileInfoFromPath($path);
54+
}
55+
}
56+
57+
/**
58+
* File name of current file
59+
*
60+
* @var string
61+
*/
62+
63+
public function getBaseName()
64+
{
65+
return "{$this->filename}.{$this->extension}";
66+
}
67+
68+
/**
69+
* Sets all instance properties from given path
70+
*
71+
* @param string $path
72+
*/
73+
public function setFileInfoFromPath($path)
74+
{
75+
$info = pathinfo($path);
76+
$this->fullpath = $path;
77+
$this->dirname = array_key_exists('dirname', $info) ? $info['dirname'] : null;
78+
$this->originalname = array_key_exists('basename', $info) ? $info['basename'] : null;
79+
$this->filename = array_key_exists('filename', $info) ? $info['filename'] : null;
80+
$this->extension = array_key_exists('extension', $info) ? $info['extension'] : null;
81+
82+
if (file_exists($path) && is_file($path)) {
83+
$this->dimensions = $this->dimensions($path);
84+
$this->mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);
85+
}
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* Get file size
92+
*
93+
* @return mixed
94+
*/
95+
public function filesize()
96+
{
97+
$path = $this->basePath();
98+
99+
if (file_exists($path) && is_file($path)) {
100+
return filesize($path);
101+
}
102+
103+
return false;
104+
}
105+
106+
/**
107+
* Get image file size
108+
*
109+
* @return mixed
110+
*/
111+
public function dimensions()
112+
{
113+
if (empty($this->dimensions)){
114+
$path = $this->fullpath;
115+
116+
if (file_exists($path) && is_file($path)) {
117+
118+
$sizes = getimagesize($path);
119+
$dimensions['width'] = $sizes[0];
120+
$dimensions['height'] = $sizes[1];
121+
122+
// We need to use exif data as getimagesize provides invalid width, height if image is rotated
123+
$exif = @exif_read_data($path);
124+
125+
if (! empty($exif['Orientation'])) {
126+
if ($exif['Orientation'] === 8 || $exif['Orientation'] === 6) {
127+
// 8 = CW Rotate Image to get original
128+
// 6 = CCW Rotate Image to get original
129+
130+
// Store width as height & height as width
131+
$height = $dimensions['width'];
132+
$width = $dimensions['height'];
133+
134+
$dimensions['width'] = $width;
135+
$dimensions['height'] = $height;
136+
}
137+
}
138+
139+
$this->dimensions = $dimensions;
140+
}
141+
}
142+
143+
return $this->dimensions;
144+
}
145+
146+
/**
147+
* Check image file is valid or not
148+
*
149+
* @return string
150+
*/
151+
public function isValid()
152+
{
153+
return exif_imagetype($this->fullpath);
154+
}
155+
156+
/**
157+
* Get file path by string
158+
*
159+
* @return string
160+
*/
161+
public function __toString ()
162+
{
163+
return $this->getBaseName();
164+
}
165+
}

0 commit comments

Comments
 (0)