Skip to content

Commit

Permalink
Merge pull request #33 from mirko-pagliai/develop
Browse files Browse the repository at this point in the history
minor code, test, and documentation improvements
  • Loading branch information
mirko-pagliai committed Mar 13, 2023
2 parents 77a8c8c + c80cd6a commit e7830bf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
31 changes: 17 additions & 14 deletions src/ThumbCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,34 @@ public function __construct(string $path)
* Internal method to get default options for the `save()` method
* @param array<string, mixed> $options Passed options
* @param string $path Path to use
* @return array<string, mixed> Passed options with default options
* @return array{format: string, quality: int, target: false|string} Passed options with default options
*/
protected function getDefaultSaveOptions(array $options = [], string $path = ''): array
protected function getDefaultSaveOptions(array $options, string $path = ''): array
{
/** @var array{format: string, quality: int, target: false|string} $options */
$options += [
'format' => $this->Filesystem->getExtension($path ?: $this->path) ?: '',
'quality' => 90,
'target' => false,
];

//Fixes some formats
$options['format'] = preg_replace(['/^jpeg$/', '/^tif$/'], ['jpg', 'tiff'], $options['format']);
$options['format'] = preg_replace(['/^jpeg$/', '/^tif$/'], ['jpg', 'tiff'], $options['format']) ?: $options['format'];

return $options;
}

/**
* Gets an `Image` instance
* @return \Intervention\Image\Image
* @throws \Thumber\Exception\NotReadableImageException|\ErrorException
* @throws \ErrorException
* @throws \Thumber\Exception\NotReadableImageException
* @throws \Thumber\Exception\UnsupportedImageTypeException
*/
protected function getImageInstance(): Image
{
try {
$imageInstance = $this->ImageManager->make($this->path);
$ImageInstance = $this->ImageManager->make($this->path);
} catch (NotReadableException $e) {
if (str_starts_with($e->getMessage(), 'Unsupported image type')) {
$type = mime_content_type($this->path) ?: null;
Expand All @@ -123,7 +126,7 @@ protected function getImageInstance(): Image
throw new NotReadableImageException($path ? 'Unable to read image from `' . $path . '`' : 'Unable to read image from file');
}

return $imageInstance;
return $ImageInstance;
}

/**
Expand All @@ -143,7 +146,7 @@ public function crop(int $width, int $height = 0, array $options = []): ThumbCre
Exceptionist::isPositive($width, sprintf('You have to set at least the width for the `%s()` method', __METHOD__));

$this->arguments[] = [__FUNCTION__, $width, $height, $options];
$this->callbacks[] = fn(Image $imageInstance): Image => $imageInstance->crop(
$this->callbacks[] = fn(Image $ImageInstance): Image => $ImageInstance->crop(
$width,
$height,
Exceptionist::isInt($options['x'], 'The `x` option must be an integer'),
Expand All @@ -170,7 +173,7 @@ public function fit(int $width = 0, int $height = 0, array $options = []): Thumb
$options += ['position' => 'center', 'upsize' => true];

$this->arguments[] = [__FUNCTION__, $width, $height, $options];
$this->callbacks[] = fn(Image $imageInstance): Image => $imageInstance
$this->callbacks[] = fn(Image $ImageInstance): Image => $ImageInstance
->fit($width, $height, function (Constraint $constraint) use ($options): void {
if ($options['upsize']) {
$constraint->upsize();
Expand All @@ -195,7 +198,7 @@ public function resize(int $width, int $height = 0, array $options = []): ThumbC
Exceptionist::isPositive($width, sprintf('You have to set at least the width for the `%s()` method', __METHOD__));

$this->arguments[] = [__FUNCTION__, $width, $height, $options];
$this->callbacks[] = fn(Image $imageInstance): Image => $imageInstance
$this->callbacks[] = fn(Image $ImageInstance): Image => $ImageInstance
->resize($width, $height, function (Constraint $constraint) use ($options): void {
if ($options['aspectRatio']) {
$constraint->aspectRatio();
Expand Down Expand Up @@ -225,7 +228,7 @@ public function resizeCanvas(int $width, int $height = 0, array $options = []):
Exceptionist::isPositive($width, sprintf('You have to set at least the width for the `%s()` method', __METHOD__));

$this->arguments[] = [__FUNCTION__, $width, $height, $options];
$this->callbacks[] = fn(Image $imageInstance): Image => $imageInstance
$this->callbacks[] = fn(Image $ImageInstance): Image => $ImageInstance
->resizeCanvas($width, $height, $options['anchor'], $options['relative'], $options['bgcolor']);

return $this;
Expand Down Expand Up @@ -256,15 +259,15 @@ public function save(array $options = []): string
//Creates the thumbnail, if this does not exist
$target = $this->Filesystem->makePathAbsolute($target, THUMBER_TARGET);
if (!file_exists($target)) {
$imageInstance = $this->getImageInstance();
$ImageInstance = $this->getImageInstance();

//Calls each callback
foreach ($this->callbacks as $callback) {
call_user_func($callback, $imageInstance);
call_user_func($callback, $ImageInstance);
}

$content = $imageInstance->encode($format, $options['quality']);
$imageInstance->destroy();
$content = $ImageInstance->encode($format, $options['quality']);
$ImageInstance->destroy();
try {
$this->Filesystem->dumpFile($target, (string)$content);
} catch (IOException $e) {
Expand Down
23 changes: 11 additions & 12 deletions src/ThumbManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Thumber;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Tools\Exceptionist;
use Tools\Filesystem;

Expand All @@ -31,15 +32,15 @@ class ThumbManager

/**
* Internal method to clear thumbnails
* @param array<string> $filenames Filenames
* @param string[] $filenames Filenames
* @return int Number of thumbnails deleted
* @throws \Tools\Exception\NotReadableException
*/
protected function _clear(array $filenames): int
{
array_walk($filenames, function (string $filename): void {
$filename = Exceptionist::isReadable(Filesystem::instance()->concatenate(THUMBER_TARGET, $filename));
Filesystem::instance()->remove($filename);
$filename = Filesystem::instance()->concatenate(THUMBER_TARGET, $filename);
Filesystem::instance()->remove(Exceptionist::isReadable($filename));
});

return count($filenames);
Expand All @@ -49,22 +50,21 @@ protected function _clear(array $filenames): int
* Internal method to find thumbnails
* @param string $pattern A pattern (a regexp, a glob, or a string)
* @param bool $sort Whether results should be sorted
* @return array<string, string> Filenames
* @throws \Tools\Exception\MethodNotExistsException
* @return string[] Filenames
*/
protected function _find(string $pattern = '', bool $sort = false): array
{
$pattern = $pattern ?: sprintf('/[\d\w]{32}_[\d\w]{32}\.(%s)$/', implode('|', self::SUPPORTED_FORMATS));
$finder = (new Finder())->files()->name($pattern)->in(THUMBER_TARGET);

return objects_map(iterator_to_array($sort ? $finder->sortByName() : $finder), 'getFilename');
return array_map(fn(SplFileInfo $file): string => $file->getFilename(), iterator_to_array($sort ? $finder->sortByName() : $finder));
}

/**
* Clears all thumbnails that have been generated from an image path
* @param string $path Path of the original image
* @return int Number of thumbnails deleted
* @throws \Tools\Exception\FileNotExistsException|\Tools\Exception\NotReadableException|\Throwable
* @throws \Tools\Exception\NotReadableException
*/
public function clear(string $path): int
{
Expand All @@ -74,7 +74,7 @@ public function clear(string $path): int
/**
* Clears all thumbnails
* @return int Number of thumbnails deleted
* @throws \Tools\Exception\FileNotExistsException|\Tools\Exception\NotReadableException|\Throwable
* @throws \Tools\Exception\NotReadableException
*/
public function clearAll(): int
{
Expand All @@ -85,8 +85,8 @@ public function clearAll(): int
* Gets all thumbnails that have been generated from an image path
* @param string $path Path of the original image
* @param bool $sort Whether results should be sorted
* @return array<string, string>
* @throws \Tools\Exception\MethodNotExistsException|\Tools\Exception\NotReadableException
* @return string[]
* @throws \Tools\Exception\NotReadableException
*/
public function get(string $path, bool $sort = false): array
{
Expand All @@ -98,8 +98,7 @@ public function get(string $path, bool $sort = false): array
/**
* Gets all thumbnails
* @param bool $sort Whether results should be sorted
* @return array<string, string>
* @throws \Tools\Exception\MethodNotExistsException
* @return string[]
*/
public function getAll(bool $sort = false): array
{
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/TestSuite/TestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
class TestTraitTest extends TestCase
{
/**
* Test for `skipIfDriverIs()` method
* @test
* @uses \Thumber\TestSuite\TestTrait::skipIfDriverIs()
*/
public function testSkipIfDriverIs(): void
{
Expand Down

0 comments on commit e7830bf

Please sign in to comment.