Search code examples
phpimage-optimization

Why image optimizer resulting identical version with no enhancement?


I installed composer require spatie/image-optimizer, but when I run the example below the I had two problems: 1- The class could not be found by calling it by using (use). So I solved it by using include.

2- After solving the first problem, the code works fine but the resultant image is the same image with no optimization.

include 'Spatie/Imageoptimizer/src/OptimizerChainFactory.php';


require __DIR__.'/autoload.php';

$pathToImage =  "D:/xampp/htdocs/images/vendor/uploads/2.png";

//use Spatie\ImageOptimizer\OptimizerChainFactory;



// Get the image and store the original size
$image = $pathToImage;
$originalSize = filesize($image);

// Optimize updates the existing image
$optimizerChain = OptimizerChainFactory::create();
$optimizerChain->optimize($image);

// Clear stat cache to get the optimized size
clearstatcache();

// Check the optimized size
$optimizedSize = filesize($image);
$percentChange = (1 - $optimizedSize / $originalSize) * 100;
echo sprintf("The image is now %.2f%% smaller\n", $percentChange);
exit(0);

Could you please suggest me any solutions!


Solution

  • I found the cause of the problem, which is the jpg and Optipng tools are not installed in the windows. Is there any way to install the tools in windows and link them to the plugin.