Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate UndefinedBox and RelativeBox #14

Merged
merged 3 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
* Don’t throw exceptions for LIBXML warnings. [#13]
* Fix bug with viewBox computation.
* Use NotSupportedException if available. [#15]
* Deprecate UndefinedBox and RelativeBox, replace them with SvgBox. [#14]

## [0.2.2] (2019-01-27)

Expand Down Expand Up @@ -63,6 +64,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
[0.1.0]: https://github.com/contao/imagine-svg/commits/0.1.0

[#15]: https://github.com/contao/imagine-svg/issues/15
[#14]: https://github.com/contao/imagine-svg/issues/14
[#13]: https://github.com/contao/imagine-svg/issues/13
[#10]: https://github.com/contao/imagine-svg/issues/10
[#9]: https://github.com/contao/imagine-svg/issues/9
Expand Down
17 changes: 9 additions & 8 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Imagine\Exception\OutOfBoundsException;
use Imagine\Exception\RuntimeException;
use Imagine\Image\AbstractImage;
use Imagine\Image\Box;
use Imagine\Image\BoxInterface;
use Imagine\Image\Fill\FillInterface;
use Imagine\Image\ImageInterface;
Expand Down Expand Up @@ -92,7 +91,7 @@ public function crop(PointInterface $start, BoxInterface $size)
}

if (
!($currentSize instanceof RelativeBox)
SvgBox::TYPE_ASPECT_RATIO !== $currentSize->getType()
&& 0 === $start->getX()
&& 0 === $start->getY()
&& $size->getWidth() === $currentSize->getWidth()
Expand Down Expand Up @@ -139,7 +138,7 @@ public function resize(BoxInterface $size, $filter = ImageInterface::FILTER_UNDE
$currentSize = $this->getSize();

if (
!($currentSize instanceof RelativeBox)
SvgBox::TYPE_ASPECT_RATIO !== $currentSize->getType()
&& $size->getWidth() === $currentSize->getWidth()
&& $size->getHeight() === $currentSize->getHeight()
) {
Expand Down Expand Up @@ -285,6 +284,8 @@ public function effects()

/**
* {@inheritdoc}
*
* @return SvgBox
*/
public function getSize()
{
Expand All @@ -295,7 +296,7 @@ public function getSize()

// Absolute dimensions
if ($width && $height) {
return new Box($width, $height);
return SvgBox::createTypeAbsolute($width, $height);
}

$viewBox = preg_split('/[\s,]+/', $svg->getAttribute('viewBox') ?: '');
Expand All @@ -304,17 +305,17 @@ public function getSize()

// Missing width/height and viewBox
if ($viewBoxWidth <= 0 || $viewBoxHeight <= 0) {
return new UndefinedBox();
return SvgBox::createTypeNone();
}

// Fixed width and viewBox
if ($width) {
return new Box($width, $width / $viewBoxWidth * $viewBoxHeight);
return SvgBox::createTypeAbsolute($width, $width / $viewBoxWidth * $viewBoxHeight);
}

// Fixed height and viewBox
if ($height) {
return new Box($height / $viewBoxHeight * $viewBoxWidth, $height);
return SvgBox::createTypeAbsolute($height / $viewBoxHeight * $viewBoxWidth, $height);
}

// Normalize floating point values
Expand All @@ -327,7 +328,7 @@ public function getSize()
}

// Missing width/height, returning relative dimensions from viewBox
return new RelativeBox(round($viewBoxWidth), round($viewBoxHeight));
return SvgBox::createTypeAspectRatio(round($viewBoxWidth), round($viewBoxHeight));
}

/**
Expand Down
64 changes: 9 additions & 55 deletions src/RelativeBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,26 @@

namespace Contao\ImagineSvg;

use Imagine\Image\Box;
use Imagine\Image\BoxInterface;
use Imagine\Image\PointInterface;

class RelativeBox implements RelativeBoxInterface
/**
* @deprecated use SvgBox::createTypeAspectRatio() instead
*/
class RelativeBox extends SvgBox implements RelativeBoxInterface
{
/**
* @var Box
*/
private $box;

/**
* @param int $width
* @param int $height
*/
public function __construct($width, $height)
{
$this->box = new Box($width, $height);
}

/**
* {@inheritdoc}
*/
public function __toString()
{
return sprintf('%dx%d', $this->box->getWidth(), $this->box->getHeight());
}

/**
* {@inheritdoc}
*/
public function getWidth()
{
return $this->box->getWidth();
}

/**
* {@inheritdoc}
*/
public function getHeight()
{
return $this->box->getHeight();
parent::__construct($width, $height, self::TYPE_ASPECT_RATIO);
}

/**
* {@inheritdoc}
*/
public function scale($ratio)
{
$box = $this->box->scale($ratio);
$box = parent::scale($ratio);

return new self($box->getWidth(), $box->getHeight());
}
Expand All @@ -69,33 +39,17 @@ public function scale($ratio)
*/
public function increase($size)
{
$box = $this->box->increase($size);
$box = parent::increase($size);

return new self($box->getWidth(), $box->getHeight());
}

/**
* {@inheritdoc}
*/
public function contains(BoxInterface $box, PointInterface $start = null)
{
return $this->box->contains($box, $start);
}

/**
* {@inheritdoc}
*/
public function square()
{
return $this->box->square();
}

/**
* {@inheritdoc}
*/
public function widen($width)
{
$box = $this->box->widen($width);
$box = parent::widen($width);

return new self($box->getWidth(), $box->getHeight());
}
Expand All @@ -105,7 +59,7 @@ public function widen($width)
*/
public function heighten($height)
{
$box = $this->box->heighten($height);
$box = parent::heighten($height);

return new self($box->getWidth(), $box->getHeight());
}
Expand Down
3 changes: 3 additions & 0 deletions src/RelativeBoxInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

use Imagine\Image\BoxInterface;

/**
* @deprecated use SvgBox::getType() === SvgBox::TYPE_ASPECT_RATIO instead
*/
interface RelativeBoxInterface extends BoxInterface
{
}
177 changes: 177 additions & 0 deletions src/SvgBox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\ImagineSvg;

use Imagine\Image\Box;
use Imagine\Image\BoxInterface;
use Imagine\Image\PointInterface;

class SvgBox implements BoxInterface
{
const TYPE_ABSOLUTE = 0;
const TYPE_ASPECT_RATIO = 1;
const TYPE_NONE = 2;

/**
* @var int
*/
private $type;

/**
* @var Box
*/
private $box;

/**
* @param int $width
* @param int $height
* @param int $type
*/
public function __construct($width, $height, $type = self::TYPE_ABSOLUTE)
{
if (!\in_array($type, [self::TYPE_ABSOLUTE, self::TYPE_ASPECT_RATIO, self::TYPE_NONE], true)) {
throw new \InvalidArgumentException(sprintf('Invalid SvgBox type "%s", must be one of the %s::TYPE_* constants.', $type, __CLASS__));
}

$this->type = $type;

if (self::TYPE_NONE === $type) {
$this->box = new Box(300, 150);
} else {
$this->box = new Box($width, $height);
}
}

/**
* {@inheritdoc}
*/
public function __toString()
{
if (self::TYPE_NONE === $this->type) {
return 'undefined';
}
if (self::TYPE_ASPECT_RATIO === $this->type) {
return sprintf('%dx%d', $this->box->getWidth(), $this->box->getHeight());
}

return (string) $this->box;
}

/**
* @param int $width
* @param int $height
*
* @return SvgBox
*/
public static function createTypeAbsolute($width, $height)
{
return new self($width, $height);
}

/**
* @param int $width
* @param int $height
*
* @return SvgBox
*/
public static function createTypeAspectRatio($width, $height)
{
return new RelativeBox($width, $height);
}

/**
* @return SvgBox
*/
public static function createTypeNone()
{
return new UndefinedBox();
}

/**
* @return int one of the SvgBox::TYPE_* constants
*/
public function getType()
{
return $this->type;
}

/**
* {@inheritdoc}
*/
public function getWidth()
{
return $this->box->getWidth();
}

/**
* {@inheritdoc}
*/
public function getHeight()
{
return $this->box->getHeight();
}

/**
* {@inheritdoc}
*/
public function scale($ratio)
{
$box = $this->box->scale($ratio);

return new self($box->getWidth(), $box->getHeight(), $this->type);
}

/**
* {@inheritdoc}
*/
public function increase($size)
{
$box = $this->box->increase($size);

return new self($box->getWidth(), $box->getHeight(), $this->type);
}

/**
* {@inheritdoc}
*/
public function contains(BoxInterface $box, PointInterface $start = null)
{
return $this->box->contains($box, $start);
}

/**
* {@inheritdoc}
*/
public function square()
{
return $this->box->square();
}

/**
* {@inheritdoc}
*/
public function widen($width)
{
$box = $this->box->widen($width);

return new self($box->getWidth(), $box->getHeight(), $this->type);
}

/**
* {@inheritdoc}
*/
public function heighten($height)
{
$box = $this->box->heighten($height);

return new self($box->getWidth(), $box->getHeight(), $this->type);
}
}
Loading