| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Framework\Support; |
| 4 |
|
| 5 |
class MediaUploader |
| 6 |
{ |
| 7 |
/** |
| 8 |
* @var bool Whether to disable EXIF reading during upload |
| 9 |
*/ |
| 10 |
protected static bool $withoutExif = false; |
| 11 |
|
| 12 |
/** |
| 13 |
* @var bool Whether to disable generating |
| 14 |
* intermediate image sizes during upload |
| 15 |
*/ |
| 16 |
protected static bool $withoutSizes = false; |
| 17 |
|
| 18 |
/** |
| 19 |
* Disable EXIF reading during upload. |
| 20 |
* |
| 21 |
* @return static |
| 22 |
*/ |
| 23 |
public static function withoutExif() |
| 24 |
{ |
| 25 |
static::$withoutExif = true; |
| 26 |
return new static; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Disable generating intermediate image sizes during upload. |
| 31 |
* |
| 32 |
* @return static |
| 33 |
*/ |
| 34 |
public static function withoutSizes() |
| 35 |
{ |
| 36 |
static::$withoutSizes = true; |
| 37 |
return new static; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Disable both EXIF reading and image sizes during upload. |
| 42 |
* |
| 43 |
* @return static |
| 44 |
*/ |
| 45 |
public static function withoutExifAndSizes() |
| 46 |
{ |
| 47 |
static::$withoutExif = true; |
| 48 |
static::$withoutSizes = true; |
| 49 |
return new static; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Upload a local file array or remote URL, optionally disabling EXIF or image sizes. |
| 54 |
* |
| 55 |
* @param array|string $resource File array from $_FILES or remote URL |
| 56 |
* @param int $postId Optional post ID to attach media to |
| 57 |
* @param string|null $filename Optional filename for remote URLs |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public static function upload($resource, $postId = 0, $filename = null) |
| 62 |
{ |
| 63 |
$media = new Media(); |
| 64 |
|
| 65 |
if (static::$withoutExif) { |
| 66 |
$media->withoutExif(); |
| 67 |
} |
| 68 |
|
| 69 |
if (static::$withoutSizes) { |
| 70 |
$media->withoutSizes(); |
| 71 |
} |
| 72 |
|
| 73 |
// Reset flags so they don't persist between calls |
| 74 |
static::$withoutExif = false; |
| 75 |
static::$withoutSizes = false; |
| 76 |
|
| 77 |
return $media->upload($resource, $postId, $filename); |
| 78 |
} |
| 79 |
} |
| 80 |
|