コード例 #1
0
 /**
  * Generates the HTML for the represented <source> or <img> element.
  *
  * @param SrcsetGeneratorInterface $srcset_gen
  *   The generator instance that will be used to generate the srcset
  *   attributes.
  * @param mixed                    $image
  *   The image representation that will be passed to $srcset_gen
  * @param string                   $alt
  *   The for an <img> `alt` attribute. Only used if $this->as_img is true.
  *
  * @return string
  *   The HTML for either a <source> or <img> element depending on the value
  *
  * @see SrcsetGeneratorInterface
  */
 public function renderWith(SrcsetGeneratorInterface $srcset_gen, $image, $alt = '')
 {
     $last = F\last($this->sizes);
     $srcset = F\map($this->sizes, function (Size $size) use($image, $srcset_gen) {
         return $srcset_gen->listFor($image, $size);
     });
     $srcset = F\unique(F\flatten($srcset), function (Src $src) {
         return $src->getUrl();
     });
     # Not really needed, but makes debugging nicer.
     usort($srcset, function (Src $l, Src $r) {
         $l = $l->getWidth() ?: (int) $l->getMultiplier();
         $r = $r->getWidth() ?: (int) $r->getMultiplier();
         return $l - $r;
     });
     $srcset = implode(', ', $srcset);
     $sizes = F\map($this->sizes, function (Size $size) use($last) {
         return $size === $last ? $size->renderWidthOnly() : (string) $size;
     });
     $sizes = implode(', ', $sizes);
     $media = !$this->as_img ? ' media="' . $last->getMediaQuery() . '"' : '';
     $alt = $this->as_img ? ' alt="' . htmlspecialchars($alt) . '"' : '';
     $attributes = "srcset=\"{$srcset}\" sizes=\"{$sizes}\"{$media}{$alt}";
     return $this->as_img ? "<img {$attributes}>" : "<source {$attributes}>";
 }
コード例 #2
0
 public function testPassNonCallableUndefinedFunction()
 {
     $this->expectArgumentError("Argument 2 passed to Functional\\unique() must be callable");
     unique($this->list, 'undefinedFunction');
 }
コード例 #3
0
 /**
  * Find the styles that that match the passed size.
  *
  * @param RImg\Size $size
  *   The size instance to match styles to.
  *
  * @return \stdClass[]
  *   List of styles that fit the passed size. Same object definition as
  *   $this->getStyles().
  *
  * @see DrupalImageStyle::getStyles()
  */
 private function stylesMatchingSize(RImg\Size $size)
 {
     # @todo Support Size instances without aspect ratio restrictions.
     # @todo Support Size instances with only width or only height restrictions.
     $styles = F\filter($this->getStyles(), function ($style) use($size) {
         return $style->firm and $size->matchesAspectRatio($style->width / $style->height);
     });
     $min_width = $size->getMinWidth();
     $max_width = $size->getMaxWidth();
     $styles = F\group($styles, function ($style) use($min_width, $max_width) {
         if ($style->width < $min_width) {
             return 'less';
         }
         if ($style->width > $max_width * 2) {
             # Multiplied for high DPI displays.
             return 'greater';
         }
         return 'within';
     });
     if (!empty($styles['greater'])) {
         # Make sure the end of the range is covered.
         $styles['within'][] = F\head($styles['greater']);
     }
     if (empty($styles['within'])) {
         if (!empty($styles['less'])) {
             # Better to have something too small than something too non-existent.
             $styles = [F\last($styles['less'])];
         } else {
             $styles = [];
         }
     } else {
         $styles = $styles['within'];
     }
     return F\unique($styles, function ($s) {
         return $s->width;
     });
 }