diff --git a/system/src/Grav/Common/Page/Medium/ImageMedium.php b/system/src/Grav/Common/Page/Medium/ImageMedium.php index 17fd32151..017c40e19 100644 --- a/system/src/Grav/Common/Page/Medium/ImageMedium.php +++ b/system/src/Grav/Common/Page/Medium/ImageMedium.php @@ -56,6 +56,11 @@ class ImageMedium extends Medium 'zoomCrop' => [ 0, 1 ] ]; + /** + * @var array + */ + protected $derivatives = []; + /** * Construct. * @@ -157,22 +162,68 @@ class ImageMedium extends Medium */ public function srcset($reset = true) { - if (empty($this->alternatives)) { + if (empty($this->alternatives) && empty($this->derivatives)) { if ($reset) { $this->reset(); } return ''; } - $srcset = [ $this->url($reset) . ' ' . $this->get('width') . 'w' ]; + if (!empty($this->derivatives)) { + asort($this->derivatives); - foreach ($this->alternatives as $ratio => $medium) { - $srcset[] = $medium->url($reset) . ' ' . $medium->get('width') . 'w'; + foreach ($this->derivatives as $url => $width) { + $srcset[] = $url . ' ' . $width . 'w'; + } + + $srcset[] = $this->url($reset) . ' ' . $this->get('width') . 'w'; + } + else { + $srcset = [ $this->url($reset) . ' ' . $this->get('width') . 'w' ]; + foreach ($this->alternatives as $ratio => $medium) { + $srcset[] = $medium->url($reset) . ' ' . $medium->get('width') . 'w'; + } } return implode(', ', $srcset); } + /** + * Generate derivatives + * + * @param int $min_width + * @param int $max_width + * @param int $step + * @return $this + */ + public function derivatives($min_width, $max_width, $step = 200) { + $width = $min_width; + + // Do not upscale images. + if ($max_width > $this->get('width')) { + $max_width = $this->get('width'); + } + + while ($width <= $max_width) { + $ratio = $width / $this->get('width'); + $derivative = MediumFactory::scaledFromMedium($this, 1, $ratio); + if (is_array($derivative)) { + $this->addDerivative($derivative['file']); + } + $width += $step; + } + return $this; + } + + /** + * Add a derivative + * + * @param ImageMedium $image + */ + public function addDerivative(ImageMedium $image) { + $this->derivatives[$image->url()] = $image->get('width'); + } + /** * Parsedown element for source display mode *