Automatic Image Format Detection in PHP

Finding out what file type something is in php

You can use php build in function: https://secure.php.net/manual/de/function.mime-content-type.php

<?php
echo mime_content_type('php.gif') . "\n";
echo mime_content_type('test.php');
?>

Will output:

image/gif
text/plain

Condition Example I: JPG Images only

if (\mime_content_type('image.jpg' /* full path to image */) === "image/jpg") {
// do something
}

Condition Example II: Any type of images

if (\strpos(\mime_content_type('image.jpg' /* full path to image */), 'image/') !== false) {
// do something
}

How to get the image type in php

use getimagesize() or exif_imagetype()

// integer - for example: IMAGETYPE_GIF, IMAGETYPE_JPEG etc.
$type = exif_imagetype($_FILES['image']['tmp_name']);

and

$info   = getimagesize($_FILES['image']['tmp_name']);
$mime = $info['mime']; // mime-type as string for ex. "image/jpeg" etc.
$width = $info[0]; // width as integer for ex. 512
$height = $info[1]; // height as integer for ex. 384
$type = $info[2]; // same as exif_imagetype

Mind that exif_imagetype is much faster than getimagesize. Check documentation for more info.

How can find image type without extension in php?

You can use getimagesize to get the mime type, and switch on the type to create generate extension.

In code this could look like something like this:

$image_info = getImageSize($path);
switch ($image_info['mime']) {
case 'image/gif':
$extension = '.gif';
break;
case 'image/jpeg':
$extension = '.jpg';
break;
case 'image/png':
$extension = '.png';
break;
default:
// handle errors
break;
}

Auto Detect Image for 360 nature in PHP

According to Facebook 360 Group:

There isn't yet a standard for tagging a photo as containing 360 content.

It suggest that you look for the EXIF tag

Projection Type : equirectangular

You can also look for

Use Panorama Viewer : True

Those two tags are present on photos taken with my LG 360.



Related Topics



Leave a reply



Submit