https://github.com/navt/reduction
Optimization of images size on the site
https://github.com/navt/reduction
gd image imagick reduction resize
Last synced: 5 months ago
JSON representation
Optimization of images size on the site
- Host: GitHub
- URL: https://github.com/navt/reduction
- Owner: navt
- License: mit
- Created: 2020-09-01T20:11:15.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-11-22T07:54:54.000Z (over 3 years ago)
- Last Synced: 2025-09-12T00:24:13.578Z (9 months ago)
- Topics: gd, image, imagick, reduction, resize
- Language: PHP
- Homepage:
- Size: 144 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Reduction
Utility Reduction is designed to optimize the size of images on a website. Often, users upload too large images to the server :)
The logic of the application is as follows: first, directories are inspected for the presence of image files (jpeg, png, gif) larger than a certain size, then the found files can be overwritten with the optimal width and height (the proportions remain correct). Files are searched recursively.
You can select images not only by the file size, but also by the size of the "long" side of the image. The settings are placed in the file `data/config.json`.
To navigate how much the application is running in time and consumes memory, the Marker class is written. The results are written to the log.
Let's look at the file `data/config.json`, in it:
`"folderPath"` - relative path to target images directory from `app.php`
`"mode"` - mode for selecting images either `"ImageSide"`, or `"FileSize"`
`"maxFileSize"` - in the `"FileSize"` mode, files larger than the specified size in bytes will be selected
`"maxImageSide"` - in the `"ImageSide"` mode, files with a long side larger than the value in pixels specified here will be selected
`"maxWidth"` - width of new images, if images are horizontal in pixels
`"maxHeight"` - height of new images, if images are vertical in pixels
`"ableTypes"` - array of file extensions, optionally there should be 3 values, select one or two for "dot" work
`"quality"` - the `quality` parameter when recording jpeg and png images using the imagejpeg(), imagepng() functions
## Usage
Consider the case where you place the `vendor` directory in the root directory of your site.
```bash
# go to the root directory of site
$ cd root-of-site
# require package navt/reduction
$ composer require navt/reduction:dev-master
# make sure that there is no app.php file and no data directory in the root directory.
# copy
$ cp vendor/navt/reduction/app.php ./
$ cp -r vendor/navt/reduction/data ./
```
Edit` data/config.json` and `app.php` in accordance with your current task.
Run
```bash
$ php -f app.php
```
Initially, the `app.php` file contains the code to inspect the target directory. If you decide that it is time to reduce the size of the selected files, then the code in `app.php` should be changed to:
```php
getList(); // creates a list of filtered files
$reduct->printList();
$marker->addMark(); // add timestamp
// reduce the size of the files in the list, destroy the Reduction instance
$reduct->reductAll(); // overwrite files
$reduct = null;
$marker->addMark();
// check what happened
$reduct = new Reduction($log, "data/config.json");
$reduct->getList();
$reduct->printList();
$marker->addMark();
$marker->display();
```
### Peculiars
Using the console is preferable because with a large enough volume of images, the application will take time to complete the task. Conduct an evaluation run with a small number of files to understand how fast the utility works.
Take care of the integrity of your data: make a dump of the directories where you plan to carry out work, run the script on the test data on the local computer, see if the result is satisfactory to you.
To work with jpeg, png and gif images without animation, the PHP module `gd` will be used.
To work with gif-images that have several layers (animation), the PHP module `imagick` must be installed on your server. If there is no such module, then animated gifs will not be affected.
If you're going to scale down gifs that have multiple layers, consider running the gif-only utility separately.