digitalzenworks / php-image-optimizer
A responsive image workflow for optimizing and resizing your images
Fund package maintenance!
jamesjohnmcguire
Requires
- php: >=7.1.0
- ext-imagick: *
- gumlet/php-image-resize: ^2.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-11-14 16:38:11 UTC
README
A responsive image workflow for optimizing and resizing your images. Details can be found in the excellent article at Efficient Image Resizing With ImageMagick
Originally based off (forked) https://github.com/nwtn/php-respimg
Requirements/dependencies
-
Optional: For optimization, depending on what settings you pass:
Installation
Git
git clone https://github.com/jamesjohnmcguire/PhpImageOptimizer
Composer
composer require https://packagist.org/packages/digitalzenworks/php-image-optimizer
Examples
To resize one raster image, without optimization:
$image = new DigitalZenWorks\ImageOptimizer($input_filename); $image->smartResize($output_width, $output_height, false); $image->writeImage($output_filename);
To resize one raster image and maintain aspect ratio, without optimization:
$image = new DigitalZenWorks\ImageOptimizer($input_filename); $image->smartResize($output_width, 0, false); $image->writeImage($output_filename);
To resize one raster image and maintain aspect ratio, with optimization:
$image = new DigitalZenWorks\ImageOptimizer($input_filename); $image->smartResize($output_width, 0, true); $image->writeImage($output_filename); nwtn\Respimg::optimize($output_filename, 0, 1, 1, 1);
To resize a directory of raster images and maintain aspect ratio, with optimization:
$exts = array('jpeg', 'jpg', 'png'); if ($dir = opendir($input_path)) { while (($file = readdir($dir)) !== false) { $base = pathinfo($file, PATHINFO_BASENAME); $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); if (in_array($ext, $exts)) { $image = new DigitalZenWorks\ImageOptimizer($input_path . '/' . $file); $image->smartResize($width, 0, true); $image->writeImage($output_path . '/' . $base . '-w' . $w . '.' . $ext); } } } DigitalZenWorks\ImageOptimizer::optimize($output_path, 0, 1, 1, 1);
To resize a directory of raster images and SVGs and maintain aspect ratio, with optimization:
$exts = array('jpeg', 'jpg', 'png'); if ($dir = opendir($input_path)) { while (($file = readdir($dir)) !== false) { $base = pathinfo($file, PATHINFO_BASENAME); $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); if (in_array($ext, $exts)) { $image = new DigitalZenWorks\ImageOptimizer($input_path . '/' . $file); $image->smartResize($width, 0, true); $image->writeImage($output_path . '/' . $base . '-w' . $w . '.' . $ext); } elseif ($ext === 'svg') { copy($input_path . '/' . $file, $output_path . '/' . $file); DigitalZenWorks\ImageOptimizer::rasterize($input_path . '/' . $file, $output_path . '/', $width, 0); } } } DigitalZenWorks\ImageOptimizer::optimize($output_path, 3, 1, 1, 1);
Contributing
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE
for more information.
Contact
James John McGuire - @jamesmc - jamesjohnmcguire@gmail.com
Project Link: https://github.com/jamesjohnmcguire/PhpImageOptimizer