-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathAbstractEncoder.php
More file actions
78 lines (67 loc) Β· 2.12 KB
/
AbstractEncoder.php
File metadata and controls
78 lines (67 loc) Β· 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
declare(strict_types=1);
namespace Intervention\Image\Drivers;
use Intervention\Image\EncodedImage;
use Intervention\Image\Exceptions\LogicException;
use Intervention\Image\Exceptions\StreamException;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Traits\CanBuildStream;
abstract class AbstractEncoder implements EncoderInterface
{
use CanBuildStream;
/**
* Default encoding quality.
*/
public const int DEFAULT_QUALITY = 75;
/**
* {@inheritdoc}
*
* @see EncoderInterface::encode()
*
* @throws LogicException
*/
public function encode(ImageInterface $image): EncodedImageInterface
{
if ($this instanceof SpecializedInterface) {
throw new LogicException(
"Specialized class '" . static::class . "' must override encode()"
);
}
return $image->encode($this);
}
/**
* Build new stream, run callback with it and return result as encoded image.
*
* @throws InvalidArgumentException
* @throws StreamException
*/
protected function createEncodedImage(callable $callback, ?string $mediaType = null): EncodedImage
{
$stream = self::buildStreamOrFail();
$callback($stream);
return is_string($mediaType) ? new EncodedImage($stream, $mediaType) : new EncodedImage($stream);
}
/**
* {@inheritdoc}
*
* @see EncoderInterface::setOptions()
*
* @throws InvalidArgumentException
*/
public function setOptions(mixed ...$options): self
{
foreach ($options as $key => $value) {
if (!property_exists($this, (string) $key)) {
throw new InvalidArgumentException(
'Option $' . $key . ' does not exist on ' . $this::class,
);
}
$this->{$key} = $value;
}
return $this;
}
}