Skip to content

Commit

Permalink
Added a feature to save the original picture and a version bump.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulhas Vardhan Golchha committed Apr 22, 2020
1 parent d1af076 commit 027672a
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 73 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)

Expand All @@ -21,12 +22,14 @@ php artisan vendor:publish --provider Golchha21\ReSmushIt\ReSmushItServiceProvid
```

## Example configuration file

```php
// config/ReSmushIt.php
<?php

return [


'original' => true|false,

'quality' => 92,

'mime' => [
Expand Down Expand Up @@ -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.
reSmushit for Laravel is open-sourced software licensed under the [MIT license](LICENSE.md).
107 changes: 55 additions & 52 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
}
55 changes: 39 additions & 16 deletions src/ReSmushIt.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@

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;
}

/**
* validates the file before processing
* @param string
* @param string
* @return boolean|array
*/
protected function validate()
Expand Down Expand Up @@ -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',
Expand All @@ -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);
Expand All @@ -93,7 +111,7 @@ protected function process()

/**
* send the image for optimization
* @return CURLFile
* @return bool|array
*/
protected function postCurl()
{
Expand All @@ -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;
}
Expand All @@ -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)
{
Expand All @@ -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;
Expand All @@ -195,4 +217,5 @@ public function paths($paths)
'error_long' => 'No url of images provided.',
]);
}

}
2 changes: 1 addition & 1 deletion src/ReSmushItServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/config/ReSmushIt.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
|--------------------------------------------------------------------------
*/

'original' => false,

'quality' => 92,

'mime' => [
Expand Down

0 comments on commit 027672a

Please sign in to comment.