Skip to content

Commit

Permalink
Return mime type #69
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Jul 24, 2019
1 parent 6291c44 commit 20db1c2
Show file tree
Hide file tree
Showing 49 changed files with 356 additions and 446 deletions.
27 changes: 8 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ $detector->addProvider(new RawProvider());
$detector->addProvider(new VectorProvider());
$detector->addProvider(new RasterProvider());

echo $detector->getImageTypeFromFile($file)->toString(); // jpeg
$imageType = $detector->getImageTypeFromFile($file);

// Get the image format
echo $imageType->getFormat(); // jpeg

// Get the mime type
echo $imageType->getMimeType(); // image/jpeg
```

### Detect the image type of in-memory object
Expand All @@ -123,24 +129,7 @@ $detector = new ImageTypeDetector();
// Add image detectors
$detector->addProvider(new RasterProvider());

echo $detector->getImageTypeFromFile($file)->toString();
```

### Detect mime type of file

```php
use Selective\ImageType\ImageTypeDetector;
use Selective\ImageType\Provider\RasterProvider;
use SplFileObject;

$file = new SplFileObject('example.jpg');

$detector = new ImageTypeDetector();

// Add image detectors
$detector->addProvider(new RasterProvider());

echo $detector->getMimeTypeFromFile($file)->toString(); // image/jpeg
echo $detector->getImageTypeFromFile($file)->getFormat();
```

## Similar libraries
Expand Down
4 changes: 3 additions & 1 deletion src/Detector/AiDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -23,6 +25,6 @@ public function detect(SplFileObject $file): ?ImageType
{
$bytes = (string)$file->fread(10);

return $bytes === '%!PS-Adobe' ? new ImageType(ImageType::AI) : null;
return $bytes === '%!PS-Adobe' ? new ImageType(ImageFormat::AI, MimeType::APPLICATION_POSTSCRIPT) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/AniDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -23,6 +25,6 @@ public function detect(SplFileObject $file): ?ImageType
$file->fread(4);
$aniSignature = $file->fread(4);

return $riffSignature === 'RIFF' && $aniSignature === 'ACON' ? new ImageType(ImageType::ANI) : null;
return $riffSignature === 'RIFF' && $aniSignature === 'ACON' ? new ImageType(ImageFormat::ANI, MimeType::APPLICATION_X_NAVI_ANIMATION) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/BmpDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -19,6 +21,6 @@ final class BmpDetector implements DetectorInterface
*/
public function detect(SplFileObject $file): ?ImageType
{
return $file->fread(2) === 'BM' ? new ImageType(ImageType::BMP) : null;
return $file->fread(2) === 'BM' ? new ImageType(ImageFormat::BMP, MimeType::IMAGE_BMP) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/CineonDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -23,6 +25,6 @@ public function detect(SplFileObject $file): ?ImageType
{
$bytes = bin2hex((string)$file->fread(4));

return $bytes === '802a5fd7' || $bytes === 'd75f2a80' ? new ImageType(ImageType::CIN) : null;
return $bytes === '802a5fd7' || $bytes === 'd75f2a80' ? new ImageType(ImageFormat::CIN, MimeType::IMAGE_CINEON) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/Cr2Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -28,6 +30,6 @@ public function detect(SplFileObject $file): ?ImageType

$bytes = $file->fread(8);

return $bytes === "\x2a\0\x10\0\0\0CR" ? new ImageType(ImageType::CR2) : null;
return $bytes === "\x2a\0\x10\0\0\0CR" ? new ImageType(ImageFormat::CR2, MimeType::IMAGE_CR2) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/Cr3Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -23,6 +25,6 @@ public function detect(SplFileObject $file): ?ImageType

$bytes = $file->fread(7);

return $bytes === 'ftypcrx' ? new ImageType(ImageType::CR3) : null;
return $bytes === 'ftypcrx' ? new ImageType(ImageFormat::CR3, MimeType::IMAGE_CR3) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/CurDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -19,6 +21,6 @@ final class CurDetector implements DetectorInterface
*/
public function detect(SplFileObject $file): ?ImageType
{
return $file->fread(3) === "\0\0\2" ? new ImageType(ImageType::CUR) : null;
return $file->fread(3) === "\0\0\2" ? new ImageType(ImageFormat::CUR, MimeType::IMAGE_X_ICON) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/DcmDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -21,6 +23,6 @@ public function detect(SplFileObject $file): ?ImageType
{
$file->fread(128);

return $file->fread(4) === 'DICM' ? new ImageType(ImageType::DICOM) : null;
return $file->fread(4) === 'DICM' ? new ImageType(ImageFormat::DICOM, MimeType::APPLICATION_DICOM) : null;
}
}
5 changes: 4 additions & 1 deletion src/Detector/DngDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand Down Expand Up @@ -30,6 +32,7 @@ public function detect(SplFileObject $file): ?ImageType
$bytes = (string)$file->fread(12);

return ((strpos($bytes, "\x04") !== false || strpos($bytes, "\x02") !== false) &&
strpos($bytes, "\x01") && substr_count($bytes, "\0") >= 2) ? new ImageType(ImageType::DNG) : null;
strpos($bytes, "\x01") && substr_count($bytes, "\0") >= 2) ?
new ImageType(ImageFormat::DNG, MimeType::IMAGE_X_ADOBE_DNG) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/DpxDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -21,6 +23,6 @@ public function detect(SplFileObject $file): ?ImageType
{
$bytes = (string)$file->fread(4);

return $bytes === 'SDPX' || $bytes === 'XPDS' ? new ImageType(ImageType::DPX) : null;
return $bytes === 'SDPX' || $bytes === 'XPDS' ? new ImageType(ImageFormat::DPX, MimeType::IMAGE_X_DPX) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/EmfDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand Down Expand Up @@ -36,6 +38,6 @@ public function detect(SplFileObject $file): ?ImageType
$emfPlusSignature = (string)$file->fread(4);
$hasEmfPlus = $emfPlusSignature === 'EMF+';

return $hasEmf && !$hasEmfPlus ? new ImageType(ImageType::EMF) : null;
return $hasEmf && !$hasEmfPlus ? new ImageType(ImageFormat::EMF, MimeType::IMAGE_X_EMF) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/EmfPlusDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand Down Expand Up @@ -36,6 +38,6 @@ public function detect(SplFileObject $file): ?ImageType
$emfPlusSignature = (string)$file->fread(4);
$hasEmfPlus = $emfPlusSignature === 'EMF+';

return $hasEmf && $hasEmfPlus ? new ImageType(ImageType::EMF_PLUS) : null;
return $hasEmf && $hasEmfPlus ? new ImageType(ImageFormat::EMF_PLUS, MimeType::IMAGE_X_EMF) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/ExrDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -21,6 +23,6 @@ public function detect(SplFileObject $file): ?ImageType
{
$bytes = (string)$file->fread(4);

return bin2hex($bytes) === '762f3101' ? new ImageType(ImageType::EXR) : null;
return bin2hex($bytes) === '762f3101' ? new ImageType(ImageFormat::EXR, MimeType::IMAGE_X_EXR) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/Fr3Detector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -28,6 +30,6 @@ public function detect(SplFileObject $file): ?ImageType

$bytes = (string)$file->fread(512);

return strpos($bytes, 'Hasselblad') > 10 ? new ImageType(ImageType::FR3) : null;
return strpos($bytes, 'Hasselblad') > 10 ? new ImageType(ImageFormat::FR3, MimeType::IMAGE_X_3_FR) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/GifDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -19,6 +21,6 @@ final class GifDetector implements DetectorInterface
*/
public function detect(SplFileObject $file): ?ImageType
{
return $file->fread(2) === 'GI' ? new ImageType(ImageType::GIF) : null;
return $file->fread(2) === 'GI' ? new ImageType(ImageFormat::GIF, MimeType::IMAGE_GIF) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/HdrDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -21,6 +23,6 @@ public function detect(SplFileObject $file): ?ImageType
{
$bytes = (string)$file->fread(10);

return $bytes === '#?RADIANCE' || $bytes === '#?RGBE' ? new ImageType(ImageType::HDR) : null;
return $bytes === '#?RADIANCE' || $bytes === '#?RGBE' ? new ImageType(ImageFormat::HDR, MimeType::IMAGE_VND_RADIANCE) : null;
}
}
24 changes: 13 additions & 11 deletions src/Detector/HeicDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand Down Expand Up @@ -33,26 +35,26 @@ public function detect(SplFileObject $file): ?ImageType
// Source: https://github.com/strukturag/libheif/issues/83
$ccCodes = [
// Usual HEIF images
'heic' => ImageType::HEIC,
'heic' => ImageFormat::HEIC,
// 10bit images, or anything that uses h265 with range extension
'heix' => ImageType::HEIC,
'heix' => ImageFormat::HEIC,
// Brands for image sequences
'hevc' => ImageType::HEIC_SEQUENCE,
'hevx' => ImageType::HEIC_SEQUENCE,
'hevc' => ImageFormat::HEIC_SEQUENCE,
'hevx' => ImageFormat::HEIC_SEQUENCE,
// Multiview
'heim' => ImageType::HEIC,
'heim' => ImageFormat::HEIC,
// Scalable
'heis' => ImageType::HEIC,
'heis' => ImageFormat::HEIC,
// Multiview sequence
'hevm' => ImageType::HEIC_SEQUENCE,
'hevm' => ImageFormat::HEIC_SEQUENCE,
// Scalable sequence
'hevs' => ImageType::HEIC_SEQUENCE,
'hevs' => ImageFormat::HEIC_SEQUENCE,
// Special brands
'mif1' => ImageType::HEIC,
'mif1' => ImageFormat::HEIC,
// Equivalent case for image sequences
'msf1' => ImageType::HEIC_SEQUENCE,
'msf1' => ImageFormat::HEIC_SEQUENCE,
];

return $bytes === 'ftyp' && isset($ccCodes[$ccCode]) ? new ImageType($ccCodes[$ccCode]) : null;
return $bytes === 'ftyp' && isset($ccCodes[$ccCode]) ? new ImageType($ccCodes[$ccCode], MimeType::IMAGE_HEIC) : null;
}
}
4 changes: 3 additions & 1 deletion src/Detector/IcoDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Selective\ImageType\Detector;

use Selective\ImageType\ImageFormat;
use Selective\ImageType\ImageType;
use Selective\ImageType\MimeType;
use SplFileObject;

/**
Expand All @@ -19,6 +21,6 @@ final class IcoDetector implements DetectorInterface
*/
public function detect(SplFileObject $file): ?ImageType
{
return $file->fread(3) === "\0\0\1" ? new ImageType(ImageType::ICO) : null;
return $file->fread(3) === "\0\0\1" ? new ImageType(ImageFormat::ICO, MimeType::IMAGE_X_ICON) : null;
}
}
Loading

0 comments on commit 20db1c2

Please sign in to comment.