You are here

public static function Blazy::widthFromDescriptors in Blazy 7

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

Gets the numeric "width" part from a descriptor.

@todo deprecate for BlazyBreakpoint::widthFromDescriptors() blazy:7.x-2.0.

2 calls to Blazy::widthFromDescriptors()
Blazy::buildBreakpointAttributes in src/Blazy.php
Provides re-usable breakpoint data-attributes for IMG or DIV element.
BlazyManagerBase::buildDataBlazy in src/BlazyManagerBase.php
To be deprecated method.

File

src/Blazy.php, line 293

Class

Blazy
Implements BlazyInterface.

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