Skip to content
Open
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
7 changes: 6 additions & 1 deletion src/Drivers/Imagick/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

class Driver extends AbstractDriver
{
/**
* Font processor shared by all text operations of the driver
*/
private ?FontProcessor $fontProcessor = null;

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -117,7 +122,7 @@ public function colorProcessor(ImageInterface $image): ColorProcessorInterface
*/
public function fontProcessor(): FontProcessorInterface
{
return new FontProcessor();
return $this->fontProcessor ??= new FontProcessor();
}

/**
Expand Down
25 changes: 20 additions & 5 deletions src/Drivers/Imagick/FontProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@

class FontProcessor extends AbstractFontProcessor
{
/**
* ImagickDraw objects with the font file already set, by real font file path
*
* @var array<string, ImagickDraw>
*/
private array $prototypes = [];

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -63,12 +70,20 @@ public function toImagickDraw(FontInterface $font, ?ImagickPixel $color = null):
}

try {
$draw = new ImagickDraw();
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$draw->setFont($font->filepath());
// ImagickDraw::setFont() leaks memory in each call (imagick 3.8.1 and earlier, see
// Imagick/imagick#786), so the font is set only once per file and then cloned
$key = realpath($font->filepath()) ?: $font->filepath();
if (!isset($this->prototypes[$key])) {
$prototype = new ImagickDraw();
$prototype->setStrokeAntialias(true);
$prototype->setTextAntialias(true);
$prototype->setFont($font->filepath());
$prototype->setTextAlignment(Imagick::ALIGN_LEFT);
$this->prototypes[$key] = $prototype;
}

$draw = clone $this->prototypes[$key];
$draw->setFontSize($this->nativeFontSize($font));
$draw->setTextAlignment(Imagick::ALIGN_LEFT);

if ($color instanceof ImagickPixel) {
$draw->setFillColor($color);
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/Drivers/Imagick/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function testId(): void
$this->assertEquals('Imagick', $this->driver->id());
}

public function testFontProcessorIsShared(): void
{
$this->assertSame($this->driver->fontProcessor(), $this->driver->fontProcessor());
}

public function testCreateImage(): void
{
$image = $this->driver->createImage(3, 2);
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Drivers/Imagick/FontProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Intervention\Image\Tests\Unit\Drivers\Imagick;

use ImagickPixel;
use Intervention\Image\Drivers\Imagick\FontProcessor;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Interfaces\SizeInterface;
Expand Down Expand Up @@ -72,6 +73,20 @@ public function testLeading(): void
$this->assertEquals(9, $result);
}

public function testToImagickDrawReturnsIndependentObjects(): void
{
$processor = new FontProcessor();
$first = $processor->toImagickDraw($this->testFont()->setSize(10), new ImagickPixel('#ff0000'));
$second = $processor->toImagickDraw($this->testFont()->setSize(20));

$this->assertNotSame($first, $second);
$this->assertEquals(10, $first->getFontSize());
$this->assertEquals(20, $second->getFontSize());
$this->assertEquals($first->getFont(), $second->getFont());
$this->assertEquals('srgb(255,0,0)', $first->getFillColor()->getColorAsString());
$this->assertNotEquals('srgb(255,0,0)', $second->getFillColor()->getColorAsString());
}

private function testFont(): Font
{
return new Font(Resource::create('test.ttf')->path());
Expand Down