Skip to content

Commit

Permalink
Make TextModifier::class able to modify animated images
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Jan 12, 2025
1 parent 6800c77 commit 9b3b408
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions src/Modifiers/TextModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace Intervention\Image\Drivers\Vips\Modifiers;

use Intervention\Image\Drivers\Vips\Core;
use Intervention\Image\Drivers\Vips\FontProcessor;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\TextModifier as GenericTextModifier;
use Jcupitt\Vips\BlendMode;
use Jcupitt\Vips\Image as VipsImage;

class TextModifier extends GenericTextModifier implements SpecializedInterface
{
Expand Down Expand Up @@ -39,15 +42,40 @@ public function apply(ImageInterface $image): ImageInterface
default => $text->similarity(['angle' => $this->font->angle()]),
};

// place line on image
$image->core()->setNative(
$image->core()->native()->composite($text, BlendMode::OVER, [
'x' => $line->position()->x(),
'y' => $line->position()->y() - $height,
])
);
if (!$image->isAnimated()) {
// place line on image
$modified = $this->placeTextOnFrame(
$text,
$image->core()->first(),
$line->position()->x(),
$line->position()->y() - $height,
)->native();
} else {
$frames = [];
foreach ($image as $frame) {
$frames[] = $this->placeTextOnFrame(
$text,
$frame,
$line->position()->x(),
$line->position()->y() - $height,
);
}

$modified = Core::replaceFrames($image->core()->native(), $frames);

Check failure on line 64 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - prefer-stable

Method Intervention\Image\Drivers\Vips\Modifiers\TextModifier::apply() throws checked exception Jcupitt\Vips\Exception but it's missing from the PHPDoc @throws tag.

Check failure on line 64 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - prefer-stable

Method Intervention\Image\Drivers\Vips\Modifiers\TextModifier::apply() throws checked exception Jcupitt\Vips\Exception but it's missing from the PHPDoc @throws tag.

Check failure on line 64 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - prefer-stable

Method Intervention\Image\Drivers\Vips\Modifiers\TextModifier::apply() throws checked exception Jcupitt\Vips\Exception but it's missing from the PHPDoc @throws tag.

Check failure on line 64 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - prefer-stable

Method Intervention\Image\Drivers\Vips\Modifiers\TextModifier::apply() throws checked exception Jcupitt\Vips\Exception but it's missing from the PHPDoc @throws tag.

Check failure on line 64 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - prefer-stable

Method Intervention\Image\Drivers\Vips\Modifiers\TextModifier::apply() throws checked exception Jcupitt\Vips\Exception but it's missing from the PHPDoc @throws tag.

Check failure on line 64 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 - prefer-stable

Method Intervention\Image\Drivers\Vips\Modifiers\TextModifier::apply() throws checked exception Jcupitt\Vips\Exception but it's missing from the PHPDoc @throws tag.

Check failure on line 64 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 - prefer-stable

Method Intervention\Image\Drivers\Vips\Modifiers\TextModifier::apply() throws checked exception Jcupitt\Vips\Exception but it's missing from the PHPDoc @throws tag.

Check failure on line 64 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - prefer-stable

Method Intervention\Image\Drivers\Vips\Modifiers\TextModifier::apply() throws checked exception Jcupitt\Vips\Exception but it's missing from the PHPDoc @throws tag.
}
}

$image->core()->setNative($modified);

Check failure on line 68 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - prefer-stable

Variable $modified might not be defined.

Check failure on line 68 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - prefer-stable

Variable $modified might not be defined.

Check failure on line 68 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - prefer-stable

Variable $modified might not be defined.

Check failure on line 68 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 - prefer-stable

Variable $modified might not be defined.

Check failure on line 68 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 - prefer-stable

Variable $modified might not be defined.

Check failure on line 68 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 - prefer-stable

Variable $modified might not be defined.

Check failure on line 68 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.4 - prefer-stable

Variable $modified might not be defined.

Check failure on line 68 in src/Modifiers/TextModifier.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 - prefer-stable

Variable $modified might not be defined.

return $image;
}

private function placeTextOnFrame(VipsImage $text, FrameInterface $frame, int $x, int $y): FrameInterface
{
$frame->setNative(
$frame->native()->composite($text, BlendMode::OVER, ['x' => $x, 'y' => $y])
);

return $frame;
}
}

0 comments on commit 9b3b408

Please sign in to comment.