You are here

public static function Blazy::widthFromDescriptors in Blazy 8

Same name and namespace in other branches
  1. 7 src/Blazy.php \Drupal\blazy\Blazy::widthFromDescriptors()

Gets the numeric "width" part from a descriptor.

3 calls to Blazy::widthFromDescriptors()
Blazy::buildBreakpointAttributes in src/Blazy.php
Provides re-usable breakpoint data-attributes.
BlazyManager::buildDataBlazy in src/BlazyManager.php
Builds breakpoints suitable for top-level [data-blazy] wrapper attributes.
BlazyUnitTest::testWidthFromDescriptors in tests/src/Unit/BlazyUnitTest.php
Test \Drupal\blazy\Blazy\widthFromDescriptors.

File

src/Blazy.php, line 378

Class

Blazy
Implements BlazyInterface.

Namespace

Drupal\blazy

Code

public static function widthFromDescriptors($descriptor = '') {

  // Dynamic multi-serving aspect ratio with backward compatibility.
  $descriptor = trim($descriptor);
  if (is_numeric($descriptor)) {
    return (int) $descriptor;
  }

  // Cleanup w descriptor to fetch numerical width for JS aspect ratio.
  $width = strpos($descriptor, "w") !== FALSE ? str_replace('w', '', $descriptor) : $descriptor;

  // If both w and x descriptors are provided.
  if (strpos($descriptor, " ") !== FALSE) {

    // If the position is expected: 640w 2x.
    list($width, $px) = array_pad(array_map('trim', explode(" ", $width, 2)), 2, NULL);

    // If the position is reversed: 2x 640w.
    if (is_numeric($px) && strpos($width, "x") !== FALSE) {
      $width = $px;
    }
  }
  return is_numeric($width) ? (int) $width : FALSE;
}