
- Views: 352
- Category: Laravel
- Published at: 17 Aug, 2023
- Updated at: 04 Sep, 2023
Integrating the pngquant Command-line Utility for Lossy PNG Compression in Laravel 10
In the realm of PHP image manipulation, the Intervention library is a staple. Built atop the robust GD Library and Imagick, it’s known for impressive performance. But it's not without its shortcomings, particularly when dealing with PNG images.
Intervention’s Limitation with PNGs
Though Intervention excels at converting images, especially from .png and .jpg to the webp format (with reductions as substantial as from 1381.50 KB to a mere 46.75 KB), it falls short with lossy PNG compression. A reduction from 1.3 MB to just about 1.1 or 1.2 MB might not seem much. Given the emphasis on website speed optimization and SEO, even 1 MB images can be detrimental.
The pngquant Solution
Enter pngquant, a command-line utility library tailor-made for lossy image compression, especially for the .png format. Before integrating it, ensure that pngquant is enabled in your environment. For those with VPS, integration is straightforward. To verify the installation, execute:
pngquant --version
Your terminal should display an output similar to 2.12.5 (July 2019). This step requires terminal or SSH access. Users on shared hosting may not have direct access, so contact your hosting provider for assistance.
Executing pngquant Directly
Upon confirming the pngquant library’s presence, use the following command for direct image compression:
pngquant --force --output=shekztech_output.png shekztech_input.png
To ascertain PHP’s ability to execute external programs, utilize:
if(function_exists('exec')) {
echo "exec is enabled";
} else {
echo "exec is disabled";
}
Installation Steps for Those Without pngquant
For those operating on AlmaLinux v8.8.0 STANDARD KVM OS:
- Enable the EPEL Repository:
As pngquant resides in the EPEL repository, start by enabling it:
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
- Install pngquant:
With EPEL set up, proceed to install pngquant:
sudo dnf install pngquant
- Verify the Installation:
Validate your pngquant installation by checking its version:
pngquant --version
Ensure exec is enabled in your PHP environment. You can identify disabled functions with:
php -I | grep disable_functions
Those with root or WHM access can modify the disable_functions via the MultiPHP Manager, navigating to "Edit PHP-FPM", identifying the desired section, removing it from "Disable Functions", and updating.
Integrating pngquant in Laravel 10
Let's harness pngquant in Laravel 10 for image conversion:
use Illuminate\Http\Request;
public function shekztech_convertImage(Request $request) {
$shekztech_imagePath = $request->file('image')->store('images');
$shekztech_command = "pngquant --force --output=storage/{$shekztech_imagePath} storage/{$shekztech_imagePath}";
exec($shekztech_command);
return back()->with('success', 'Image converted successfully.');
}
Ensure to manage necessary validations and error checks based on your project's requirements.
Conclusion
For those seeking efficient lossy PNG compression in Laravel 10, pngquant bridges the gap left by the Intervention library. By integrating this potent command-line utility, webmasters can significantly improve their website's speed and SEO performance.
0 Comment(s)