Skip to content

Commit d5db89a

Browse files
committed
Create reusable trait for decoder support detector methods
1 parent d3ed5cf commit d5db89a

File tree

10 files changed

+133
-104
lines changed

10 files changed

+133
-104
lines changed

src/Drivers/Gd/Decoders/Base64ImageDecoder.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,20 @@
88
use Intervention\Image\Exceptions\ImageDecoderException;
99
use Intervention\Image\Interfaces\DecoderInterface;
1010
use Intervention\Image\Interfaces\ImageInterface;
11-
use Stringable;
11+
use Intervention\Image\Traits\CanDetectImageSources;
1212

1313
class Base64ImageDecoder extends BinaryImageDecoder implements DecoderInterface
1414
{
15+
use CanDetectImageSources;
16+
1517
/**
1618
* {@inheritdoc}
1719
*
1820
* @see DecoderInterface::supports()
1921
*/
2022
public function supports(mixed $input): bool
2123
{
22-
if (!is_string($input) && !$input instanceof Stringable) {
23-
return false;
24-
}
25-
26-
$input = (string) $input;
27-
28-
if (str_ends_with($input, '=')) {
29-
return true;
30-
}
31-
32-
return preg_match('/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/', $input) === 1;
24+
return $this->couldBeBase64Data($input);
3325
}
3426

3527
/**

src/Drivers/Gd/Decoders/BinaryImageDecoder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
use Intervention\Image\Exceptions\StateException;
1414
use Intervention\Image\Format;
1515
use Intervention\Image\Modifiers\AlignRotationModifier;
16-
use Intervention\Image\Traits\CanDetectBinaryData;
16+
use Intervention\Image\Traits\CanDetectImageSources;
1717
use Stringable;
1818

1919
class BinaryImageDecoder extends NativeObjectDecoder implements DecoderInterface
2020
{
21-
use CanDetectBinaryData;
21+
use CanDetectImageSources;
2222

2323
/**
2424
* {@inheritdoc}
@@ -27,7 +27,7 @@ class BinaryImageDecoder extends NativeObjectDecoder implements DecoderInterface
2727
*/
2828
public function supports(mixed $input): bool
2929
{
30-
return $this->isBinary($input);
30+
return $this->couldBeBinaryData($input);
3131
}
3232

3333
/**

src/Drivers/Gd/Decoders/DataUriImageDecoder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313
use Intervention\Image\Exceptions\StateException;
1414
use Intervention\Image\Interfaces\DecoderInterface;
1515
use Intervention\Image\Interfaces\ImageInterface;
16+
use Intervention\Image\Traits\CanDetectImageSources;
1617

1718
class DataUriImageDecoder extends BinaryImageDecoder implements DecoderInterface
1819
{
20+
use CanDetectImageSources;
21+
1922
/**
2023
* {@inheritdoc}
2124
*
2225
* @see DecoderInterface::supports()
2326
*/
2427
public function supports(mixed $input): bool
2528
{
26-
return is_string($input) && str_starts_with($input, 'data:');
29+
return $this->couldBeDataUrl($input);
2730
}
2831

2932
/**

src/Drivers/Gd/Decoders/FilePathImageDecoder.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,21 @@
1717
use Intervention\Image\Interfaces\ImageInterface;
1818
use Intervention\Image\MediaType;
1919
use Intervention\Image\Modifiers\AlignRotationModifier;
20-
use Stringable;
20+
use Intervention\Image\Traits\CanDetectImageSources;
2121
use Throwable;
2222

2323
class FilePathImageDecoder extends NativeObjectDecoder implements DecoderInterface
2424
{
25+
use CanDetectImageSources;
26+
2527
/**
2628
* {@inheritdoc}
2729
*
2830
* @see DecoderInterface::supports()
2931
*/
3032
public function supports(mixed $input): bool
3133
{
32-
if (!is_string($input) && !$input instanceof Stringable) {
33-
return false;
34-
}
35-
36-
$input = (string) $input;
37-
38-
if (strlen($input) > PHP_MAXPATHLEN) {
39-
return false;
40-
}
41-
42-
if (str_starts_with($input, DIRECTORY_SEPARATOR)) {
43-
return true;
44-
}
45-
46-
if (preg_match('/[^ -~]/', $input) === 1) {
47-
return false;
48-
}
49-
50-
return true;
34+
return $this->couldBeFilePath($input);
5135
}
5236

5337
/**

src/Drivers/Imagick/Decoders/Base64ImageDecoder.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,20 @@
77
use Intervention\Image\Exceptions\DecoderException;
88
use Intervention\Image\Exceptions\ImageDecoderException;
99
use Intervention\Image\Interfaces\ImageInterface;
10-
use Stringable;
10+
use Intervention\Image\Traits\CanDetectImageSources;
1111

1212
class Base64ImageDecoder extends BinaryImageDecoder
1313
{
14+
use CanDetectImageSources;
15+
1416
/**
1517
* {@inheritdoc}
1618
*
1719
* @see DecoderInterface::supports()
1820
*/
1921
public function supports(mixed $input): bool
2022
{
21-
if (!is_string($input) && !$input instanceof Stringable) {
22-
return false;
23-
}
24-
25-
$input = (string) $input;
26-
27-
if (str_ends_with($input, '=')) {
28-
return true;
29-
}
30-
31-
return preg_match('/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/', $input) === 1;
23+
return $this->couldBeBase64Data($input);
3224
}
3325

3426
/**

src/Drivers/Imagick/Decoders/BinaryImageDecoder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
use Intervention\Image\Exceptions\StateException;
1313
use Intervention\Image\Format;
1414
use Intervention\Image\Interfaces\ImageInterface;
15-
use Intervention\Image\Traits\CanDetectBinaryData;
15+
use Intervention\Image\Traits\CanDetectImageSources;
1616
use Stringable;
1717

1818
class BinaryImageDecoder extends NativeObjectDecoder
1919
{
20-
use CanDetectBinaryData;
20+
use CanDetectImageSources;
2121

2222
/**
2323
* {@inheritdoc}
@@ -26,7 +26,7 @@ class BinaryImageDecoder extends NativeObjectDecoder
2626
*/
2727
public function supports(mixed $input): bool
2828
{
29-
return $this->isBinary($input);
29+
return $this->couldBeBinaryData($input);
3030
}
3131

3232
/**

src/Drivers/Imagick/Decoders/DataUriImageDecoder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@
1111
use Intervention\Image\Exceptions\InvalidArgumentException;
1212
use Intervention\Image\Exceptions\StateException;
1313
use Intervention\Image\Interfaces\ImageInterface;
14+
use Intervention\Image\Traits\CanDetectImageSources;
1415

1516
class DataUriImageDecoder extends BinaryImageDecoder
1617
{
18+
use CanDetectImageSources;
19+
1720
/**
1821
* {@inheritdoc}
1922
*
2023
* @see DecoderInterface::supports()
2124
*/
2225
public function supports(mixed $input): bool
2326
{
24-
return is_string($input) && str_starts_with($input, 'data:');
27+
return $this->couldBeDataUrl($input);
2528
}
2629

2730
/**

src/Drivers/Imagick/Decoders/FilePathImageDecoder.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,20 @@
1414
use Intervention\Image\Exceptions\InvalidArgumentException;
1515
use Intervention\Image\Exceptions\StateException;
1616
use Intervention\Image\Interfaces\ImageInterface;
17-
use Stringable;
17+
use Intervention\Image\Traits\CanDetectImageSources;
1818

1919
class FilePathImageDecoder extends NativeObjectDecoder
2020
{
21+
use CanDetectImageSources;
22+
2123
/**
2224
* {@inheritdoc}
2325
*
2426
* @see DecoderInterface::supports()
2527
*/
2628
public function supports(mixed $input): bool
2729
{
28-
// todo: centralize code for all drivers
29-
if (!is_string($input) && !$input instanceof Stringable) {
30-
return false;
31-
}
32-
33-
$input = (string) $input;
34-
35-
if (strlen($input) > PHP_MAXPATHLEN) {
36-
return false;
37-
}
38-
39-
if (str_starts_with($input, DIRECTORY_SEPARATOR)) {
40-
return true;
41-
}
42-
43-
if (preg_match('/[^ -~]/', $input) === 1) {
44-
return false;
45-
}
46-
47-
return true;
30+
return $this->couldBeFilePath($input);
4831
}
4932

5033
/**

src/Traits/CanDetectBinaryData.php

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Intervention\Image\Traits;
6+
7+
use Intervention\Image\Interfaces\DataUriInterface;
8+
use Stringable;
9+
10+
trait CanDetectImageSources
11+
{
12+
/**
13+
* Returns true if the specified content could be base64 encoded.
14+
*
15+
* This does not necessarily mean that the content actually meets this
16+
* assumption, but only serves as an initial filter.
17+
*/
18+
protected function couldBeBase64Data(mixed $input): bool
19+
{
20+
if (!is_string($input) && !$input instanceof Stringable) {
21+
return false;
22+
}
23+
24+
$input = (string) $input;
25+
26+
if (str_ends_with($input, '=')) {
27+
return true;
28+
}
29+
30+
return preg_match('/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/', $input) === 1;
31+
}
32+
33+
/**
34+
* Returns true if the specified content could be binary data.
35+
*
36+
* This does not necessarily mean that the content actually meets this
37+
* assumption, but only serves as an initial filter.
38+
*/
39+
protected function couldBeBinaryData(mixed $input): bool
40+
{
41+
if (!is_string($input) && !$input instanceof Stringable) {
42+
return false;
43+
}
44+
45+
$input = (string) $input;
46+
47+
// contains non printable ascii
48+
if (preg_match('/[^ -~]/', $input) === 1) {
49+
return true;
50+
}
51+
52+
// contains only printable ascii
53+
if (preg_match('/^[ -~]+$/', $input) === 1) {
54+
return false;
55+
}
56+
57+
return true;
58+
}
59+
60+
/**
61+
* Returns true if the specified content could be a data uri.
62+
*
63+
* This does not necessarily mean that the content actually meets this
64+
* assumption, but only serves as an initial filter.
65+
*/
66+
protected function couldBeDataUrl(mixed $input): bool
67+
{
68+
if ($input instanceof DataUriInterface) {
69+
return true;
70+
}
71+
72+
return is_string($input) && str_starts_with($input, 'data:');
73+
}
74+
75+
/**
76+
* Returns true if the specified content could be a file path.
77+
*
78+
* This does not necessarily mean that the content actually meets this
79+
* assumption, but only serves as an initial filter.
80+
*/
81+
protected function couldBeFilePath(mixed $input): bool
82+
{
83+
if (!is_string($input) && !$input instanceof Stringable) {
84+
return false;
85+
}
86+
87+
$input = (string) $input;
88+
89+
if (strlen($input) > PHP_MAXPATHLEN) {
90+
return false;
91+
}
92+
93+
if (str_starts_with($input, DIRECTORY_SEPARATOR)) {
94+
return true;
95+
}
96+
97+
if (preg_match('/[^ -~]/', $input) === 1) {
98+
return false;
99+
}
100+
101+
return true;
102+
}
103+
}

0 commit comments

Comments
 (0)