Skip to content

Commit 2b9e93d

Browse files
committed
Init commit with init features
0 parents  commit 2b9e93d

File tree

8 files changed

+458
-0
lines changed

8 files changed

+458
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Sanjay Prajapati
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.

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
# Laravel Image Handler
3+
4+
Handle image in multiple different size with optimization.
5+
6+
7+
### Installation steps
8+
```
9+
composer require codepane/laravel-image-handler
10+
php artisan vendor:publish --provider="codepane\LaravelImageHandler\ImageHandlerServiceProvider" --tag=config
11+
```
12+
13+
### Configuration
14+
15+
* After installation done once you can see imagehandler.php under the config dirctory.
16+
* You can udpate dimensions, format and quality as per your need from configuration file.
17+
* You can also add new dimension.
18+
19+
20+
21+
## Usage
22+
Lets deep dive into this package for how to use it
23+
24+
### Store Image
25+
```
26+
use ImageHandler;
27+
28+
public function store()
29+
{
30+
// its take default file name as it is
31+
ImageHandler::store($request->file);
32+
33+
// in 2nd argument you can pass your custom file name with or without path
34+
ImageHandler::store($request->file, 'file_name_with_or_without_path');
35+
}
36+
```
37+
38+
### Get Image
39+
```
40+
use ImageHandler;
41+
42+
public function get()
43+
{
44+
// this will return original image
45+
ImageHandler::get('original_file_name');
46+
47+
// pass dimension as second argument for get specific dimension of file
48+
ImageHandler::get('original_file_name', 'sm');
49+
}
50+
```
51+
52+
### Delete Image
53+
```
54+
use ImageHandler;
55+
56+
public function delete()
57+
{
58+
ImageHandler::delete('original_file_name');
59+
}
60+
```
61+
62+
63+
## Contributing
64+
65+
Contributions are always welcome!
66+
67+
- Make pull request if you have to contribute for this lovely library!
68+
- We will review and pull your request.

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "codepane/laravel-image-handler",
3+
"description": "Handle image in multiple different size with optimization.",
4+
"type": "library",
5+
"require": {
6+
"intervention/image": "^2.7"
7+
},
8+
"license": "MIT",
9+
"autoload": {
10+
"psr-4": {
11+
"Codepane\\LaravelImageHandler\\": "src/"
12+
}
13+
},
14+
"extra": {
15+
"laravel": {
16+
"providers": [
17+
"Codepane\\LaravelImageHandler\\ImageHandlerServiceProvider"
18+
],
19+
"aliases": {
20+
"ImageHandler": "Codepane\\LaravelImageHandler\\Facades\\ImageHandler"
21+
}
22+
}
23+
},
24+
"authors": [
25+
{
26+
"name": "Sanjay Prajapati",
27+
"email": "[email protected]"
28+
}
29+
]
30+
}

config/imagehandler.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
return [
4+
5+
// Set the default image quality.
6+
'quality' => env('IMAGE_HANDLER_QUALITY', 75),
7+
8+
// YOU CAN EDIT THIS DIMENSIONS AS PER YOU NEED
9+
'dimensions' => [
10+
/* This is the default image size for the small image. */
11+
'sm' => [
12+
'width' => 80,
13+
'height' => 80
14+
],
15+
/* Setting the default image size for the medium image. */
16+
'md' => [
17+
'width' => 200,
18+
'height' => 200
19+
],
20+
/* This is the default image size for the large image. */
21+
'lg' => [
22+
'width' => 450,
23+
'height' => 450
24+
],
25+
26+
],
27+
28+
/**
29+
* The readable image formats depend on the choosen driver (GD or Imagick) and your local configuration.
30+
*
31+
* Formats:
32+
* png, jpeg, gif, webp, tif, bmp, ico, psd, webp
33+
*/
34+
'format' => env('IMAGE_HANDLER_FORMAT', 'webp')
35+
];

src/Facades/ImageHandler.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace CodePane\LaravelImageHandler\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class ImageHandler extends Facade
8+
{
9+
protected static function getFacadeAccessor()
10+
{
11+
return 'imagehandler';
12+
}
13+
}

src/ImageConfiguration.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
namespace Codepane\LaravelImageHandler;
4+
5+
use Exception;
6+
7+
/**
8+
* trait image configuration
9+
*/
10+
trait ImageConfiguration
11+
{
12+
/**
13+
* get quality for image
14+
*
15+
* @return int|string
16+
*/
17+
public function getQuality(): int|string
18+
{
19+
$quality = config('imagehandler.quality');
20+
21+
if(is_null($quality) || empty($quality))
22+
throw new Exception("Laravel Image Handler:: Quality not should be null or empty!");
23+
24+
return $quality;
25+
}
26+
27+
/**
28+
* get dimensions for image
29+
*
30+
* @return array|string
31+
*/
32+
public function getDimensions(): array|string
33+
{
34+
$dimensions = config('imagehandler.dimensions');
35+
36+
if(!$dimensions && count($dimensions) == 0)
37+
throw new Exception("Laravel Image Handler:: Dimensions are not valid. At least one dimension is mandatory!");
38+
39+
return $dimensions;
40+
}
41+
42+
/**
43+
* get storage disk for where to store image
44+
*
45+
* @return string
46+
*/
47+
public function getStorageDisk()
48+
{
49+
return config('filesystems.default');
50+
}
51+
52+
/**
53+
* It returns the format of the image
54+
*
55+
* @param string fileName The name of the file you want to get the format of.
56+
* @param string dimension The dimension of the image.
57+
*
58+
* @return string The format of the image.
59+
*/
60+
public function getFormat(string $fileName = null, string $dimension = null)
61+
{
62+
$format = config('imagehandler.format');
63+
64+
$allowedFormats = ['webp', 'jpeg', 'jpg', 'png'];
65+
66+
if(is_null($format) || empty($format))
67+
throw new Exception("Laravel Image Handler:: Format should be exist or not null!");
68+
69+
if(!in_array($format, $allowedFormats))
70+
throw new Exception("Laravel Image Handler:: Format should be webp or jpeg or jpeg or png!");
71+
72+
return (!is_null($fileName) && !is_null($dimension) && $dimension == 'orig') ? pathInfo($fileName, PATHINFO_EXTENSION) : $format;
73+
}
74+
75+
76+
/**
77+
* It takes an image name, a time, and a dimension, and returns a new image name
78+
*
79+
* @param string imageName The name of the image file.
80+
* @param int time The time the image was last modified.
81+
* @param string dimension The dimension of the image.
82+
*
83+
* @return string The file name of the image.
84+
*/
85+
public function makeFileName(string $imageName, int $time, string $dimension = null): string
86+
{
87+
$fileName = pathinfo($imageName, PATHINFO_FILENAME) . '-' . $dimension . '-'. $time;
88+
89+
$directory = pathinfo($imageName, PATHINFO_DIRNAME);
90+
91+
if(!empty($directory) && $directory != '.')
92+
$fileName = $directory . '/' . $fileName;
93+
94+
$format = $this->getFormat($imageName, $dimension);
95+
96+
return $fileName . '.' . $format;
97+
}
98+
99+
/**
100+
* It takes a file name, splits it into two parts, and then returns the file name with the
101+
* dimension added to it
102+
*
103+
* @param string fileName The file name of the image.
104+
* @param string dimension The dimension of the image.
105+
*
106+
* @return string The file name with the dimension.
107+
*/
108+
public function getFileName(string $fileName, string $dimension)
109+
{
110+
/* Getting the file extension of the file name. */
111+
$fileExt = $this->getFormat();
112+
113+
$directory = pathinfo($fileName, PATHINFO_DIRNAME);
114+
115+
/* Getting the file name without the extension. */
116+
$fileName = pathinfo($fileName, PATHINFO_FILENAME);
117+
118+
if(!empty($directory) && $directory != '.')
119+
$fileName = $directory . '/' . $fileName;
120+
121+
/* Splitting the file name into two parts. The first part is the original file name and the second part is the time. */
122+
$fileNameWithDimension = preg_split('~-(?=[^-]*$)~', $fileName);
123+
124+
/* Getting the time from the file name. */
125+
$time = $fileNameWithDimension[1];
126+
127+
/* Splitting the file name into two parts. The first part is the original file name and the second part is the time. */
128+
$origFileName = preg_split('~-(?=[^-]*$)~', $fileNameWithDimension[0])[0];
129+
130+
return $this->makeFileName($origFileName, $time, $dimension, $fileExt);
131+
}
132+
}

0 commit comments

Comments
 (0)