Subiendo proyecto completo sin restricciones de git ignore

This commit is contained in:
Jose Sanchez
2023-08-17 11:44:02 -04:00
parent a0d4f5ba3b
commit 20f1c60600
19921 changed files with 2509159 additions and 45 deletions

View File

@@ -0,0 +1,44 @@
<?php
namespace Spatie\ImageOptimizer;
use Psr\Log\LoggerInterface;
class DummyLogger implements LoggerInterface
{
public function emergency($message, array $context = [])
{
}
public function alert($message, array $context = [])
{
}
public function critical($message, array $context = [])
{
}
public function error($message, array $context = [])
{
}
public function warning($message, array $context = [])
{
}
public function notice($message, array $context = [])
{
}
public function info($message, array $context = [])
{
}
public function debug($message, array $context = [])
{
}
public function log($level, $message, array $context = [])
{
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Spatie\ImageOptimizer;
use InvalidArgumentException;
class Image
{
protected $pathToImage = '';
public function __construct(string $pathToImage)
{
if (! file_exists($pathToImage)) {
throw new InvalidArgumentException("`{$pathToImage}` does not exist");
}
$this->pathToImage = $pathToImage;
}
public function mime(): string
{
return mime_content_type($this->pathToImage);
}
public function path(): string
{
return $this->pathToImage;
}
public function extension(): string
{
$extension = pathinfo($this->pathToImage, PATHINFO_EXTENSION);
return strtolower($extension);
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Spatie\ImageOptimizer;
interface Optimizer
{
/**
* Returns the name of the binary to be executed.
*
* @return string
*/
public function binaryName(): string;
/**
* Determines if the given image can be handled by the optimizer.
*
* @param \Spatie\ImageOptimizer\Image $image
*
* @return bool
*/
public function canHandle(Image $image): bool;
/**
* Set the path to the image that should be optimized.
*
* @param string $imagePath
*
* @return $this
*/
public function setImagePath(string $imagePath);
/**
* Set the options the optimizer should use.
*
* @param array $options
*
* @return $this
*/
public function setOptions(array $options = []);
/**
* Get the command that should be executed.
*
* @return string
*/
public function getCommand(): string;
}

View File

@@ -0,0 +1,116 @@
<?php
namespace Spatie\ImageOptimizer;
use Psr\Log\LoggerInterface;
use Symfony\Component\Process\Process;
class OptimizerChain
{
/* @var \Spatie\ImageOptimizer\Optimizer[] */
protected $optimizers = [];
/** @var \Psr\Log\LoggerInterface */
protected $logger;
/** @var int */
protected $timeout = 60;
public function __construct()
{
$this->useLogger(new DummyLogger());
}
public function getOptimizers(): array
{
return $this->optimizers;
}
public function addOptimizer(Optimizer $optimizer)
{
$this->optimizers[] = $optimizer;
return $this;
}
public function setOptimizers(array $optimizers)
{
$this->optimizers = [];
foreach ($optimizers as $optimizer) {
$this->addOptimizer($optimizer);
}
return $this;
}
/*
* Set the amount of seconds each separate optimizer may use.
*/
public function setTimeout(int $timeoutInSeconds)
{
$this->timeout = $timeoutInSeconds;
return $this;
}
public function useLogger(LoggerInterface $log)
{
$this->logger = $log;
return $this;
}
public function optimize(string $pathToImage, string $pathToOutput = null)
{
if ($pathToOutput) {
copy($pathToImage, $pathToOutput);
$pathToImage = $pathToOutput;
}
$image = new Image($pathToImage);
$this->logger->info("Start optimizing {$pathToImage}");
foreach ($this->optimizers as $optimizer) {
$this->applyOptimizer($optimizer, $image);
}
}
protected function applyOptimizer(Optimizer $optimizer, Image $image)
{
if (! $optimizer->canHandle($image)) {
return;
}
$optimizerClass = get_class($optimizer);
$this->logger->info("Using optimizer: `{$optimizerClass}`");
$optimizer->setImagePath($image->path());
$command = $optimizer->getCommand();
$this->logger->info("Executing `{$command}`");
$process = Process::fromShellCommandline($command);
$process
->setTimeout($this->timeout)
->run();
$this->logResult($process);
}
protected function logResult(Process $process)
{
if (! $process->isSuccessful()) {
$this->logger->error("Process errored with `{$process->getErrorOutput()}`");
return;
}
$this->logger->info("Process successfully ended with output `{$process->getOutput()}`");
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Spatie\ImageOptimizer;
use Spatie\ImageOptimizer\Optimizers\Cwebp;
use Spatie\ImageOptimizer\Optimizers\Gifsicle;
use Spatie\ImageOptimizer\Optimizers\Jpegoptim;
use Spatie\ImageOptimizer\Optimizers\Optipng;
use Spatie\ImageOptimizer\Optimizers\Pngquant;
use Spatie\ImageOptimizer\Optimizers\Svgo;
class OptimizerChainFactory
{
public static function create(array $config = []): OptimizerChain
{
$jpegQuality = '--max=85';
$pngQuality = '--quality=85';
if (isset($config['quality'])) {
$jpegQuality = '--max='.$config['quality'];
$pngQuality = '--quality='.$config['quality'];
}
return (new OptimizerChain())
->addOptimizer(new Jpegoptim([
$jpegQuality,
'--strip-all',
'--all-progressive',
]))
->addOptimizer(new Pngquant([
$pngQuality,
'--force',
]))
->addOptimizer(new Optipng([
'-i0',
'-o2',
'-quiet',
]))
->addOptimizer(new Svgo([
'--disable={cleanupIDs,removeViewBox}',
]))
->addOptimizer(new Gifsicle([
'-b',
'-O3',
]))
->addOptimizer(new Cwebp([
'-m 6',
'-pass 10',
'-mt',
'-q 80',
]));
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Spatie\ImageOptimizer\Optimizers;
use Spatie\ImageOptimizer\Optimizer;
abstract class BaseOptimizer implements Optimizer
{
public $options = [];
public $imagePath = '';
public $binaryPath = '';
public function __construct($options = [])
{
$this->setOptions($options);
}
public function binaryName(): string
{
return $this->binaryName;
}
public function setBinaryPath(string $binaryPath)
{
if (strlen($binaryPath) > 0 && substr($binaryPath, -1) !== DIRECTORY_SEPARATOR) {
$binaryPath = $binaryPath.DIRECTORY_SEPARATOR;
}
$this->binaryPath = $binaryPath;
return $this;
}
public function setImagePath(string $imagePath)
{
$this->imagePath = $imagePath;
return $this;
}
public function setOptions(array $options = [])
{
$this->options = $options;
return $this;
}
public function getCommand(): string
{
$optionString = implode(' ', $this->options);
return "\"{$this->binaryPath}{$this->binaryName}\" {$optionString} ".escapeshellarg($this->imagePath);
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Spatie\ImageOptimizer\Optimizers;
use Spatie\ImageOptimizer\Image;
class Cwebp extends BaseOptimizer
{
public $binaryName = 'cwebp';
public function canHandle(Image $image): bool
{
return $image->mime() === 'image/webp';
}
public function getCommand(): string
{
$optionString = implode(' ', $this->options);
return "\"{$this->binaryPath}{$this->binaryName}\" {$optionString}"
.' '.escapeshellarg($this->imagePath)
.' -o '.escapeshellarg($this->imagePath);
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Spatie\ImageOptimizer\Optimizers;
use Spatie\ImageOptimizer\Image;
class Gifsicle extends BaseOptimizer
{
public $binaryName = 'gifsicle';
public function canHandle(Image $image): bool
{
return $image->mime() === 'image/gif';
}
public function getCommand(): string
{
$optionString = implode(' ', $this->options);
return "\"{$this->binaryPath}{$this->binaryName}\" {$optionString}"
.' -i '.escapeshellarg($this->imagePath)
.' -o '.escapeshellarg($this->imagePath);
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Spatie\ImageOptimizer\Optimizers;
use Spatie\ImageOptimizer\Image;
class Jpegoptim extends BaseOptimizer
{
public $binaryName = 'jpegoptim';
public function canHandle(Image $image): bool
{
return $image->mime() === 'image/jpeg';
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Spatie\ImageOptimizer\Optimizers;
use Spatie\ImageOptimizer\Image;
class Optipng extends BaseOptimizer
{
public $binaryName = 'optipng';
public function canHandle(Image $image): bool
{
return $image->mime() === 'image/png';
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Spatie\ImageOptimizer\Optimizers;
use Spatie\ImageOptimizer\Image;
class Pngquant extends BaseOptimizer
{
public $binaryName = 'pngquant';
public function canHandle(Image $image): bool
{
return $image->mime() === 'image/png';
}
public function getCommand(): string
{
$optionString = implode(' ', $this->options);
return "\"{$this->binaryPath}{$this->binaryName}\" {$optionString}"
.' '.escapeshellarg($this->imagePath)
.' --output='.escapeshellarg($this->imagePath);
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Spatie\ImageOptimizer\Optimizers;
use Spatie\ImageOptimizer\Image;
class Svgo extends BaseOptimizer
{
public $binaryName = 'svgo';
public function canHandle(Image $image): bool
{
if ($image->extension() !== 'svg') {
return false;
}
return in_array($image->mime(), [
'text/html',
'image/svg',
'image/svg+xml',
'text/plain',
]);
}
public function getCommand(): string
{
$optionString = implode(' ', $this->options);
return "\"{$this->binaryPath}{$this->binaryName}\" {$optionString}"
.' --input='.escapeshellarg($this->imagePath)
.' --output='.escapeshellarg($this->imagePath);
}
}