├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── config └── imagehandler.php └── src ├── Facades └── ImageHandler.php ├── ImageConfiguration.php ├── ImageHandler.php └── ImageHandlerServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | node_modules/ 3 | npm-debug.log 4 | yarn-error.log 5 | 6 | # Laravel 4 specific 7 | bootstrap/compiled.php 8 | app/storage/ 9 | 10 | # Laravel 5 & Lumen specific 11 | public/storage 12 | public/hot 13 | 14 | # Laravel 5 & Lumen specific with changed public path 15 | public_html/storage 16 | public_html/hot 17 | 18 | storage/*.key 19 | .env 20 | Homestead.yaml 21 | Homestead.json 22 | /.vagrant 23 | .phpunit.result.cache 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Laravel Image Handler 3 | 4 | Optimize and store images in multiple sizes easily. 5 | 6 | 7 | ### Installation steps 8 | ``` 9 | composer require codepane/laravel-image-handler 10 | php artisan vendor:publish --provider="Codepane\LaravelImageHandler\ImageHandlerServiceProvider" 11 | ``` 12 | 13 | ### Configuration 14 | 15 | * After installation is done once you can see imagehandler.php under the config directory. 16 | * You can update dimensions, format, and quality as per your need from a configuration file. 17 | * You can also add a new dimension. 18 | 19 | 20 | 21 | ## Usage 22 | Let's 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 takes the 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 the 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 the original image 45 | ImageHandler::get('original_file_name'); 46 | 47 | // pass dimension as the second argument to get a specific dimension of the file 48 | ImageHandler::get('original_file_name', 'sm'); 49 | } 50 | ``` 51 | 52 | ### Get Image Name 53 | ``` 54 | use ImageHandler; 55 | 56 | public function get() 57 | { 58 | // pass dimension as the second argument to get a specific dimension of the file name 59 | ImageHandler::getFileName('original_file_name', 'sm'); 60 | } 61 | ``` 62 | 63 | ### Delete Image 64 | ``` 65 | use ImageHandler; 66 | 67 | public function delete() 68 | { 69 | ImageHandler::delete('original_file_name'); 70 | } 71 | ``` 72 | 73 | 74 | ## Contributing 75 | 76 | Contributions are always welcome! 77 | 78 | - Make a pull request if you have to contribute to this lovely library! 79 | - We will review and pull your request. 80 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "codepane/laravel-image-handler", 3 | "description": "Optimize and store images in multiple sizes easily.", 4 | "type": "library", 5 | "version": "0.7", 6 | "keywords": [ 7 | "image", 8 | "laravel", 9 | "image-handler" 10 | ], 11 | "require": { 12 | "intervention/image": "^2.7" 13 | }, 14 | "readme": "README.md", 15 | "license": "MIT", 16 | "autoload": { 17 | "psr-4": { 18 | "Codepane\\LaravelImageHandler\\": "src/" 19 | } 20 | }, 21 | "extra": { 22 | "laravel": { 23 | "providers": [ 24 | "Codepane\\LaravelImageHandler\\ImageHandlerServiceProvider" 25 | ], 26 | "aliases": { 27 | "ImageHandler": "Codepane\\LaravelImageHandler\\Facades\\ImageHandler" 28 | } 29 | } 30 | }, 31 | "authors": [ 32 | { 33 | "name": "Sanjay Prajapati", 34 | "email": "sanjay14prajapati1998@gmail.com" 35 | } 36 | ], 37 | "prefer-stable": true 38 | } 39 | -------------------------------------------------------------------------------- /config/imagehandler.php: -------------------------------------------------------------------------------- 1 | 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 | ]; 36 | -------------------------------------------------------------------------------- /src/Facades/ImageHandler.php: -------------------------------------------------------------------------------- 1 | getFormat($imageName, $dimension); 95 | 96 | $fileName = str_replace([' ', '.'], '-', $fileName); 97 | 98 | return $fileName . '.' . $format; 99 | } 100 | 101 | /** 102 | * It takes a file name, splits it into two parts, and then returns the file name with the dimension added to it 103 | * 104 | * @param string fileName The file name of the image. 105 | * @param string dimension The dimension of the image. 106 | * 107 | * @return string The file name with the dimension. 108 | */ 109 | public function getImgName(string $fileName, string $dimension) 110 | { 111 | /* Getting the file extension of the file name. */ 112 | $fileExt = $this->getFormat(); 113 | 114 | $directory = pathinfo($fileName, PATHINFO_DIRNAME); 115 | 116 | /* Getting the file name without the extension. */ 117 | $fileName = pathinfo($fileName, PATHINFO_FILENAME); 118 | 119 | if(!empty($directory) && $directory != '.') 120 | $fileName = $directory . '/' . $fileName; 121 | 122 | /* Splitting the file name into two parts. The first part is the original file name and the second part is the time. */ 123 | $fileNameWithDimension = preg_split('~-(?=[^-]*$)~', $fileName); 124 | 125 | /* Getting the time from the file name. */ 126 | $time = $fileNameWithDimension[1]; 127 | 128 | /* Splitting the file name into two parts. The first part is the original file name and the second part is the time. */ 129 | $origFileName = preg_split('~-(?=[^-]*$)~', $fileNameWithDimension[0])[0]; 130 | 131 | return $this->makeFileName($origFileName, $time, $dimension, $fileExt); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/ImageHandler.php: -------------------------------------------------------------------------------- 1 | getDimensions(); 25 | $storageDisk = $this->getStorageDisk(); 26 | $time = time(); 27 | 28 | if(is_null($imgPath)) 29 | $imgPath = $image->getClientOriginalName(); 30 | 31 | /* Creating a file name for the original image. */ 32 | $fileOriginalName = $this->makeFileName($imgPath, $time, 'orig'); 33 | 34 | $this->makeDirectoryIfDoesNotExist($storageDisk, $fileOriginalName); 35 | 36 | $this->storeImg($storageDisk, $fileOriginalName, file_get_contents($image)); 37 | 38 | foreach($dimensions as $key => $dimension) { 39 | 40 | /* Creating a file name for the image. */ 41 | $fileName = $this->makeFileName($imgPath, $time, $key); 42 | 43 | /* Optimizing the image. */ 44 | $optimizedImg = $this->optimize($image->getRealPath(), $dimension, $this->getFormat()); 45 | 46 | /* Storing the image in the storage disk. */ 47 | $this->storeImg($storageDisk, $fileName, $optimizedImg); 48 | } 49 | 50 | $fileBaseName = pathinfo($fileOriginalName, PATHINFO_BASENAME); 51 | 52 | return $fileBaseName; 53 | } 54 | 55 | /** 56 | * make a directory if does not exist 57 | * 58 | * @param string $storageDisk 59 | * @param string $fileName 60 | * 61 | * @return void 62 | */ 63 | public function makeDirectoryIfDoesNotExist(string $storageDisk, string $fileName) 64 | { 65 | $directory = pathinfo($fileName, PATHINFO_DIRNAME); 66 | 67 | if(($storageDisk == 'local' || $storageDisk == 'public') && !Storage::disk($storageDisk)->exists($directory)) { 68 | $dirPath = Storage::disk($storageDisk)->path($directory); 69 | File::makeDirectory($dirPath, 0777); 70 | } 71 | } 72 | 73 | /** 74 | * It stores an image in a specified disk. 75 | * 76 | * @param string disk The disk you want to store the file on. 77 | * @param string fileName The name of the file you want to store. 78 | * @param string file The file to be stored. 79 | * 80 | * @return void 81 | */ 82 | public function storeImg(string $disk, string $fileName, string $file): void 83 | { 84 | Storage::disk($disk)->put( $fileName, $file); 85 | } 86 | 87 | /** 88 | * It takes an image, resizes it to the dimensions provided, and returns the image in the format provided 89 | * 90 | * @param string image The image to be optimized 91 | * @param array dimensions An array of width and height. 92 | * @param string format The format of the image. 93 | * 94 | * @return string The image is being returned. 95 | */ 96 | public function optimize(string $image, array $dimensions, string $format) : string 97 | { 98 | $manager = (new ImageManager()); 99 | $image = $manager->make($image); 100 | $image->resize($dimensions['width'], $dimensions['height']); 101 | $image->encode($this->getFormat()); 102 | 103 | return $image; 104 | } 105 | 106 | /** 107 | * It returns the file contents of the file with the given name and dimension 108 | * 109 | * @param string fileName The name of the file you want to get. 110 | * @param string dimension The dimension of the image you want to get. 111 | * 112 | * @return The file contents. 113 | */ 114 | public function get(string $fileName, string $dimension = 'orig') 115 | { 116 | if($dimension != 'orig') 117 | $fileName = $this->getImgName($fileName, $dimension); 118 | 119 | return Storage::disk($this->getStorageDisk())->get($fileName); 120 | } 121 | 122 | /** 123 | * It returns the file name of the image 124 | * 125 | * @param string fileName The name of the file you want to get. 126 | * @param string dimension The dimension of the image you want to get. 127 | * 128 | * @return The file name of the image. 129 | */ 130 | public function getFileName(string $fileName, string $dimension = 'orig') 131 | { 132 | if($dimension != 'orig') 133 | $fileName = $this->getImgName($fileName, $dimension); 134 | 135 | return $fileName; 136 | } 137 | 138 | 139 | /** 140 | * It deletes the original file and all the resized versions of it 141 | * 142 | * @param string fileName The name of the file to be deleted. 143 | */ 144 | public function delete(string $fileName): void 145 | { 146 | $dimensions = $this->getDimensions(); 147 | $storageDisk = $this->getStorageDisk(); 148 | 149 | if(!Storage::disk($storageDisk)->exists($fileName)) 150 | throw new Exception("Laravel Image Handler:: File does not exists"); 151 | 152 | $this->removeFile($storageDisk, $fileName); 153 | 154 | foreach($dimensions as $dimension => $val) { 155 | $fileName = $this->getImgName($fileName, $dimension); 156 | 157 | $this->removeFile($storageDisk, $fileName); 158 | } 159 | } 160 | 161 | /** 162 | * It removes a file from a storage disk 163 | * 164 | * @param string storageDisk The name of the storage disk you want to use. 165 | * @param string fileName The name of the file to be deleted. 166 | */ 167 | public function removeFile(string $storageDisk, string $fileName): void 168 | { 169 | Storage::disk($storageDisk)->delete($fileName); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/ImageHandlerServiceProvider.php: -------------------------------------------------------------------------------- 1 | mergeConfigFrom( 12 | __DIR__.'/../config/imagehandler.php', 13 | 'imagehandler'); 14 | 15 | $this->app->bind('imagehandler', function($app) { 16 | return new ImageHandler(); 17 | }); 18 | 19 | } 20 | 21 | public function boot() 22 | { 23 | $this->publishes([ 24 | __DIR__.'/../config/imagehandler.php' => config_path('imagehandler.php'), 25 | ], 'imagehandler'); 26 | 27 | } 28 | } 29 | --------------------------------------------------------------------------------