class Image

Same name in this branch
  1. 8.9.x core/modules/media/src/Plugin/media/Source/Image.php \Drupal\media\Plugin\media\Source\Image
  2. 8.9.x core/modules/image/src/Plugin/InPlaceEditor/Image.php \Drupal\image\Plugin\InPlaceEditor\Image
  3. 8.9.x core/lib/Drupal/Core/Image/Image.php \Drupal\Core\Image\Image
Same name in other branches
  1. 9 core/modules/media/src/Plugin/media/Source/Image.php \Drupal\media\Plugin\media\Source\Image
  2. 9 core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Image.php \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Image
  3. 9 core/modules/quickedit/src/Plugin/InPlaceEditor/Image.php \Drupal\quickedit\Plugin\InPlaceEditor\Image
  4. 9 core/modules/image/src/Plugin/InPlaceEditor/Image.php \Drupal\image\Plugin\InPlaceEditor\Image
  5. 9 core/lib/Drupal/Core/Image/Image.php \Drupal\Core\Image\Image
  6. 9 core/lib/Drupal/Component/Utility/Image.php \Drupal\Component\Utility\Image
  7. 10 core/modules/media/src/Plugin/media/Source/Image.php \Drupal\media\Plugin\media\Source\Image
  8. 10 core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Image.php \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Image
  9. 10 core/lib/Drupal/Core/Image/Image.php \Drupal\Core\Image\Image
  10. 10 core/lib/Drupal/Component/Utility/Image.php \Drupal\Component\Utility\Image
  11. 11.x core/modules/media/src/Plugin/media/Source/Image.php \Drupal\media\Plugin\media\Source\Image
  12. 11.x core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Image.php \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Image
  13. 11.x core/lib/Drupal/Core/Image/Image.php \Drupal\Core\Image\Image
  14. 11.x core/lib/Drupal/Component/Utility/Image.php \Drupal\Component\Utility\Image

Provides helpers to operate on images.

Hierarchy

  • class \Drupal\Component\Utility\Image

Expanded class hierarchy of Image

Related topics

2 files declare their use of Image
ImageTest.php in core/tests/Drupal/Tests/Component/Utility/ImageTest.php
ScaleImageEffect.php in core/modules/image/src/Plugin/ImageEffect/ScaleImageEffect.php
266 string references to 'Image'
aggregator_entity_extra_field_info in core/modules/aggregator/aggregator.module
Implements hook_entity_extra_field_info().
AjaxFileManagedMultipleTest::testMultipleFilesUpload in core/modules/file/tests/src/FunctionalJavascript/AjaxFileManagedMultipleTest.php
Test if managed file form element works well with multiple files upload.
CKEditorIntegrationTest::setUp in core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
CKEditorIntegrationTest::setUp in core/modules/media_library/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
CKEditorIntegrationTest::setUp in core/modules/media/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php

... See full list

File

core/lib/Drupal/Component/Utility/Image.php, line 10

Namespace

Drupal\Component\Utility
View source
class Image {
    
    /**
     * Scales image dimensions while maintaining aspect ratio.
     *
     * The resulting dimensions can be smaller for one or both target dimensions.
     *
     * @param array $dimensions
     *   Dimensions to be modified - an array with components width and height, in
     *   pixels.
     * @param int $width
     *   (optional) The target width, in pixels. If this value is NULL then the
     *   scaling will be based only on the height value.
     * @param int $height
     *   (optional) The target height, in pixels. If this value is NULL then the
     *   scaling will be based only on the width value.
     * @param bool $upscale
     *   (optional) Boolean indicating that images smaller than the target
     *   dimensions will be scaled up. This generally results in a low quality
     *   image.
     *
     * @return bool
     *   TRUE if $dimensions was modified, FALSE otherwise.
     */
    public static function scaleDimensions(array &$dimensions, $width = NULL, $height = NULL, $upscale = FALSE) {
        $aspect = $dimensions['height'] / $dimensions['width'];
        // Calculate one of the dimensions from the other target dimension,
        // ensuring the same aspect ratio as the source dimensions. If one of the
        // target dimensions is missing, that is the one that is calculated. If both
        // are specified then the dimension calculated is the one that would not be
        // calculated to be bigger than its target.
        if ($width && !$height || $width && $height && $aspect < $height / $width) {
            $height = (int) round($width * $aspect);
        }
        else {
            $width = (int) round($height / $aspect);
        }
        // Don't upscale if the option isn't enabled.
        if (!$upscale && ($width >= $dimensions['width'] || $height >= $dimensions['height'])) {
            return FALSE;
        }
        $dimensions['width'] = $width;
        $dimensions['height'] = $height;
        return TRUE;
    }

}

Members

Title Sort descending Modifiers Object type Summary
Image::scaleDimensions public static function Scales image dimensions while maintaining aspect ratio.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.