selective-php / image-type

Image type (format) detection for PHP
MIT License
7 stars 4 forks source link

Consider returning MIME content type #69

Closed peter279k closed 5 years ago

peter279k commented 5 years ago

As title, we should consider implementing this work.

We only return image type extension name after detecting the specific image. Such as jpg, png and so on.

Sometimes developers want to return the MIME content type. Such as image/png, image/webp and etc.

@odan, I think we can implement this issue.

odan commented 5 years ago

I think this would be a good idea. Would you like to create a PR?

peter279k commented 5 years ago

@odan, thank you for your reply.

I think I can create this PR. Please consider following points:

odan commented 5 years ago

How to let the developers decide to return MIME content type or image formats?

I think this extra decision is not really necessary (and too complicated btw). We should keep it simple. Instead we could give all the ImageType constant values a proper mime type value.

For example

Before:

public const JPEG = 'jpeg';

After:

public const JPEG = 'image/jpeg';

We should have the MIME content type lists before creating this PR.

We just can google it or find it on wikipedia.

peter279k commented 5 years ago

Hi @odan, thank you for your reply and suggestion.

Some of the image format has many MIME content types.

For example, PBM - Portable Bit Map (HDR) format has the image/x‑portable‑bitmap, image/x‑portable‑graymap, image/x‑portable‑pixmap and image/x‑portable‑anymap MIME content types.

How to solve this problem? Should we return a MIME content type arrays?

References

odan commented 5 years ago

We already have different and unique const values for this types.

Before:

public const PBM = 'pbm';
public const PGM = 'pgm';
public const PPM = 'ppm';

After

public const PBM = 'image/x‑portable‑bitmap';
public const PGM = 'image/x‑portable‑graymap';
public const PPM = 'image/x‑portable‑pixmap';

image/x‑portable‑anymap is not releavant here, because it's not a specific format.

peter279k commented 5 years ago

@odan, thank you for your reply. It looks great because we have the different formats about PBM.

But how about the HEIC image format? It should be image/heif, image/heic,image/heif-sequence, and image/heic-sequence possibly.

I think we don't have different HEIC image formats are like PBM.

How to solve this current problem?

References

odan commented 5 years ago

HEIC is the file format name Apple has chosen for the new HEIF standard.

This means, I would ignore the image/heif and image/heif-sequence mime type because we cannot identify / detect this images.

Let's focus us on image/heic, image/heif-sequence. We are detecting the HEIC images and HEIF-sequences already. We just have to add a new image type e.g. HEIF_SEQUENCE and add an additional mapping. Instead of returning a fixed value ImageType::HEIC, we could detect here ImageType::HEIF_SEQUENCE too (depending on the CC codes).

https://github.com/selective-php/image-type/blob/master/src/Detector/HeicDetector.php#L34

After that we can differentiate between these two mime types:

Before

public const HEIC = 'heic';

After

public const HEIC = 'image/heic';
public const HEIF_SEQUENCE = 'image/heic-sequence';
odan commented 5 years ago

Here is a first list with mime types. I have marked all unclear and problematic mime types.

  1. AI => application/postscript is not a exclusive mime type for AI
  2. CR2 => image/x-dcraw is not a exclusive mime type for CR2
  3. CR3 => ? is unknown
  4. image/x-emf+ is not a official mime type
  5. image/jpeg-hdr is not a official mime type
  6. HEIC_SEQUENCE = 'image/heic-sequence is not a exclusive mime type

Conclusion: I think the idea adding the mime type to the constants will not work. We need another solution. Maybe we add a new class ImageMimeType and a method: getMimeType(ImageType $imageType): string which returns the best possible mime type or throws a Exception on error.

// https://fr.wikipedia.org/wiki/Adobe_Illustrator_Artwork
public const AI = 'application/postscript'; // ???
public const ANI = 'application/x-navi-animation';
public const BMP = 'image/webp';
public const CIN = 'image/cineon';
public const CR2 = 'image/x-dcraw'; // ???
public const CR3 = 'cr3'; // ???
public const CUR = 'image/x-icon';
public const DICOM = 'application/dicom';
public const DNG = 'image/x-adobe-dng';
public const DPX = 'image/x-dpx';
public const EMF = 'image/x-emf';
public const EMFPLUS = 'image/x-emf+'; // ???
public const EXR = 'image/x-exr';
public const FR3 = 'image/x-3fr';
public const GIF = 'image/gif';
public const HDR = 'image/vnd.radiance';
public const HEIC = 'image/heic';
public const HEIC_SEQUENCE = 'image/heic-sequence'; // ???
public const ICO = 'image/x-icon';
public const JP2 = 'image/jp2';
public const JPEG = 'image/jpeg';
public const JPEGHDR = 'image/jpeg-hdr'; // ???
public const JPM = 'image/jpm';
public const MNG = 'video/x-mng';
public const ORF = 'image/x-olympus-orf';
public const PBM = 'image/x-portable-bitmap';
public const PDN = 'image/x-paintnet';
public const PEF = 'image/x-pentax-raw';
public const PFM = 'image/x-portable-floatmap';
public const PGM = 'image/x-portable-graymap';
public const PNG = 'image/png';
public const PPM = 'image/x-portable-pixmap';
public const PSB = 'image/x-psb';
public const PSD = 'image/vnd.adobe.photoshop';
public const RW2 = 'image/x-panasonic-rw2';
public const SVG = 'image/svg+xml';
public const SWF = 'application/x-shockwave-flash';
public const TIFF = 'image/tiff';
public const WEBP = 'image/webp';
public const WMF = 'image/x-wmf';
public const XCF = 'image/x-xcf';
peter279k commented 5 years ago

@odan, thank you for your effort.

I will update this MIME content type lists at my available time.

peter279k commented 5 years ago

I think some MIME content types contain many different image types.

We can use the approach that @odan mentioned about creating ImageMimeType class.

I think we also return the possible MIME type or exception error via ImageMimeType::getMimeType(ImageType $imageType) method call.

peter279k commented 5 years ago

@odan, I also found this package named mimesniffer.

And I think we do the similar works for MIME content types detection.

Should we consider integrating this package :confused: ?

odan commented 5 years ago

The mimesniffer package does't support all image format we are supporting. The results would be incomplete. We also don't need a second image format / type detection.

What do you think about the the change?

Example: https://github.com/selective-php/image-type#detect-mime-type-of-file

peter279k commented 5 years ago

@odan, thank you for your reply.

I think it seems that we create our MIME types are for our image-type package.

odan commented 5 years ago

I'ts already "done". Please check the current version. We may need better tests for this new feature.

peter279k commented 5 years ago

Hi @odan, thank you for your effort for this issue,

And I also found that this line in MimeTypeDetector class is strange.

It seems that the MIME type value will always be string.

Why do we have to add casting string syntax on this line?

odan commented 5 years ago

In the next step I will move the mime value to the imagetype value object.

odan commented 5 years ago

Please take a look at the latest changes. I had to separate the image "format" and the "mime type". The mime type is now part of the return value. You can read the value with

$imageType = $detector->getImageTypeFromFile($file);

$mime = $imageType->getMimeType();
odan commented 5 years ago

I think we can close this issue. What do you think?

peter279k commented 5 years ago

I think it's fine to close this issue.