| 1 |
<?php |
| 2 |
|
| 3 |
namespace Spatie\Image; |
| 4 |
|
| 5 |
use BadMethodCallException; |
| 6 |
use Intervention\Image\ImageManagerStatic as InterventionImage; |
| 7 |
use Spatie\Image\Exceptions\InvalidImageDriver; |
| 8 |
use Spatie\ImageOptimizer\OptimizerChain; |
| 9 |
use Spatie\ImageOptimizer\OptimizerChainFactory; |
| 10 |
use Spatie\ImageOptimizer\Optimizers\BaseOptimizer; |
| 11 |
|
| 12 |
/** @mixin \Spatie\Image\Manipulations */ |
| 13 |
class Image |
| 14 |
{ |
| 15 |
protected Manipulations $manipulations; |
| 16 |
|
| 17 |
protected string $imageDriver = 'gd'; |
| 18 |
|
| 19 |
protected ?string $temporaryDirectory = null; |
| 20 |
|
| 21 |
protected ?OptimizerChain $optimizerChain = null; |
| 22 |
|
| 23 |
public function __construct(protected string $pathToImage) |
| 24 |
{ |
| 25 |
$this->manipulations = new Manipulations(); |
| 26 |
} |
| 27 |
|
| 28 |
public static function load(string $pathToImage): static |
| 29 |
{ |
| 30 |
return new static($pathToImage); |
| 31 |
} |
| 32 |
|
| 33 |
public function setTemporaryDirectory($tempDir): static |
| 34 |
{ |
| 35 |
$this->temporaryDirectory = $tempDir; |
| 36 |
|
| 37 |
return $this; |
| 38 |
} |
| 39 |
|
| 40 |
public function setOptimizeChain(OptimizerChain $optimizerChain): static |
| 41 |
{ |
| 42 |
$this->optimizerChain = $optimizerChain; |
| 43 |
|
| 44 |
return $this; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param string $imageDriver |
| 49 |
* @return $this |
| 50 |
* @throws InvalidImageDriver |
| 51 |
*/ |
| 52 |
public function useImageDriver(string $imageDriver): static |
| 53 |
{ |
| 54 |
if (! in_array($imageDriver, ['gd', 'imagick'])) { |
| 55 |
throw InvalidImageDriver::driver($imageDriver); |
| 56 |
} |
| 57 |
|
| 58 |
$this->imageDriver = $imageDriver; |
| 59 |
|
| 60 |
InterventionImage::configure([ |
| 61 |
'driver' => $this->imageDriver, |
| 62 |
]); |
| 63 |
|
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
public function manipulate(callable |Manipulations $manipulations): static |
| 68 |
{ |
| 69 |
if (is_callable($manipulations)) { |
| 70 |
$manipulations($this->manipulations); |
| 71 |
} |
| 72 |
|
| 73 |
if ($manipulations instanceof Manipulations) { |
| 74 |
$this->manipulations->mergeManipulations($manipulations); |
| 75 |
} |
| 76 |
|
| 77 |
return $this; |
| 78 |
} |
| 79 |
|
| 80 |
public function __call($name, $arguments): static |
| 81 |
{ |
| 82 |
if (! method_exists($this->manipulations, $name)) { |
| 83 |
throw new BadMethodCallException("Manipulation `{$name}` does not exist"); |
| 84 |
} |
| 85 |
|
| 86 |
$this->manipulations->$name(...$arguments); |
| 87 |
|
| 88 |
return $this; |
| 89 |
} |
| 90 |
|
| 91 |
public function getWidth(): int |
| 92 |
{ |
| 93 |
return InterventionImage::make($this->pathToImage)->width(); |
| 94 |
} |
| 95 |
|
| 96 |
public function getHeight(): int |
| 97 |
{ |
| 98 |
return InterventionImage::make($this->pathToImage)->height(); |
| 99 |
} |
| 100 |
|
| 101 |
public function getManipulationSequence(): ManipulationSequence |
| 102 |
{ |
| 103 |
return $this->manipulations->getManipulationSequence(); |
| 104 |
} |
| 105 |
|
| 106 |
public function save(string $outputPath = ''): void |
| 107 |
{ |
| 108 |
if ($outputPath === '') { |
| 109 |
$outputPath = $this->pathToImage; |
| 110 |
} |
| 111 |
|
| 112 |
$this->addFormatManipulation($outputPath); |
| 113 |
|
| 114 |
$glideConversion = GlideConversion::create($this->pathToImage)->useImageDriver($this->imageDriver); |
| 115 |
|
| 116 |
if (! is_null($this->temporaryDirectory)) { |
| 117 |
$glideConversion->setTemporaryDirectory($this->temporaryDirectory); |
| 118 |
} |
| 119 |
|
| 120 |
$glideConversion->performManipulations($this->manipulations); |
| 121 |
|
| 122 |
$glideConversion->save($outputPath); |
| 123 |
|
| 124 |
if ($this->shouldOptimize()) { |
| 125 |
$optimizerChainConfiguration = $this->manipulations->getFirstManipulationArgument('optimize'); |
| 126 |
|
| 127 |
$optimizerChainConfiguration = json_decode($optimizerChainConfiguration, true); |
| 128 |
|
| 129 |
$this->performOptimization($outputPath, $optimizerChainConfiguration); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
protected function shouldOptimize(): bool |
| 134 |
{ |
| 135 |
return ! is_null($this->manipulations->getFirstManipulationArgument('optimize')); |
| 136 |
} |
| 137 |
|
| 138 |
protected function performOptimization($path, array $optimizerChainConfiguration): void |
| 139 |
{ |
| 140 |
$optimizerChain = $this->optimizerChain ?? OptimizerChainFactory::create(); |
| 141 |
|
| 142 |
if (count($optimizerChainConfiguration)) { |
| 143 |
$existingOptimizers = $optimizerChain->getOptimizers(); |
| 144 |
|
| 145 |
$optimizers = array_map(function (array $optimizerOptions, string $optimizerClassName) use ($existingOptimizers) { |
| 146 |
$optimizer = array_values(array_filter($existingOptimizers, function ($optimizer) use ($optimizerClassName) { |
| 147 |
return $optimizer::class === $optimizerClassName; |
| 148 |
})); |
| 149 |
|
| 150 |
$optimizer = isset($optimizer[0]) && $optimizer[0] instanceof BaseOptimizer ? $optimizer[0] : new $optimizerClassName(); |
| 151 |
|
| 152 |
return $optimizer->setOptions($optimizerOptions)->setBinaryPath($optimizer->binaryPath); |
| 153 |
}, $optimizerChainConfiguration, array_keys($optimizerChainConfiguration)); |
| 154 |
|
| 155 |
$optimizerChain->setOptimizers($optimizers); |
| 156 |
} |
| 157 |
|
| 158 |
$optimizerChain->optimize($path); |
| 159 |
} |
| 160 |
|
| 161 |
protected function addFormatManipulation($outputPath): void |
| 162 |
{ |
| 163 |
if ($this->manipulations->hasManipulation('format')) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
$inputExtension = strtolower(pathinfo($this->pathToImage, PATHINFO_EXTENSION)); |
| 168 |
$outputExtension = strtolower(pathinfo($outputPath, PATHINFO_EXTENSION)); |
| 169 |
|
| 170 |
if ($inputExtension === $outputExtension) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
$supportedFormats = [ |
| 175 |
Manipulations::FORMAT_JPG, |
| 176 |
Manipulations::FORMAT_PJPG, |
| 177 |
Manipulations::FORMAT_PNG, |
| 178 |
Manipulations::FORMAT_GIF, |
| 179 |
Manipulations::FORMAT_WEBP, |
| 180 |
Manipulations::FORMAT_AVIF, |
| 181 |
]; |
| 182 |
//gd driver doesn't support TIFF |
| 183 |
if ($this->imageDriver === 'imagick') { |
| 184 |
$supportedFormats[] = Manipulations::FORMAT_TIFF; |
| 185 |
} |
| 186 |
|
| 187 |
if (in_array($outputExtension, $supportedFormats)) { |
| 188 |
$this->manipulations->format($outputExtension); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|