0

I'm trying to use this php script for image-optimization: https://github.com/psliwa/image-optimizer

But I think that the documentation is very poor, this is all what it says:

$factory = new \ImageOptimizer\OptimizerFactory();
$optimizer = $factory->get();

$filepath = /* path to image */;

$optimizer->optimize($filepath);

But I really don't understand this way of calling a class with "new \" and get this error when I copy that code: Fatal error: Class 'ImageOptimizer\OptimizerFactory' not found in... What is my mistake?

leonardo_palma
  • 303
  • 2
  • 4
  • 12
  • Possible duplicate of [Backslash in PHP -- what does it mean?](http://stackoverflow.com/questions/10788400/backslash-in-php-what-does-it-mean) – Djizeus Oct 03 '15 at 20:17
  • Possible duplicate of [Backslash syntax when creating objects](http://stackoverflow.com/questions/4075521/backslash-syntax-when-creating-objects) – Prune Oct 03 '15 at 20:56
  • I read those post but I still don't know what I'm doing wrong, and I cannot call correctly the image optimizer class. That is my problem, and that was my question. Sorry I'm not an expert. – leonardo_palma Oct 03 '15 at 22:11

1 Answers1

1

If you are at a loss and the composer library install isn't working for you, you can just call every one of the libraries from your install folder. It's a dirty way of doing it with composer being so convenient, but will work.

Add this to the top of your image processing file:

require "your_path/ImageOptimizer/Optimizer.php";
require "your_path/ImageOptimizer/OptimizerFactory.php";
require "your_path/ImageOptimizer/CommandOptimizer.php";
require "your_path/ImageOptimizer/Command.php";
require "your_path/ImageOptimizer/SuppressErrorOptimizer.php";
require "your_path/ImageOptimizer/ChainOptimizer.php";
require "your_path/ImageOptimizer/SmartOptimizer.php";
require "your_path/ImageOptimizer/TypeGuesser/TypeGuesser.php";
require "your_path/ImageOptimizer/TypeGuesser/SmartTypeGuesser.php";
require "your_path/ImageOptimizer/TypeGuesser/GdTypeGuesser.php";
require "your_path/ImageOptimizer/TypeGuesser/ExtensionTypeGuesser.php";
require "your_path/ImageOptimizer/Exception/Exception.php";
require "your_path/ImageOptimizer/Exception/CommandNotFound.php";

... and then call the methods as described:

$factory = new \ImageOptimizer\OptimizerFactory();
$optimizer = $factory->get();

$filepath = /* path to image */;

$optimizer->optimize($filepath);
//optimized file overwrites original one

SOURCE: https://github.com/psliwa/image-optimizer

Stephen
  • 395
  • 1
  • 15