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
if ($image->info['extension'] == 'gif') { }
D8
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:
/**
* {@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:
isValid()
: Is used to check if the image from the file is correct and supported by the active toolkit.getMimeType()
: This method is returning the image MIME type.