diff --git a/CHANGELOG.md b/CHANGELOG.md index 447be05..7426087 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ All notable changes to `golchha21/resmushit` will be documented in this file +## 1.1.0 - 2020-04-18 + +- Ability to save the original images +- configuration value load error now rectified +- Reformatted the code +- Updated the class documentation +- Updated CHANGELOG +- Version bump composer.json + ## 1.0.4 - 2020-04-17 - Made the const Private diff --git a/README.md b/README.md index a9ede7e..ab58e90 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ reSmushit for Laravel ========== [![Latest Version on Packagist](https://img.shields.io/packagist/v/golchha21/resmushit.svg?style=flat-square)](https://packagist.org/packages/golchha21/resmushit) -[![Total Downloads](https://img.shields.io/packagist/dt/golchha21/resmushit.svg?style=flat-square)](https://packagist.org/packages/golchha21/resmushit) +[![Total Downloads](https://poser.pugx.org/golchha21/resmushit/downloads)](https://packagist.org/packages/golchha21/resmushit) +[![License](https://poser.pugx.org/golchha21/resmushit/license)](https://packagist.org/packages/golchha21/resmushit) A Laravel package for the popular image optimization web service [reSmush.it](http://resmush.it/) @@ -21,12 +22,14 @@ php artisan vendor:publish --provider Golchha21\ReSmushIt\ReSmushItServiceProvid ``` ## Example configuration file + ```php // config/ReSmushIt.php - true|false, + 'quality' => 92, 'mime' => [ @@ -75,4 +78,4 @@ See also the list of [contributors](https://github.com/golchha21/reSmushit/graph ## License -The MIT License (MIT). Please see [License File](LICENSE.md) for more information. \ No newline at end of file +reSmushit for Laravel is open-sourced software licensed under the [MIT license](LICENSE.md). diff --git a/composer.json b/composer.json index ba3bd0a..8f7c16e 100644 --- a/composer.json +++ b/composer.json @@ -1,55 +1,58 @@ { - "name": "golchha21/resmushit", - "version": "1.0.4", - "description": "A Laravel package for the popular image optimization web service reSmush.it", - "keywords": [ - "golchha21", - "image-optimization", - "image-compression", - "resmushit", - "laravel", - "package", - "php" - ], - "homepage": "https://github.com/golchha21/resmushit", - "license": "MIT", - "type": "library", - "authors": [ - { - "name": "Ulhas Vardhan Golchha", - "email": "vardhans@ulhas.neet", - "homepage": "http://www.ulhas.net", - "role": "Web Architect" + "name": "golchha21/resmushit", + "version": "1.1.0", + "description": "A Laravel package for the popular image optimization web service reSmush.it", + "keywords": [ + "golchha21", + "laravel", + "laravel-package", + "resmushit", + "image-optimization", + "image-optimisation", + "image-optimizer", + "image-compression", + "image-compressor", + "php" + ], + "homepage": "https://github.com/golchha21/resmushit", + "license": "MIT", + "type": "library", + "authors": [ + { + "name": "Ulhas Vardhan Golchha", + "email": "vardhans@ulhas.neet", + "homepage": "http://www.ulhas.net", + "role": "Web Architect" + } + ], + "support": { + "email": "vardhans@ulhas.net", + "issues": "https://github.com/golchha21/resmushit/issues" + }, + "minimum-stability": "dev", + "prefer-stable": true, + "require": { + "php": "^7.1", + "ext-json": "*", + "ext-fileinfo": "*", + "ext-curl": "*" + }, + "require-dev": { + "laravel/framework": "^5.8|^6.0|^7.0" + }, + "autoload": { + "psr-4": { + "Golchha21\\ReSmushIt\\": "src" + } + }, + "config": { + "sort-packages": true + }, + "extra": { + "laravel": { + "providers": [ + "Golchha21\\ReSmushIt\\ReSmushItServiceProvider" + ] + } } - ], - "support": { - "email": "vardhans@ulhas.net", - "issues": "https://github.com/golchha21/resmushit/issues" - }, - "minimum-stability": "dev", - "prefer-stable": true, - "require": { - "php": "^7.1", - "ext-json": "*", - "ext-fileinfo": "*", - "ext-curl": "*" - }, - "require-dev": { - "laravel/framework": "^5.8|^6.0|^7.0" - }, - "autoload": { - "psr-4": { - "Golchha21\\ReSmushIt\\": "src" - } - }, - "config": { - "sort-packages": true - }, - "extra": { - "laravel": { - "providers": [ - "Golchha21\\ReSmushIt\\ReSmushItServiceProvider" - ] - } - } } diff --git a/src/ReSmushIt.php b/src/ReSmushIt.php index 321acd6..1394e6a 100644 --- a/src/ReSmushIt.php +++ b/src/ReSmushIt.php @@ -8,24 +8,25 @@ class ReSmushIt { - private const VERSION = '1.0.4'; + private const VERSION = '1.1.0'; private const ENDPOINT = 'http://api.resmush.it/'; private const TIMEOUT = 10; private const MAX_FILE_SIZE = 5242880; /** @var file */ - protected $file; + private $file; /** * starts the optimization process through the validations * @param $path string - * @return boolean array + * @return boolean|array */ public function path($path) { $this->file = $path; $validation = $this->validate(); if ($validation === true) { + $this->saveOriginalPicture(); return $this->process(); } return $validation; @@ -33,7 +34,7 @@ public function path($path) /** * validates the file before processing - * @param string + * @param string * @return boolean|array */ protected function validate() @@ -62,11 +63,12 @@ protected function validate() /** * sets the list of supported mime types by the API + * @returns array */ protected function getMime() { - if (Config::has('resmushit.mime')) { - return Config::get('resmushit.mime'); + if (Config::has('ReSmushIt.mime')) { + return Config::get('ReSmushIt.mime'); } return [ 'image/png', @@ -77,6 +79,22 @@ protected function getMime() ]; } + /** + * checks whether to save the original picture and then saves it. + * @returns void + */ + protected function saveOriginalPicture() + { + if (Config::get('ReSmushIt.original')) { + $original = File::dirname($this->file) . DIRECTORY_SEPARATOR . File::name($this->file) . '_original.' . File::extension($this->file); + File::copy($this->file, $original); + } + } + + /** + * Process the optimization + * @returns bool|array + */ protected function process() { $result = json_decode($this->postCurl(), true); @@ -93,7 +111,7 @@ protected function process() /** * send the image for optimization - * @return CURLFile + * @return bool|array */ protected function postCurl() { @@ -120,22 +138,24 @@ protected function postCurl() /** * sets the quality of the optimized picture. + * @returns int */ protected function getPictureQuality() { - if (Config::has('resmushit.quality')) { - return Config::get('resmushit.quality'); + if (Config::has('ReSmushIt.quality')) { + return Config::get('ReSmushIt.quality'); } return 92; } /** * sets whether to preserve exif for picture. + * @returns bool */ protected function getExif() { - if (Config::has('resmushit.exif')) { - return Config::get('resmushit.exif'); + if (Config::has('ReSmushIt.exif')) { + return Config::get('ReSmushIt.exif'); } return false; } @@ -146,15 +166,15 @@ protected function getExif() */ protected function getUserAgent() { - if (Config::has('resmushit.useragent')) { - return Config::get('resmushit.useragent'); + if (Config::has('ReSmushIt.useragent')) { + return Config::get('ReSmushIt.useragent'); } - return "reSmushit".self::VERSION.' - '.env('app.url'); + return "reSmushit" . self::VERSION . ' - ' . env('app.url'); } /** * downloads and saves the optimized picture - * @param string + * @param string */ protected function getCurl($destination) { @@ -174,15 +194,17 @@ protected function getCurl($destination) /** * starts the optimization process through the validations * @param $paths array - * @return boolean array + * @return boolean|array */ public function paths($paths) { + $result = []; if (is_array($paths)) { foreach ($paths as $path) { $this->file = $path; $validation = $this->validate(); if ($validation === true) { + $this->saveOriginalPicture(); $result[File::basename($this->file)] = $this->process(); } else { $result[File::basename($this->file)] = $validation; @@ -195,4 +217,5 @@ public function paths($paths) 'error_long' => 'No url of images provided.', ]); } + } diff --git a/src/ReSmushItServiceProvider.php b/src/ReSmushItServiceProvider.php index fea6900..624b77a 100644 --- a/src/ReSmushItServiceProvider.php +++ b/src/ReSmushItServiceProvider.php @@ -6,7 +6,7 @@ class ReSmushItServiceProvider extends ServiceProvider { - const CONFIG_PATH = __DIR__.'/config/ReSmushIt.php'; + const CONFIG_PATH = __DIR__ . '/config/ReSmushIt.php'; /** * Bootstrap the application services. diff --git a/src/config/ReSmushIt.php b/src/config/ReSmushIt.php index f9e3a8c..a0be51d 100644 --- a/src/config/ReSmushIt.php +++ b/src/config/ReSmushIt.php @@ -7,6 +7,8 @@ |-------------------------------------------------------------------------- */ + 'original' => false, + 'quality' => 92, 'mime' => [