mirror of
https://github.com/Intervention/image.git
synced 2025-01-17 20:28:21 +01:00
Refactor checking for driver objects
This commit is contained in:
parent
50f7ebce78
commit
25572c0ef9
@ -4,11 +4,13 @@ namespace Intervention\Image\Drivers\Abstract;
|
||||
|
||||
use Intervention\Image\Interfaces\ColorInterface;
|
||||
use Intervention\Image\Interfaces\FontInterface;
|
||||
use Intervention\Image\Traits\CanCheckType;
|
||||
use Intervention\Image\Traits\CanHandleInput;
|
||||
|
||||
abstract class AbstractFont implements FontInterface
|
||||
{
|
||||
use CanHandleInput;
|
||||
use CanCheckType;
|
||||
|
||||
protected $size = 12;
|
||||
protected $angle = 0;
|
||||
|
@ -5,10 +5,13 @@ namespace Intervention\Image\Drivers\Abstract;
|
||||
use Intervention\Image\Geometry\Point;
|
||||
use Intervention\Image\Interfaces\FontInterface;
|
||||
use Intervention\Image\Interfaces\ModifierInterface;
|
||||
use Intervention\Image\Traits\CanCheckType;
|
||||
use Intervention\Image\Typography\TextBlock;
|
||||
|
||||
abstract class AbstractTextWriter implements ModifierInterface
|
||||
{
|
||||
use CanCheckType;
|
||||
|
||||
public function __construct(
|
||||
protected Point $position,
|
||||
protected FontInterface $font,
|
||||
|
@ -11,11 +11,13 @@ use Intervention\Image\Geometry\Rectangle;
|
||||
use Intervention\Image\Interfaces\ColorInterface;
|
||||
use Intervention\Image\Interfaces\DrawableInterface;
|
||||
use Intervention\Image\Interfaces\PointInterface;
|
||||
use Intervention\Image\Traits\CanCheckType;
|
||||
use Intervention\Image\Traits\CanHandleInput;
|
||||
|
||||
class AbstractDrawModifier
|
||||
{
|
||||
use CanHandleInput;
|
||||
use CanCheckType;
|
||||
|
||||
public function __construct(
|
||||
protected PointInterface $position,
|
||||
|
@ -5,9 +5,12 @@ namespace Intervention\Image\Drivers\Abstract\Modifiers;
|
||||
use Intervention\Image\Geometry\Rectangle;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
use Intervention\Image\Interfaces\SizeInterface;
|
||||
use Intervention\Image\Traits\CanCheckType;
|
||||
|
||||
abstract class AbstractPadModifier
|
||||
{
|
||||
use CanCheckType;
|
||||
|
||||
public function __construct(
|
||||
protected int $width,
|
||||
protected int $height,
|
||||
|
@ -5,11 +5,13 @@ namespace Intervention\Image\Drivers\Abstract\Modifiers;
|
||||
use Intervention\Image\Exceptions\DecoderException;
|
||||
use Intervention\Image\Exceptions\TypeException;
|
||||
use Intervention\Image\Interfaces\ColorInterface;
|
||||
use Intervention\Image\Traits\CanCheckType;
|
||||
use Intervention\Image\Traits\CanHandleInput;
|
||||
|
||||
abstract class AbstractRotateModifier
|
||||
{
|
||||
use CanHandleInput;
|
||||
use CanCheckType;
|
||||
|
||||
public function __construct(protected float $angle, protected $background)
|
||||
{
|
||||
|
@ -4,8 +4,6 @@ namespace Intervention\Image\Drivers\Gd\Modifiers;
|
||||
|
||||
use Intervention\Image\Drivers\Abstract\AbstractTextWriter;
|
||||
use Intervention\Image\Drivers\Gd\Font;
|
||||
use Intervention\Image\Exceptions\FontException;
|
||||
use Intervention\Image\Interfaces\FontInterface;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
|
||||
class TextWriter extends AbstractTextWriter
|
||||
@ -13,30 +11,27 @@ class TextWriter extends AbstractTextWriter
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
$lines = $this->getAlignedTextBlock();
|
||||
$font = $this->failIfNotClass($this->getFont(), Font::class);
|
||||
|
||||
foreach ($image as $frame) {
|
||||
if ($this->font->hasFilename()) {
|
||||
foreach ($lines as $line) {
|
||||
imagettftext(
|
||||
$frame->getCore(),
|
||||
$this->getFont()->getSize(),
|
||||
$this->getFont()->getAngle() * (-1),
|
||||
$font->getSize(),
|
||||
$font->getAngle() * (-1),
|
||||
$line->getPosition()->getX(),
|
||||
$line->getPosition()->getY(),
|
||||
$this->getFont()->getColor()->toInt(),
|
||||
$this->getFont()->getFilename(),
|
||||
$font->getColor()->toInt(),
|
||||
$font->getFilename(),
|
||||
$line
|
||||
);
|
||||
}
|
||||
|
||||
// debug
|
||||
// $lines = new TextBlock($this->text);
|
||||
// $box = $lines->getBoundingBox($this->font, $this->position);
|
||||
// imagepolygon($frame->getCore(), $box->toArray(), 0);
|
||||
} else {
|
||||
foreach ($lines as $line) {
|
||||
imagestring(
|
||||
$frame->getCore(),
|
||||
$this->getFont()->getGdFont(),
|
||||
$font->getGdFont(),
|
||||
$line->getPosition()->getX(),
|
||||
$line->getPosition()->getY(),
|
||||
$line,
|
||||
@ -48,12 +43,4 @@ class TextWriter extends AbstractTextWriter
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
protected function getFont(): FontInterface
|
||||
{
|
||||
if (!is_a($this->font, Font::class)) {
|
||||
throw new FontException('Font is not compatible to current driver.');
|
||||
}
|
||||
return $this->font;
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,6 @@ use Intervention\Image\Drivers\Abstract\AbstractFont;
|
||||
use Intervention\Image\Exceptions\FontException;
|
||||
use Intervention\Image\Geometry\Polygon;
|
||||
use Intervention\Image\Geometry\Rectangle;
|
||||
use Intervention\Image\Interfaces\ColorInterface;
|
||||
|
||||
class Font extends AbstractFont
|
||||
{
|
||||
@ -18,28 +17,19 @@ class Font extends AbstractFont
|
||||
throw new FontException('No font file specified.');
|
||||
}
|
||||
|
||||
$color = $this->failIfNotClass($this->getColor(), Color::class);
|
||||
|
||||
$draw = new ImagickDraw();
|
||||
$draw->setStrokeAntialias(true);
|
||||
$draw->setTextAntialias(true);
|
||||
$draw->setFont($this->getFilename());
|
||||
$draw->setFontSize($this->getSize());
|
||||
$draw->setFillColor($this->getColor()->getPixel());
|
||||
$draw->setFillColor($color->getPixel());
|
||||
$draw->setTextAlignment(Imagick::ALIGN_LEFT);
|
||||
|
||||
return $draw;
|
||||
}
|
||||
|
||||
public function getColor(): ?ColorInterface
|
||||
{
|
||||
$color = parent::getColor();
|
||||
|
||||
if (!is_a($color, Color::class)) {
|
||||
throw new FontException('Font is not compatible to current driver.');
|
||||
}
|
||||
|
||||
return $color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate box size of current font
|
||||
*
|
||||
|
@ -5,8 +5,6 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
|
||||
use ImagickDraw;
|
||||
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
|
||||
use Intervention\Image\Drivers\Imagick\Color;
|
||||
use Intervention\Image\Exceptions\DecoderException;
|
||||
use Intervention\Image\Interfaces\ColorInterface;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
use Intervention\Image\Interfaces\ModifierInterface;
|
||||
|
||||
@ -14,13 +12,16 @@ class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterf
|
||||
{
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
return $image->eachFrame(function ($frame) {
|
||||
$background_color = $this->failIfNotClass($this->getBackgroundColor(), Color::class);
|
||||
$border_color = $this->failIfNotClass($this->getBorderColor(), Color::class);
|
||||
|
||||
return $image->eachFrame(function ($frame) use ($background_color, $border_color) {
|
||||
$drawing = new ImagickDraw();
|
||||
$drawing->setFillColor($this->getBackgroundColor()->getPixel());
|
||||
$drawing->setFillColor($background_color->getPixel());
|
||||
|
||||
if ($this->ellipse()->hasBorder()) {
|
||||
$drawing->setStrokeWidth($this->ellipse()->getBorderSize());
|
||||
$drawing->setStrokeColor($this->getBorderColor()->getPixel());
|
||||
$drawing->setStrokeColor($border_color->getPixel());
|
||||
}
|
||||
|
||||
$drawing->ellipse(
|
||||
@ -35,24 +36,4 @@ class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterf
|
||||
$frame->getCore()->drawImage($drawing);
|
||||
});
|
||||
}
|
||||
|
||||
protected function getBackgroundColor(): ColorInterface
|
||||
{
|
||||
$color = parent::getBackgroundColor();
|
||||
if (!is_a($color, Color::class)) {
|
||||
throw new DecoderException('Unable to decode background color.');
|
||||
}
|
||||
|
||||
return $color;
|
||||
}
|
||||
|
||||
protected function getBorderColor(): ColorInterface
|
||||
{
|
||||
$color = parent::getBorderColor();
|
||||
if (!is_a($color, Color::class)) {
|
||||
throw new DecoderException('Unable to decode border color.');
|
||||
}
|
||||
|
||||
return $color;
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
|
||||
|
||||
use ImagickDraw;
|
||||
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
|
||||
use Intervention\Image\Drivers\Imagick\Color;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
use Intervention\Image\Interfaces\ModifierInterface;
|
||||
|
||||
@ -12,7 +13,8 @@ class DrawLineModifier extends AbstractDrawModifier implements ModifierInterface
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
$drawing = new ImagickDraw();
|
||||
$drawing->setStrokeColor($this->getBackgroundColor()->getPixel());
|
||||
$color = $this->failIfNotClass($this->getBackgroundColor(), Color::class);
|
||||
$drawing->setStrokeColor($color->getPixel());
|
||||
$drawing->setStrokeWidth($this->line()->getWidth());
|
||||
$drawing->line(
|
||||
$this->line()->getStart()->getX(),
|
||||
|
@ -4,15 +4,16 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
|
||||
|
||||
use ImagickDraw;
|
||||
use Intervention\Image\Drivers\Imagick\Color;
|
||||
use Intervention\Image\Exceptions\TypeException;
|
||||
use Intervention\Image\Geometry\Point;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
use Intervention\Image\Interfaces\ModifierInterface;
|
||||
use Intervention\Image\Traits\CanCheckType;
|
||||
use Intervention\Image\Traits\CanHandleInput;
|
||||
|
||||
class DrawPixelModifier implements ModifierInterface
|
||||
{
|
||||
use CanHandleInput;
|
||||
use CanCheckType;
|
||||
|
||||
public function __construct(protected Point $position, protected mixed $color)
|
||||
{
|
||||
@ -21,7 +22,11 @@ class DrawPixelModifier implements ModifierInterface
|
||||
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
$color = $this->decodeColor();
|
||||
$color = $this->failIfNotClass(
|
||||
$this->handleInput($this->color),
|
||||
Color::class,
|
||||
);
|
||||
|
||||
$pixel = new ImagickDraw();
|
||||
$pixel->setFillColor($color->getPixel());
|
||||
$pixel->point($this->position->getX(), $this->position->getY());
|
||||
@ -30,15 +35,4 @@ class DrawPixelModifier implements ModifierInterface
|
||||
$frame->getCore()->drawImage($pixel);
|
||||
});
|
||||
}
|
||||
|
||||
private function decodeColor(): Color
|
||||
{
|
||||
$color = $this->handleInput($this->color);
|
||||
|
||||
if (!is_a($color, Color::class)) {
|
||||
throw new TypeException('Color is not compatible to current driver.');
|
||||
}
|
||||
|
||||
return $color;
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
|
||||
use ImagickDraw;
|
||||
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
|
||||
use Intervention\Image\Drivers\Imagick\Color;
|
||||
use Intervention\Image\Exceptions\TypeException;
|
||||
use Intervention\Image\Interfaces\DrawableInterface;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
use Intervention\Image\Interfaces\ModifierInterface;
|
||||
@ -21,12 +20,15 @@ class DrawPolygonModifier extends AbstractDrawModifier implements ModifierInterf
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
$drawing = new ImagickDraw();
|
||||
$background_color = $this->failIfNotClass($this->getBackgroundColor(), Color::class);
|
||||
$border_color = $this->failIfNotClass($this->getBorderColor(), Color::class);
|
||||
|
||||
if ($this->polygon()->hasBackgroundColor()) {
|
||||
$drawing->setFillColor($this->getBackgroundColor()->getPixel());
|
||||
$drawing->setFillColor($background_color->getPixel());
|
||||
}
|
||||
|
||||
if ($this->polygon()->hasBorder()) {
|
||||
$drawing->setStrokeColor($this->getBorderColor()->getPixel());
|
||||
$drawing->setStrokeColor($border_color->getPixel());
|
||||
$drawing->setStrokeWidth($this->polygon()->getBorderSize());
|
||||
}
|
||||
|
||||
@ -46,26 +48,4 @@ class DrawPolygonModifier extends AbstractDrawModifier implements ModifierInterf
|
||||
|
||||
return $points;
|
||||
}
|
||||
|
||||
protected function getBackgroundColor(): ?Color
|
||||
{
|
||||
$color = parent::getBackgroundColor();
|
||||
|
||||
if (!is_a($color, Color::class)) {
|
||||
throw new TypeException('Color is not compatible to current driver.');
|
||||
}
|
||||
|
||||
return $color;
|
||||
}
|
||||
|
||||
protected function getBorderColor(): ?Color
|
||||
{
|
||||
$color = parent::getBorderColor();
|
||||
|
||||
if (!is_a($color, Color::class)) {
|
||||
throw new TypeException('Color is not compatible to current driver.');
|
||||
}
|
||||
|
||||
return $color;
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
|
||||
use ImagickDraw;
|
||||
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
|
||||
use Intervention\Image\Drivers\Imagick\Color;
|
||||
use Intervention\Image\Exceptions\DecoderException;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
use Intervention\Image\Interfaces\ModifierInterface;
|
||||
|
||||
@ -13,11 +12,14 @@ class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInte
|
||||
{
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
// setup rectangle
|
||||
// setup
|
||||
$drawing = new ImagickDraw();
|
||||
$drawing->setFillColor($this->getBackgroundColor()->getPixel());
|
||||
$background_color = $this->failIfNotClass($this->getBackgroundColor(), Color::class);
|
||||
$border_color = $this->failIfNotClass($this->getBorderColor(), Color::class);
|
||||
|
||||
$drawing->setFillColor($background_color->getPixel());
|
||||
if ($this->rectangle()->hasBorder()) {
|
||||
$drawing->setStrokeColor($this->getBorderColor()->getPixel());
|
||||
$drawing->setStrokeColor($border_color->getPixel());
|
||||
$drawing->setStrokeWidth($this->rectangle()->getBorderSize());
|
||||
}
|
||||
|
||||
@ -35,24 +37,4 @@ class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInte
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
protected function getBackgroundColor(): Color
|
||||
{
|
||||
$color = parent::getBackgroundColor();
|
||||
if (!is_a($color, Color::class)) {
|
||||
throw new DecoderException('Unable to decode background color.');
|
||||
}
|
||||
|
||||
return $color;
|
||||
}
|
||||
|
||||
protected function getBorderColor(): Color
|
||||
{
|
||||
$color = parent::getBorderColor();
|
||||
if (!is_a($color, Color::class)) {
|
||||
throw new DecoderException('Unable to decode border color.');
|
||||
}
|
||||
|
||||
return $color;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ use Imagick;
|
||||
use ImagickDraw;
|
||||
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractPadModifier;
|
||||
use Intervention\Image\Drivers\Imagick\Color;
|
||||
use Intervention\Image\Exceptions\DecoderException;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
use Intervention\Image\Interfaces\ModifierInterface;
|
||||
use Intervention\Image\Interfaces\SizeInterface;
|
||||
@ -22,11 +21,7 @@ class PadModifier extends AbstractPadModifier implements ModifierInterface
|
||||
{
|
||||
$resize = $this->getResizeSize($image);
|
||||
$crop = $this->getCropSize($image);
|
||||
$background = $this->handleInput($this->background);
|
||||
|
||||
if (!is_a($background, Color::class)) {
|
||||
throw new DecoderException('Unable to decode backgroud color.');
|
||||
}
|
||||
$background = $this->failIfNotClass($this->handleInput($this->background), Color::class);
|
||||
|
||||
foreach ($image as $frame) {
|
||||
// resize current core
|
||||
|
@ -4,7 +4,6 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
|
||||
|
||||
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractRotateModifier;
|
||||
use Intervention\Image\Drivers\Imagick\Color;
|
||||
use Intervention\Image\Exceptions\DecoderException;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
use Intervention\Image\Interfaces\ModifierInterface;
|
||||
|
||||
@ -12,10 +11,7 @@ class RotateModifier extends AbstractRotateModifier implements ModifierInterface
|
||||
{
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
$background = $this->backgroundColor();
|
||||
if (!is_a($background, Color::class)) {
|
||||
throw new DecoderException('Unable to decode given background color.');
|
||||
}
|
||||
$background = $this->failIfNotClass($this->backgroundColor(), Color::class);
|
||||
|
||||
foreach ($image as $frame) {
|
||||
$frame->getCore()->rotateImage(
|
||||
|
@ -4,7 +4,6 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
|
||||
|
||||
use Intervention\Image\Drivers\Abstract\AbstractTextWriter;
|
||||
use Intervention\Image\Drivers\Imagick\Font;
|
||||
use Intervention\Image\Exceptions\FontException;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
|
||||
class TextWriter extends AbstractTextWriter
|
||||
@ -12,13 +11,15 @@ class TextWriter extends AbstractTextWriter
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
$lines = $this->getAlignedTextBlock();
|
||||
$font = $this->failIfNotClass($this->getFont(), Font::class);
|
||||
|
||||
foreach ($image as $frame) {
|
||||
foreach ($lines as $line) {
|
||||
$frame->getCore()->annotateImage(
|
||||
$this->getFont()->toImagickDraw(),
|
||||
$font->toImagickDraw(),
|
||||
$line->getPosition()->getX(),
|
||||
$line->getPosition()->getY(),
|
||||
$this->getFont()->getAngle(),
|
||||
$font->getAngle(),
|
||||
$line
|
||||
);
|
||||
}
|
||||
@ -26,12 +27,4 @@ class TextWriter extends AbstractTextWriter
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
protected function getFont(): Font
|
||||
{
|
||||
if (!is_a($this->font, Font::class)) {
|
||||
throw new FontException('Font is not compatible to current driver.');
|
||||
}
|
||||
return $this->font;
|
||||
}
|
||||
}
|
||||
|
17
src/Traits/CanCheckType.php
Normal file
17
src/Traits/CanCheckType.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Traits;
|
||||
|
||||
use Intervention\Image\Exceptions\TypeException;
|
||||
|
||||
trait CanCheckType
|
||||
{
|
||||
public function failIfNotClass(mixed $input, string $classname)
|
||||
{
|
||||
if (!is_object($input) || get_class($input) != $classname) {
|
||||
throw new TypeException('Given input is not instance of ' . $classname);
|
||||
}
|
||||
|
||||
return $input;
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace Intervention\Image\Traits;
|
||||
|
||||
use Intervention\Image\Exceptions\DecoderException;
|
||||
use Intervention\Image\Interfaces\ColorInterface;
|
||||
use Intervention\Image\Interfaces\ImageInterface;
|
||||
|
||||
@ -9,8 +10,14 @@ trait CanHandleInput
|
||||
{
|
||||
use CanResolveDriverClass;
|
||||
|
||||
public function handleInput($input): ImageInterface|ColorInterface
|
||||
public function handleInput($input, ?string $check_result_against_classname = null): ImageInterface|ColorInterface
|
||||
{
|
||||
return $this->resolveDriverClass('InputHandler')->handle($input);
|
||||
$result = $this->resolveDriverClass('InputHandler')->handle($input);
|
||||
|
||||
if (!is_null($check_result_against_classname) && get_class($result) != $check_result_against_classname) {
|
||||
throw new DecoderException('Decoded result is not an instance of ' . $check_result_against_classname);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ final class AbstractRotateModifierTest extends TestCase
|
||||
return parent::backgroundColor();
|
||||
}
|
||||
|
||||
public function handleInput($input): ImageInterface|ColorInterface
|
||||
public function handleInput($input, ?string $check_result_against_classname = null): ImageInterface|ColorInterface
|
||||
{
|
||||
if ($this->background === 'bad value') {
|
||||
throw new DecoderException();
|
||||
|
Loading…
x
Reference in New Issue
Block a user