Skip to content

Commit

Permalink
Add array initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
ipf committed Oct 14, 2021
1 parent 89c32a4 commit 244d285
Show file tree
Hide file tree
Showing 10 changed files with 561 additions and 566 deletions.
20 changes: 8 additions & 12 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<?php

$finder = PhpCsFixer\Finder::create()
->exclude('build')
->exclude('cache')
->exclude('var')
->exclude('vendor')
->exclude(['var', 'cache', 'build', 'vendor', 'public'])
->in(__DIR__);

$config = new PhpCsFixer\Config();
$config
return (new PhpCsFixer\Config())
->setRules([
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_class_elements' => [
'order' => [
'use_trait',
Expand All @@ -26,11 +23,10 @@
'phpunit',
'method_public',
'method_protected',
'method_private'
]
'method_private',
],
'sort_algorithm' => 'alpha',
],
'array_syntax' => ['syntax' => 'short'],
])
->setFinder($finder);

return $config;
->setFinder($finder)
;
263 changes: 131 additions & 132 deletions Service/ImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@
*/
class ImageService implements \Subugoe\IIIFModel\Service\ImageServiceInterface
{
private FileService $fileService;

private array $imageConfiguration = [];
private Imagine $imagine;

private RouterInterface $router;

private TranslatorInterface $translator;

private FileService $fileService;

private array $imageConfiguration;

/**
* ImageService constructor.
*/
Expand All @@ -53,30 +52,33 @@ public function __construct(
$this->fileService = $fileService;
}

public function setImageConfiguration(array $imageConfiguration): void
public function getCachedFileIdentifier(Image $image): string
{
$this->imageConfiguration = $imageConfiguration;
return vsprintf(
'%s/%s.%s',
[
$image->getIdentifier(),
$this->getImageHash($image),
$image->getFormat(),
]
);
}

public function getImageConfiguration(): array
public function getFilename(Document $document, Image $image): string
{
return $this->imageConfiguration;
/** @var PhysicalStructure $physicalStructure */
foreach ($document->getPhysicalStructures() as $physicalStructure) {
if ($image->getIdentifier() === $physicalStructure->getIdentifier()) {
return $physicalStructure->getFilename();
}
}

return $image->getIdentifier().'.'.$image->getFormat();
}

public function process(Image $imageEntity): string
public function getImageConfiguration(): array
{
$image = $this->imagine->load($this->getOriginalFileContents($imageEntity));

// strip the profile, as Firefox uses this and may display images with strange colors
$profile = new Profile('', '');
$image = $image->profile($profile);

$this->getRegion($imageEntity->getRegion(), $image);
$this->getSize($imageEntity->getSize(), $image);
$this->getRotation($imageEntity->getRotation(), $image);
$this->getQuality($imageEntity->getQuality(), $image);

return $image->get($imageEntity->getFormat());
return $this->imageConfiguration;
}

public function getImageJsonInformation(string $identifier, $originalImage): ImageInformation
Expand Down Expand Up @@ -157,23 +159,88 @@ public function getOriginalFileContents(Image $image)
return $sourceFilesystem->read($filename);
}

public function getCachedFileIdentifier(Image $image): string
public function process(Image $imageEntity): string
{
return vsprintf(
'%s/%s.%s',
[
$image->getIdentifier(),
$this->getImageHash($image),
$image->getFormat(),
]
);
$image = $this->imagine->load($this->getOriginalFileContents($imageEntity));

// strip the profile, as Firefox uses this and may display images with strange colors
$profile = new Profile('', '');
$image = $image->profile($profile);

$this->getRegion($imageEntity->getRegion(), $image);
$this->getSize($imageEntity->getSize(), $image);
$this->getRotation($imageEntity->getRotation(), $image);
$this->getQuality($imageEntity->getQuality(), $image);

return $image->get($imageEntity->getFormat());
}

public function setImageConfiguration(array $imageConfiguration): void
{
$this->imageConfiguration = $imageConfiguration;
}

private function getImageHash(Image $image): string
{
return hash('sha256', serialize($image));
}

private function getImageSizes(BoxInterface $originalSize): array
{
$sizes = [];
$sizeList = $this->imageConfiguration['zoom_levels'];

foreach ($sizeList as $size) {
$dimension = new Dimension();
$dimension
->setHeight($originalSize->getHeight() / $size)
->setWidth($originalSize->getWidth() / $size);

$sizes[] = $dimension;
}

return array_reverse($sizes);
}

/*
* Apply the requested image quality as per IIIF-Image API
*
* Quality parameters may be:
* - color
* - gray
* - bitonal
* - default
*
* @see http://iiif.io/api/image/2.0/#quality
*
* @param string $quality The requested image quality
* @param ImageInterface $image The image object
*
* @throws BadRequestHttpException if wrong quality parameters provided
*
* @return ImageInterface
*/
private function getQuality(string $quality, ImageInterface $image): void
{
if ('default' === $quality || 'color' === $quality) {
return;
}

switch ($quality) {
case 'gray':
$image->effects()->grayscale();
break;
case 'bitonal':
$max = $image->getImagick()->getQuantumRange();
$max = $max['quantumRangeLong'];
$imageClearnessFactor = 0.20;
$image->getImagick()->thresholdImage($max * $imageClearnessFactor);
break;
default:
throw new BadRequestHttpException(sprintf('Bad Request: %s is not a supported quality.', $quality));
}
}

/*
* Apply the requested image region as per IIIF-Image API.
* Region parameters may be:
Expand Down Expand Up @@ -253,6 +320,40 @@ private function getRegion(string $region, ImageInterface $image): void
$image->crop(new Point($x, $y), new Box($w, $h));
}

/*
* Apply the requested image rotation as per IIIF-Image API
* Rotation parameters may be:
* - n
* - !n
*
* @see http://iiif.io/api/image/2.0/#rotation
*
* @param string $rotation The requested image rotation
* @param ImageInterface $image The image object
*
* @throws BadRequestHttpException if wrong rotation parameters provided
*
* @return ImageInterface
*/
private function getRotation(string $rotation, ImageInterface $image): void
{
if (0 === (int) $rotation) {
return;
}

if (!empty($rotation)) {
$rotationDegree = str_replace('!', '', $rotation);
if ((int) $rotationDegree <= 360) {
if (false !== strpos($rotation, '!')) {
$image->flipVertically();
}
$image->rotate(str_replace('!', '', $rotation));
} else {
throw new BadRequestHttpException(sprintf('Bad Request: Rotation argument %s is not between 0 and 360.', $rotationDegree));
}
}
}

/*
* Apply the requested image size as per IIIF-Image API
* Size parameters may be:
Expand Down Expand Up @@ -311,96 +412,6 @@ private function getSize(string $size, ImageInterface $image): void
}
}

/*
* Apply the requested image rotation as per IIIF-Image API
* Rotation parameters may be:
* - n
* - !n
*
* @see http://iiif.io/api/image/2.0/#rotation
*
* @param string $rotation The requested image rotation
* @param ImageInterface $image The image object
*
* @throws BadRequestHttpException if wrong rotation parameters provided
*
* @return ImageInterface
*/
private function getRotation(string $rotation, ImageInterface $image): void
{
if (0 === (int) $rotation) {
return;
}

if (!empty($rotation)) {
$rotationDegree = str_replace('!', '', $rotation);
if ((int) $rotationDegree <= 360) {
if (false !== strpos($rotation, '!')) {
$image->flipVertically();
}
$image->rotate(str_replace('!', '', $rotation));
} else {
throw new BadRequestHttpException(sprintf('Bad Request: Rotation argument %s is not between 0 and 360.', $rotationDegree));
}
}
}

/*
* Apply the requested image quality as per IIIF-Image API
*
* Quality parameters may be:
* - color
* - gray
* - bitonal
* - default
*
* @see http://iiif.io/api/image/2.0/#quality
*
* @param string $quality The requested image quality
* @param ImageInterface $image The image object
*
* @throws BadRequestHttpException if wrong quality parameters provided
*
* @return ImageInterface
*/
private function getQuality(string $quality, ImageInterface $image): void
{
if ('default' === $quality || 'color' === $quality) {
return;
}

switch ($quality) {
case 'gray':
$image->effects()->grayscale();
break;
case 'bitonal':
$max = $image->getImagick()->getQuantumRange();
$max = $max['quantumRangeLong'];
$imageClearnessFactor = 0.20;
$image->getImagick()->thresholdImage($max * $imageClearnessFactor);
break;
default:
throw new BadRequestHttpException(sprintf('Bad Request: %s is not a supported quality.', $quality));
}
}

private function getImageSizes(BoxInterface $originalSize): array
{
$sizes = [];
$sizeList = $this->imageConfiguration['zoom_levels'];

foreach ($sizeList as $size) {
$dimension = new Dimension();
$dimension
->setHeight($originalSize->getHeight() / $size)
->setWidth($originalSize->getWidth() / $size);

$sizes[] = $dimension;
}

return array_reverse($sizes);
}

private function getTileInformation(array $sizeList): array
{
$tiles = [];
Expand All @@ -414,16 +425,4 @@ private function getTileInformation(array $sizeList): array

return $tiles;
}

public function getFilename(Document $document, Image $image): string
{
/** @var PhysicalStructure $physicalStructure */
foreach ($document->getPhysicalStructures() as $physicalStructure) {
if ($image->getIdentifier() === $physicalStructure->getIdentifier()) {
return $physicalStructure->getFilename();
}
}

return $image->getIdentifier().'.'.$image->getFormat();
}
}
Loading

0 comments on commit 244d285

Please sign in to comment.