You are here

public static function BlazyBreakpoint::widthFromDescriptors in Blazy 7

Gets the numeric "width" part from a descriptor.

3 calls to BlazyBreakpoint::widthFromDescriptors()
BlazyBreakpoint::attributes in src/BlazyBreakpoint.php
Provides re-usable breakpoint data-attributes for IMG or DIV element.
BlazyBreakpoint::buildDataBlazy in src/BlazyBreakpoint.php
Builds breakpoints suitable for top-level [data-blazy] wrapper attributes.
BlazyBreakpointTest::testWidthFromDescriptors in tests/BlazyBreakpoint.test
Test widthFromDescriptors.

File

src/BlazyBreakpoint.php, line 83

Class

BlazyBreakpoint
Implements BlazyBreakpointInterface.

Namespace

Drupal\blazy

Code

public static function widthFromDescriptors($descriptor = '') {
  if (empty($descriptor)) {
    return FALSE;
  }

  // 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;
}