Implement common AlbumArt resize handler.

This commit is contained in:
Buster "Silver Eagle" Neece 2022-05-29 17:58:56 -05:00
parent 03ca7e2ec6
commit 87d74adbd3
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
6 changed files with 57 additions and 57 deletions

View File

@ -506,15 +506,6 @@ return [
return new Supervisor\Supervisor($client, $logger);
},
// Image Manager
Intervention\Image\ImageManager::class => static function () {
return new Intervention\Image\ImageManager(
[
'driver' => 'gd',
]
);
},
// NowPlaying Adapter factory
NowPlaying\AdapterFactory::class => static function (
GuzzleHttp\Client $httpClient,

View File

@ -9,15 +9,14 @@ use App\Entity;
use App\Environment;
use App\Http\Response;
use App\Http\ServerRequest;
use App\Media\AlbumArt;
use App\Service\Flow;
use Intervention\Image\ImageManager;
use Psr\Http\Message\ResponseInterface;
final class PostCustomAssetAction
{
public function __construct(
private readonly Environment $environment,
private readonly ImageManager $imageManager,
private readonly Environment $environment
) {
}
@ -34,9 +33,9 @@ final class PostCustomAssetAction
}
$imageContents = $flowResponse->readAndDeleteUploadedFile();
$image = $this->imageManager->make($imageContents);
$customAsset->upload($image);
$customAsset->upload(
AlbumArt::getImageManager()->make($imageContents)
);
return $response->withJson(Entity\Api\Status::success());
}

View File

@ -10,10 +10,9 @@ use App\Entity;
use App\Environment;
use App\Exception\InvalidPodcastMediaFileException;
use App\Exception\StorageLocationFullException;
use App\Media\AlbumArt;
use App\Media\MetadataManager;
use Azura\Files\ExtendedFilesystemInterface;
use Intervention\Image\Constraint;
use Intervention\Image\ImageManager;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToRetrieveMetadata;
use Psr\Log\LoggerInterface;
@ -25,7 +24,6 @@ use Symfony\Component\Serializer\Serializer;
class PodcastEpisodeRepository extends Repository
{
public function __construct(
protected ImageManager $imageManager,
protected MetadataManager $metadataManager,
ReloadableEntityManagerInterface $entityManager,
Serializer $serializer,
@ -85,17 +83,7 @@ class PodcastEpisodeRepository extends Repository
Entity\PodcastEpisode $episode,
string $rawArtworkString
): void {
$episodeArtwork = $this->imageManager->make($rawArtworkString);
$episodeArtwork->fit(
3000,
3000,
function (Constraint $constraint): void {
$constraint->upsize();
}
);
$episodeArtwork->encode('jpg');
$episodeArtworkString = $episodeArtwork->getEncoded();
$episodeArtworkString = AlbumArt::resize($rawArtworkString);
$storageLocation = $episode->getPodcast()->getStorageLocation();
$fs = $storageLocation->getFilesystem();

View File

@ -9,9 +9,8 @@ use App\Doctrine\Repository;
use App\Entity;
use App\Environment;
use App\Exception\StorageLocationFullException;
use App\Media\AlbumArt;
use Azura\Files\ExtendedFilesystemInterface;
use Intervention\Image\Constraint;
use Intervention\Image\ImageManager;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToRetrieveMetadata;
use Psr\Log\LoggerInterface;
@ -27,7 +26,6 @@ class PodcastRepository extends Repository
Serializer $serializer,
Environment $environment,
LoggerInterface $logger,
protected ImageManager $imageManager,
protected PodcastEpisodeRepository $podcastEpisodeRepo,
) {
parent::__construct($entityManager, $serializer, $environment, $logger);
@ -87,16 +85,7 @@ class PodcastRepository extends Repository
$storageLocation = $podcast->getStorageLocation();
$fs ??= $storageLocation->getFilesystem();
$podcastArtwork = $this->imageManager->make($rawArtworkString);
$podcastArtwork->fit(
3000,
3000,
function (Constraint $constraint): void {
$constraint->upsize();
}
);
$podcastArtwork->encode('jpg');
$podcastArtworkString = $podcastArtwork->getEncoded();
$podcastArtworkString = AlbumArt::resize($rawArtworkString);
$podcastArtworkSize = strlen($podcastArtworkString);
if (!$storageLocation->canHoldFile($podcastArtworkSize)) {

View File

@ -9,14 +9,13 @@ use App\Doctrine\Repository;
use App\Entity;
use App\Environment;
use App\Exception\CannotProcessMediaException;
use App\Media\AlbumArt;
use App\Media\MetadataManager;
use App\Media\RemoteAlbumArt;
use App\Service\AudioWaveform;
use Azura\Files\ExtendedFilesystemInterface;
use Exception;
use Generator;
use Intervention\Image\Constraint;
use Intervention\Image\ImageManager;
use League\Flysystem\FilesystemException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Serializer\Serializer;
@ -40,8 +39,7 @@ class StationMediaRepository extends Repository
protected CustomFieldRepository $customFieldRepo,
protected StationPlaylistMediaRepository $spmRepo,
protected StorageLocationRepository $storageLocationRepo,
protected UnprocessableMediaRepository $unprocessableMediaRepo,
protected ImageManager $imageManager
protected UnprocessableMediaRepository $unprocessableMediaRepo
) {
parent::__construct($em, $serializer, $environment, $logger);
}
@ -334,19 +332,10 @@ class StationMediaRepository extends Repository
$media->setArtUpdatedAt(time());
$this->em->persist($media);
$albumArt = $this->imageManager->make($rawArtString);
$albumArt->fit(
1200,
1200,
function (Constraint $constraint): void {
$constraint->upsize();
}
);
$albumArtPath = Entity\StationMedia::getArtPath($media->getUniqueId());
$albumArtStream = $albumArt->stream('jpg');
$albumArtString = AlbumArt::resize($rawArtString);
$fs->writeStream($albumArtPath, $albumArtStream->detach());
$fs->write($albumArtPath, $albumArtString);
}
public function removeAlbumArt(

44
src/Media/AlbumArt.php Normal file
View File

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Media;
use Intervention\Image\Constraint;
use Intervention\Image\ImageManager;
final class AlbumArt
{
public const IMAGE_WIDTH = 1500;
public static function resize(
string $rawArtworkString,
int $width = self::IMAGE_WIDTH,
int $height = self::IMAGE_WIDTH,
bool $upsize = false,
): string {
$newArtwork = self::getImageManager()->make($rawArtworkString);
$newArtwork->fit(
$width,
$height,
function (Constraint $constraint) use ($upsize) {
if (!$upsize) {
$constraint->upsize();
}
}
);
$newArtwork->encode('jpg');
return $newArtwork->getEncoded();
}
public static function getImageManager(): ImageManager
{
return new ImageManager(
[
'driver' => 'gd',
]
);
}
}