Add support for derivatives

This commit is contained in:
Jelle-S
2015-09-16 17:32:21 +02:00
committed by Peter Droogmans
parent 86ca9cf01c
commit 952ed806ac

View File

@@ -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
*