Skip to content

Commit

Permalink
Implement Image::encodeByPath()
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Dec 21, 2023
1 parent 38cdd24 commit 097bfed
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Encoders/FileExtensionEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@ public function encode(ImageInterface $image): EncodedImageInterface
);
}

protected function encoderByFileExtension(string $extension): EncoderInterface
/**
* Create matching encoder for given file extension
*
* @param string $extension
* @return EncoderInterface
* @throws EncoderException
*/
protected function encoderByFileExtension(?string $extension): EncoderInterface
{
if (empty($extension)) {
throw new EncoderException('No encoder found for empty file extension.');
}

return match ($extension) {
'webp' => new WebpEncoder(),
'avif' => new AvifEncoder(),
Expand Down
33 changes: 33 additions & 0 deletions src/Encoders/FilePathEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Intervention\Image\Encoders;

use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\EncodedImageInterface;

class FilePathEncoder extends FileExtensionEncoder
{
/**
* Create new encoder instance to encode to format of file extension in given path
*
* @param null|string $path
* @return void
*/
public function __construct(protected ?string $path = null)
{
}

/**
* {@inheritdoc}
*
* @see EncoderInterface::encode()
*/
public function encode(ImageInterface $image): EncodedImageInterface
{
return $image->encode(
$this->encoderByFileExtension(
is_null($this->path) ? $image->origin()->fileExtension() : pathinfo($this->path, PATHINFO_EXTENSION)
)
);
}
}
25 changes: 25 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Intervention\Image\Encoders\AvifEncoder;
use Intervention\Image\Encoders\BmpEncoder;
use Intervention\Image\Encoders\FileExtensionEncoder;
use Intervention\Image\Encoders\FilePathEncoder;
use Intervention\Image\Encoders\GifEncoder;
use Intervention\Image\Encoders\JpegEncoder;
use Intervention\Image\Encoders\MediaTypeEncoder;
Expand Down Expand Up @@ -250,6 +251,20 @@ public function encode(EncoderInterface $encoder = new AutoEncoder()): EncodedIm
return $this->driver->resolve($encoder)->encode($this);
}

/**
* {@inheritdoc}
*
* @see ImageInterface::save()
*/
public function save(?string $path = null): ImageInterface
{
$path = is_null($path) ? $this->origin()->filePath() : $path;

$this->encodeByPath($path)->save($path);

return $this;
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -766,6 +781,16 @@ public function encodeByExtension(?string $extension = null): EncodedImageInterf
return $this->encode(new FileExtensionEncoder($extension));
}

/**
* {@inheritdoc}
*
* @see ImageInterface::encodeByPath()
*/
public function encodeByPath(?string $path = null): EncodedImageInterface
{
return $this->encode(new FilePathEncoder($path));
}

/**
* {@inheritdoc}
*
Expand Down
19 changes: 19 additions & 0 deletions src/Interfaces/ImageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ public function size(): SizeInterface;
*/
public function encode(EncoderInterface $encoder): EncodedImage;

/**
* Save the image to the specified path in the file system. If no path is
* given, the image will be saved at its original location.
*
* @param null|string $path
* @return ImageInterface
*/
public function save(?string $path = null): ImageInterface;

/**
* Apply given modifier to current image
*
Expand Down Expand Up @@ -576,6 +585,16 @@ public function encodeByMediaType(?string $type = null): EncodedImageInterface;
*/
public function encodeByExtension(?string $extension = null): EncodedImageInterface;

/**
* Encode the image into the format represented by the given extension of
* the given file path extension is given the image will be encoded to
* the format of the originally read image.
*
* @param null|string $path
* @return EncodedImageInterface
*/
public function encodeByPath(?string $path = null): EncodedImageInterface;

/**
* Encode image to JPEG format
*
Expand Down
10 changes: 10 additions & 0 deletions src/Origin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public function mimetype(): string
return $this->mediaType();
}

/**
* Return file path of origin
*
* @return null|string
*/
public function filePath(): ?string
{
return $this->filePath;
}

/**
* Set file path for origin
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Drivers/Gd/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,27 @@ public function testEncodeByExtension(): void
$this->assertMediaType('image/png', (string) $result);
}

public function testEncodeByPath(): void
{
$result = $this->readTestImage('blue.gif')->encodeByPath();
$this->assertInstanceOf(EncodedImage::class, $result);
$this->assertMediaType('image/gif', (string) $result);

$result = $this->readTestImage('blue.gif')->encodeByPath('foo/bar.png');
$this->assertInstanceOf(EncodedImage::class, $result);
$this->assertMediaType('image/png', (string) $result);
}

public function testSaveAsFormat(): void
{
$path = __DIR__ . '/tmp.png';
$result = $this->readTestImage('blue.gif')->save($path);
$this->assertInstanceOf(Image::class, $result);
$this->assertFileExists($path);
$this->assertMediaType('image/png', file_get_contents($path));
unlink($path);
}

public function testWidthHeightSize(): void
{
$this->assertEquals(3, $this->image->width());
Expand Down
21 changes: 21 additions & 0 deletions tests/Drivers/Imagick/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ public function testEncodeByExtension(): void
$this->assertMediaType('image/png', (string) $result);
}

public function testEncodeByPath(): void
{
$result = $this->readTestImage('blue.gif')->encodeByPath();
$this->assertInstanceOf(EncodedImage::class, $result);
$this->assertMediaType('image/gif', (string) $result);

$result = $this->readTestImage('blue.gif')->encodeByPath('foo/bar.png');
$this->assertInstanceOf(EncodedImage::class, $result);
$this->assertMediaType('image/png', (string) $result);
}

public function testSaveAsFormat(): void
{
$path = __DIR__ . '/tmp.png';
$result = $this->readTestImage('blue.gif')->save($path);
$this->assertInstanceOf(Image::class, $result);
$this->assertFileExists($path);
$this->assertMediaType('image/png', file_get_contents($path));
unlink($path);
}

public function testWidthHeightSize(): void
{
$this->assertEquals(20, $this->image->width());
Expand Down

0 comments on commit 097bfed

Please sign in to comment.