From 952ed806ac733a66a400bd7db394ebcaf4f5f24f Mon Sep 17 00:00:00 2001 From: Jelle-S Date: Wed, 16 Sep 2015 17:32:21 +0200 Subject: [PATCH 1/2] Add support for derivatives --- .../Grav/Common/Page/Medium/ImageMedium.php | 43 +++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/system/src/Grav/Common/Page/Medium/ImageMedium.php b/system/src/Grav/Common/Page/Medium/ImageMedium.php index 17fd32151..c50cbf43f 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,52 @@ 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); } + public function derivatives($min_width, $max_width, $step = 200) { + $width = $min_width; + 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; + } + + public function addDerivative(ImageMedium $image) { + $this->derivatives[$image->url()] = $image->get('width'); + } + /** * Parsedown element for source display mode * From 9c0525f292235929804a13147cc57908c0b5843d Mon Sep 17 00:00:00 2001 From: Peter Droogmans Date: Wed, 16 Sep 2015 17:41:26 +0200 Subject: [PATCH 2/2] Add comments --- .../Grav/Common/Page/Medium/ImageMedium.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/system/src/Grav/Common/Page/Medium/ImageMedium.php b/system/src/Grav/Common/Page/Medium/ImageMedium.php index c50cbf43f..017c40e19 100644 --- a/system/src/Grav/Common/Page/Medium/ImageMedium.php +++ b/system/src/Grav/Common/Page/Medium/ImageMedium.php @@ -188,12 +188,23 @@ class ImageMedium extends Medium 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) { + + while ($width <= $max_width) { $ratio = $width / $this->get('width'); $derivative = MediumFactory::scaledFromMedium($this, 1, $ratio); if (is_array($derivative)) { @@ -204,6 +215,11 @@ class ImageMedium extends Medium return $this; } + /** + * Add a derivative + * + * @param ImageMedium $image + */ public function addDerivative(ImageMedium $image) { $this->derivatives[$image->url()] = $image->get('width'); }