"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Component/Utility/Image.php" (26 Nov 2020, 2006 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Image.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\Component\Utility;
    4 
    5 /**
    6  * Provides helpers to operate on images.
    7  *
    8  * @ingroup utility
    9  */
   10 class Image {
   11 
   12   /**
   13    * Scales image dimensions while maintaining aspect ratio.
   14    *
   15    * The resulting dimensions can be smaller for one or both target dimensions.
   16    *
   17    * @param array $dimensions
   18    *   Dimensions to be modified - an array with components width and height, in
   19    *   pixels.
   20    * @param int $width
   21    *   (optional) The target width, in pixels. If this value is NULL then the
   22    *   scaling will be based only on the height value.
   23    * @param int $height
   24    *   (optional) The target height, in pixels. If this value is NULL then the
   25    *   scaling will be based only on the width value.
   26    * @param bool $upscale
   27    *   (optional) Boolean indicating that images smaller than the target
   28    *   dimensions will be scaled up. This generally results in a low quality
   29    *   image.
   30    *
   31    * @return bool
   32    *   TRUE if $dimensions was modified, FALSE otherwise.
   33    */
   34   public static function scaleDimensions(array &$dimensions, $width = NULL, $height = NULL, $upscale = FALSE) {
   35     $aspect = $dimensions['height'] / $dimensions['width'];
   36 
   37     // Calculate one of the dimensions from the other target dimension,
   38     // ensuring the same aspect ratio as the source dimensions. If one of the
   39     // target dimensions is missing, that is the one that is calculated. If both
   40     // are specified then the dimension calculated is the one that would not be
   41     // calculated to be bigger than its target.
   42     if (($width && !$height) || ($width && $height && $aspect < $height / $width)) {
   43       $height = (int) round($width * $aspect);
   44     }
   45     else {
   46       $width = (int) round($height / $aspect);
   47     }
   48 
   49     // Don't upscale if the option isn't enabled.
   50     if (!$upscale && ($width >= $dimensions['width'] || $height >= $dimensions['height'])) {
   51       return FALSE;
   52     }
   53 
   54     $dimensions['width'] = $width;
   55     $dimensions['height'] = $height;
   56     return TRUE;
   57   }
   58 
   59 }