Change record status: 
Project: 
Introduced in branch: 
8.x
Introduced in version: 
8.0-ALPHA4
Description: 

In Drupal 7 the image extension was used to test if we got a valid image or to test the image format. In Drupal 8 we use \Drupal\Core\Image\ImageInterface::isValid() to test the image validity and \Drupal\Core\Image\ImageInterface::getMimeType() to get or test the image MIME type.

D7

<?php
if ($image->info['extension'] == 'gif') { }
?>

D8

<?php
if ($image->getMimeType() == 'image/gif') { }
?>

Image toolkits should manage internally what image formats they can handle. They should implement a getSupportedExtensions() public static method, that returns an array of image file extensions supported. Example from the core provided GDToolkit:

<?php
 
/**
   * {@inheritdoc}
   */
 
public static function getSupportedExtensions() {
   
$extensions = array();
    foreach (static::
supportedTypes() as $image_type) {
     
$extensions[] = Unicode::strtolower(image_type_to_extension($image_type, FALSE));
    }
    return
$extensions;
  }

 
/**
   * Returns a list of image types supported by the toolkit.
   *
   * @return array
   *   An array of available image types. An image type is represented by a PHP
   *   IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.).
   */
 
protected static function supportedTypes() {
    return array(
IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
  }
?>

The image interface (\Drupal\Core\Image\ImageInterface) is declaring 2 new methods:

  1. isValid(): Is used to check if the image from the file is correct and supported by the active toolkit.
  2. getMimeType(): This method is returning the image MIME type.
Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done