Skip to content

Commit

Permalink
FileUpload: detects supported images [Closes #224]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2023
1 parent 17f16c1 commit 54d7971
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Http/FileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ final class FileUpload
{
use Nette\SmartObject;

public const ImageMimeTypes = ['image/gif', 'image/png', 'image/jpeg', 'image/webp'];

/** @deprecated use FileUpload::ImageMimeTypes */
public const IMAGE_MIME_TYPES = self::ImageMimeTypes;
/** @deprecated */
public const IMAGE_MIME_TYPES = ['image/gif', 'image/png', 'image/jpeg', 'image/webp'];

/** @var string */
private $name;
Expand Down Expand Up @@ -214,12 +212,20 @@ function (string $message) use ($dest): void {


/**
* Returns true if the uploaded file is a JPEG, PNG, GIF, or WebP image.
* Returns true if the uploaded file is an image supported by PHP.
* Detection is based on its signature, the integrity of the file is not checked. Requires PHP extension fileinfo.
*/
public function isImage(): bool
{
return in_array($this->getContentType(), self::ImageMimeTypes, true);
$flag = imagetypes();
$types = array_filter([
$flag & IMG_GIF ? 'image/gif' : null,
$flag & IMG_JPG ? 'image/jpeg' : null,
$flag & IMG_PNG ? 'image/png' : null,
$flag & IMG_WEBP ? 'image/webp' : null,
$flag & 256 ? 'image/avif' : null, // IMG_AVIF
]);
return in_array($this->getContentType(), $types, true);
}


Expand Down

0 comments on commit 54d7971

Please sign in to comment.