This tutorial explains in detail, the ways to optimize images in PHP using in-built and 3rd party options.
Image Libraries
This section list the various image processing libraries available to optimize the images.
Install Image Packages on Ubuntu
Use below-mentioned commands to install 3rd-party image libraries on Ubuntu.
sudo apt-get install jpegoptim
sudo apt-get install optipng
sudo apt-get install pngquant
sudo apt-get install gifsicle
Install Image Packages on MacOS
Use below-mentioned commands to install 3rd-party image libraries on MacOS.
brew install jpegoptim
brew install optipng
brew install pngquant
brew install gifsicle