function image_get_info

Gets details about an image.

Drupal supports GIF, JPG and PNG file formats when used with the GD toolkit, and may support others, depending on which toolkits are installed.

Parameters

string $filepath: String specifying the path of the image file.

\Drupal\system\Plugin\ImageToolkitInterface $toolkit: (optional) An image toolkit object to override the default.

Return value

array FALSE, if the file could not be found or is not an image. Otherwise, a keyed array containing information about the image:

  • "width": Width, in pixels.
  • "height": Height, in pixels.
  • "extension": Commonly used file extension for the image.
  • "mime_type": MIME type ('image/jpeg', 'image/gif', 'image/png').
  • "file_size": File size in bytes.

Related topics

16 calls to image_get_info()
file_validate_image_resolution in drupal/core/modules/file/file.module
Verifies that image dimensions are within the specified maximum and minimum.
file_validate_is_image in drupal/core/modules/file/file.module
Checks that the file is recognized by image_get_info() as an image.
hook_file_download in drupal/core/modules/system/system.api.php
Control access to private file downloads and specify HTTP headers.
hook_prepare in drupal/core/modules/node/node.api.php
Act on a node object about to be shown on the add/edit form.
ImageDimensionsTest::testImageDimensions in drupal/core/modules/image/lib/Drupal/image/Tests/ImageDimensionsTest.php
Test styled image dimensions cumulatively.

... See full list

File

drupal/core/includes/image.inc, line 58
API for manipulating images.

Code

function image_get_info($filepath, ImageToolkitInterface $toolkit = NULL) {
  $details = FALSE;
  if (!is_file($filepath) && !is_uploaded_file($filepath)) {
    return $details;
  }
  if ($toolkit === NULL) {
    $toolkit = Drupal::service('image.toolkit');
  }
  if ($toolkit) {
    $image = new stdClass();
    $image->source = $filepath;
    $image->toolkit = $toolkit;
    $details = $toolkit
      ->getInfo($image);
    if (isset($details) && is_array($details)) {
      $details['file_size'] = filesize($filepath);
    }
  }
  return $details;
}