Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By larowlan on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Introduced in version:
8.0-ALPHA3
Issue links:
Description:
Utility methods in image.inc were removed in favour of a classed image object.
A new service 'image.factory' was added for easy instantiation of image objects using the active image toolkit.
Image loading and saving
What | Drupal 7 | Drupal 8 |
---|---|---|
Load an image from a file: | $image = image_load($file->getFileUri()); |
$image = Drupal::service('image.factory')->get($file->getFileUri()); |
Save image at its original file location: | image_save($image); |
$image->save(); |
Image information
What | Drupal 7 | Drupal 8 |
---|---|---|
Get information about the image: | image_get_info($image); |
removed |
Check if an image is valid (i.e. can be manipulated/saved to file storage): |
n/a | $is_valid = $image->isValid(); |
Get image width: |
|
$width = $image->getWidth(); |
Get image height: |
|
$height = $image->getHeight(); |
Get the MIME type of the image file: |
|
$mime_type = $image->getMimeType(); |
Get image file size: |
|
$filesize = $image->getFileSize(); |
Get the URI of the image file: | N/A | $source = $image->getSource(); |
Get a list of image file extensions supported by the current toolkit: | N/A | $image_factory = \Drupal::service('image.factory'); $supported_extensions = $image_factory->getSupportedExtensions(); |
Image manipulation
What | Drupal 7 | Drupal 8 |
---|---|---|
Create a new image: | n/a | $image->createNew($width, $height, 'jpeg'); |
Scale and crop image: | image_scale_and_crop($image, $width, $height); |
$image->scaleAndCrop($width, $height); |
Scale image: | image_scale($image, $width, $height); |
$image->scale($width, $height); |
Resize image: | image_resize($image, $width, $height); |
$image->resize($width, $height); |
Rotate image: | image_rotate($image, $degrees); |
$image->rotate($degrees); |
Crop image: | image_crop($image, $x, $y, $width, $height); |
$image->crop($x, $y, $width, $height); |
Desaturate image (convert to grayscale): | image_desaturate($image); |
$image->desaturate(); |
Convert image to a different format: | n/a | $image->convert('jpeg'); |
Apply a custom toolkit operation to the image: | image_toolkit_invoke('my_op', $image, array('p1' => $parm1, 'p2' => $parm2)); |
$image->apply('my_op', array('p1' => $parm1, 'p2' => $parm2)); |
Accessing an Image's toolkit
What | Drupal 7 | Drupal 8 |
---|---|---|
Access image toolkit instance: | N/A | $toolkit = $image->getToolkit(); |
Get the ID of the image toolkit: | N/A | $toolkit_id = $image->getToolkitId(); |
GD Toolkit - get the standard file extension of the image, based on its internal type (for GD, jpeg/png/gif): |
|
$extension = image_type_to_extension($image->getToolkit()->getType(), FALSE); |
Impacts:
Module developers