Skip to content

Commit

Permalink
v11
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Nov 28, 2023
1 parent 9d92e72 commit 679c75f
Show file tree
Hide file tree
Showing 7 changed files with 786 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"illuminate/pipeline": "^9.18|^10.0",
"illuminate/support": "^9.18|^10.0",
"maennchen/zipstream-php": "^2.0|^3.0",
"spatie/image": "^2.2.7",
"spatie/image": "v3.x-dev",
"spatie/temporary-directory": "^2.0",
"symfony/console": "^6.0"
},
Expand Down
16 changes: 13 additions & 3 deletions src/Conversions/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use BadMethodCallException;
use Illuminate\Support\Traits\Conditionable;
use Spatie\Image\Manipulations;
use Spatie\MediaLibrary\Conversions\Manipulations;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\FileNamer\FileNamer;

/** @mixin \Spatie\Image\Manipulations */
/** @mixin \Spatie\Image\Drivers\ImageDriver */
class Conversion
{
use Conditionable;
Expand All @@ -34,9 +34,17 @@ class Conversion
public function __construct(
protected string $name
) {
// TODO: convert to jpg by default, implement optimizers

$this->manipulations = new Manipulations();

/*
$this->manipulations = (new Manipulations())
->optimize(config('media-library.image_optimizers'))
->format(Manipulations::FORMAT_JPG);
->format(Manipulations::FORMAT_JPG)
;
*/


$this->fileNamer = app(config('media-library.file_namer'));

Expand Down Expand Up @@ -109,9 +117,11 @@ public function withoutManipulations(): self

public function __call($name, $arguments)
{
/*
if (! method_exists($this->manipulations, $name)) {
throw new BadMethodCallException("Manipulation `{$name}` does not exist");
}
*/

$this->manipulations->$name(...$arguments);

Expand Down
133 changes: 133 additions & 0 deletions src/Conversions/FromImagePackage/OldManipulationSequence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace Spatie\Image;

use ArrayIterator;
use IteratorAggregate;

class OldManipulationSequence implements IteratorAggregate
{
protected array $groups = [];

public function __construct(array $sequenceArray = [])
{
$this->startNewGroup();
$this->mergeArray($sequenceArray);
}

public function addManipulation(string $operation, string $argument): static
{
$lastIndex = count($this->groups) - 1;

$this->groups[$lastIndex][$operation] = $argument;

return $this;
}

public function merge(self $sequence): static
{
$sequenceArray = $sequence->toArray();

$this->mergeArray($sequenceArray);

return $this;
}

public function mergeArray(array $sequenceArray): void
{
foreach ($sequenceArray as $group) {
foreach ($group as $name => $argument) {
$this->addManipulation($name, $argument);
}

if (next($sequenceArray)) {
$this->startNewGroup();
}
}
}

public function startNewGroup(): static
{
$this->groups[] = [];

return $this;
}

public function toArray(): array
{
return $this->getGroups();
}

public function getGroups(): array
{
return $this->sanitizeManipulationSets($this->groups);
}

public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->toArray());
}

public function removeManipulation(string $manipulationName): static
{
foreach ($this->groups as &$group) {
if (array_key_exists($manipulationName, $group)) {
unset($group[$manipulationName]);
}
}

return $this;
}

public function isEmpty(): bool
{
if (count($this->groups) > 1) {
return false;
}

if (count($this->groups[0]) > 0) {
return false;
}

return true;
}

protected function sanitizeManipulationSets(array $groups): array
{
return array_values(array_filter($groups, function (array $manipulationSet) {
return count($manipulationSet);
}));
}

/*
* Determine if the sequences contain a manipulation with the given name.
*/
public function getFirstManipulationArgument($searchManipulationName)
{
foreach ($this->groups as $group) {
foreach ($group as $name => $argument) {
if ($name === $searchManipulationName) {
return $argument;
}
}
}
}

/*
* Determine if the sequences contain a manipulation with the given name.
*/
public function contains($searchManipulationName): bool
{
foreach ($this->groups as $group) {
foreach ($group as $name => $argument) {
if ($name === $searchManipulationName) {
return true;
}
}

return false;
}

return false;
}
}
Loading

0 comments on commit 679c75f

Please sign in to comment.