Skip to content

Commit

Permalink
feat: improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamriajul committed Sep 10, 2024
1 parent 62477f6 commit 0b69815
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
21 changes: 14 additions & 7 deletions src/Thumbhash.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Intervention\Image\Image;
use Intervention\Image\ImageManager;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Thumbhash\Thumbhash as ThumbhashLib;

Expand Down Expand Up @@ -67,15 +68,19 @@ public function getDriver(): string
/**
* Encode an image to thumbhash string - base64 encoded.
*
* @param string|resource|Image|UploadedFile $data
* @param string|resource|Image|UploadedFile|File $input
*
* @throws \ImagickException
*/
public function encode(mixed $data): string
public function encode(mixed $input): string
{
if (is_string($input) && is_file($input)) {
$input = new File($input);
}

$imageManager = $this->driver == 'gd' ? ImageManager::gd() : ImageManager::imagick();

$data = $imageManager->read($data);
$data = $imageManager->read($input);

// Resize the image to lower resolution. max 100x100.
$originalWidth = $data->width();
Expand All @@ -84,15 +89,17 @@ public function encode(mixed $data): string
$scale = $this->imageMaxSize / max($originalWidth, $originalHeight);
$newWidth = $originalWidth * $scale;
$newHeight = $originalHeight * $scale;
$data = $data->scaleDown($newWidth, $newHeight)->encode();
$data = $data->scaleDown($newWidth, $newHeight)->encode()->toString();
} else if ($input instanceof File) {
$data = $input->getContent();
} else {
$data = $data->encode();
$data = $data->encode()->toString();
}

if ($this->driver === 'imagick') {
[$width, $height, $rgba] = extract_size_and_pixels_with_imagick($data->toString());
[$width, $height, $rgba] = extract_size_and_pixels_with_imagick($data);
} else {
[$width, $height, $rgba] = extract_size_and_pixels_with_gd($data->toString());
[$width, $height, $rgba] = extract_size_and_pixels_with_gd($data);
}

unset($data);
Expand Down
8 changes: 4 additions & 4 deletions tests/ThumbhashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ public function testEncodeImagick(): void
],
[
'url' => 'assets/field.jpg',
'hash' => '3OcRJYB4d3h/iIeHeEh3eIhw6j7A',
'hash' => '3OcRJYB4d3h/iIeHeEh3eIhw+j3A',
],
[
'url' => 'assets/fall.jpg',
'hash' => 'HBkSHYSIeHiPiHh8eJd4eTN0EEQG',
],
[
'url' => 'assets/street.jpg',
'hash' => 'VggKDYAW6lZvdYd6d2iZh/p4CE/j',
'hash' => 'VggKDYAW6lZvdYd6d2iZh/p4GE/k',
],
[
'url' => 'assets/mountain.jpg',
'hash' => '2fcZFIB3iId/h3iJh4aJUJ2V8g',
'hash' => '2fcZFIB3iId/h3iJh4aIYJ2V8g',
],
[
'url' => 'assets/coast.jpg',
Expand Down Expand Up @@ -90,7 +90,7 @@ public function testEncodeGd(): void
],
[
'url' => 'assets/mountain.jpg',
'hash' => '2fcZFIB3iId/h3iJh4aIYJ2W8g',
'hash' => '2fcZFIB3iId/h3iJh4aIYJ2V8g',
],
[
'url' => 'assets/coast.jpg',
Expand Down

0 comments on commit 0b69815

Please sign in to comment.