-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from sitegeist/feature/addSetDimensionsAndPres…
…erverAspect FEATURE: add `setDimensions` to imageSourceInterface and add second `preserveAspect` argument to `setWidth` and `setHeight`
- Loading branch information
Showing
7 changed files
with
148 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
namespace Sitegeist\Kaleidoscope\EelHelpers; | ||
|
||
|
||
abstract class AbstractScalableImageSourceHelper extends AbstractImageSourceHelper implements ScalableImageSourceHelperInterface | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
protected $baseWidth = null; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $baseHeight = null; | ||
|
||
/** | ||
* @param int|null $targetWidth | ||
* @param bool $preserveAspectRatio | ||
* @return ImageSourceHelperInterface | ||
*/ | ||
public function setWidth(int $targetWidth = null, bool $preserveAspect = false) : ImageSourceHelperInterface | ||
{ | ||
$newSource = clone($this); | ||
$newSource->targetWidth = $targetWidth; | ||
if ($preserveAspect === true) { | ||
$aspect = ($this->targetWidth ?: $this->baseWidth) / ($this->targetHeight ?: $this->baseHeight); | ||
$newSource->targetHeight = round($targetWidth / $aspect); | ||
} | ||
return $newSource; | ||
} | ||
|
||
/** | ||
* @param int|null $targetHeight | ||
* @param bool $preserverAspectRatio | ||
* @return ImageSourceHelperInterface | ||
*/ | ||
public function setHeight(int $targetHeight = null, bool $preserveAspect = false) : ImageSourceHelperInterface | ||
{ | ||
$newSource = clone($this); | ||
$newSource->targetHeight = $targetHeight; | ||
if ($preserveAspect === true) { | ||
$aspect = ($this->targetWidth ?: $this->baseWidth) / ($this->targetHeight ?: $this->baseHeight); | ||
$newSource->targetWidth = round($targetHeight * $aspect); | ||
} | ||
return $newSource; | ||
} | ||
|
||
/** | ||
* @param float $factor | ||
* @return ImageSourceHelperInterface | ||
*/ | ||
public function scale(float $factor): ImageSourceHelperInterface | ||
{ | ||
$scaledHelper = clone($this); | ||
|
||
if ($this->targetWidth && $this->targetHeight) { | ||
$scaledHelper = $scaledHelper->setDimensions(round($factor * $this->targetWidth), round($factor * $this->targetHeight)); | ||
} elseif ($this->targetWidth) { | ||
$scaledHelper = $scaledHelper->setWidth(round($factor * $this->targetWidth)); | ||
} elseif ($this->targetHeight) { | ||
$scaledHelper = $scaledHelper->setHeight(round($factor * $this->targetHeight)); | ||
} else { | ||
$scaledHelper = $scaledHelper->setWidth(round($factor * $this->baseWidth)); | ||
} | ||
|
||
return $scaledHelper; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getCurrentWidth() : int | ||
{ | ||
if ($this->targetWidth) { | ||
return $this->targetWidth; | ||
} elseif ($this->targetHeight) { | ||
return round($this->targetHeight * $this->baseWidth/ $this->baseHeight); | ||
} else { | ||
return $this->baseWidth; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getCurrentHeight() : int | ||
{ | ||
if ($this->targetHeight) { | ||
return $this->targetHeight; | ||
} elseif ($this->targetWidth) { | ||
return round($this->targetWidth * $this->baseHeight / $this->baseWidth); | ||
} else { | ||
return $this->baseHeight; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters