Skip to content

Commit

Permalink
Added controller plugin and view helper imageSize().
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-KM committed Oct 28, 2018
1 parent e542dc4 commit 87b377b
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 249 deletions.
4 changes: 4 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'factories' => [
'iiifInfo' => Service\ViewHelper\IiifInfoFactory::class,
'iiifManifest' => Service\ViewHelper\IiifManifestFactory::class,
'imageSize' => Service\ViewHelper\ImageSizeFactory::class,
],
],
'form_elements' => [
Expand All @@ -43,6 +44,9 @@
'tileInfo' => Mvc\Controller\Plugin\TileInfo::class,
'tileServer' => Mvc\Controller\Plugin\TileServer::class,
],
'factories' => [
'imageSize' => Service\ControllerPlugin\ImageSizeFactory::class,
],
],
'media_ingesters' => [
'factories' => [
Expand Down
6 changes: 3 additions & 3 deletions config/module.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[info]
name = "IIIF Server"
description = "Integrates the IIIF specifications and a simple IIP Image Server to allow to process and share instantly images of any size and medias (pdf, audio, video, 3D...) in the desired formats."
tags = "specification, standard, share"
tags = "iiif, image server, specification, standard, share"
license = "CeCILL v2.1"
author = "Daniel Berthereau, Omeka S port by BibLibre"
author_link = "https://github.com/Daniel-KM"
module_link = "https://github.com/Daniel-KM/Omeka-S-module-IiifServer"
support_link = "https://github.com/Daniel-KM/Omeka-S-module-IiifServer/issues"
configurable = true
version = "3.5.10"
omeka_version_constraint = "^1.0.0"
version = "3.5.11"
omeka_version_constraint = "^1.1.0"
6 changes: 2 additions & 4 deletions data/scripts/upgrade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
namespace DownloadManager;

use Omeka\Module\Exception\ModuleCannotInstallException;
namespace IiifServer;

/**
* @var Module $this
Expand Down Expand Up @@ -51,7 +49,7 @@
if (empty($version)) {
// Nothing to do.
} elseif (version_compare($version, '3.15.4', '<')) {
throw new ModuleCannotInstallException(
throw new \Omeka\Module\Exception\ModuleCannotInstallException(
$t->translate('This version requires Archive Repertory 3.15.4 or greater (used for some 3D views).')); // @translate
}
}
Expand Down
84 changes: 6 additions & 78 deletions src/Controller/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ protected function _cleanRequest(MediaRepresentation $media)
$transform['source']['filepath'] = $this->_getImagePath($media, 'original');
$transform['source']['media_type'] = $media->mediaType();

list($sourceWidth, $sourceHeight) = array_values($this->_getImageSize($media, 'original'));
$imageSize = $this->imageSize($media, 'original');
list($sourceWidth, $sourceHeight) = $imageSize ? array_values($imageSize) : [null, null];
$transform['source']['width'] = $sourceWidth;
$transform['source']['height'] = $sourceHeight;

Expand Down Expand Up @@ -515,7 +516,8 @@ protected function _cleanRequest(MediaRepresentation $media)
foreach ($availableTypes as $imageType) {
$filepath = $this->_getImagePath($media, $imageType);
if ($filepath) {
list($testWidth, $testHeight) = array_values($this->_getImageSize($media, $imageType));
$imageSize = $this->imageSize($media, $imageType);
list($testWidth, $testHeight) = $imageSize ? array_values($imageSize) : [null, null];
if ($destinationWidth == $testWidth && $destinationHeight == $testHeight) {
$transform['size']['feature'] = 'full';
// Change the source file to avoid a transformation.
Expand Down Expand Up @@ -649,7 +651,8 @@ protected function _useOmekaDerivative(MediaRepresentation $media, $transform)

// Currently, the check is done only on fullsize.
$derivativeType = 'large';
list($derivativeWidth, $derivativeHeight) = array_values($this->_getImageSize($media, $derivativeType));
$imageSize = $this->imageSize($media, $derivativeType);
list($derivativeWidth, $derivativeHeight) = $imageSize ? array_values($imageSize) : [null, null];
switch ($transform['size']['feature']) {
case 'sizeByW':
case 'sizeByH':
Expand Down Expand Up @@ -737,39 +740,6 @@ protected function _transformImage($args)
return $imageServer->transform($args);
}

/**
* Get an array of the width and height of the image file.
*
* @param MediaRepresentation $media
* @param string $imageType
* @return array Associative array of width and height of the image file.
* If the file is not an image, the width and the height will be null.
*
* @see \IiifServer\View\Helper\IiifManifest::_getImageSize()
* @see \IiifServer\View\Helper\IiifInfo::_getImageSize()
* @todo Refactorize.
*/
protected function _getImageSize(MediaRepresentation $media, $imageType = 'original')
{
// Check if this is an image.
if (empty($media) || strpos($media->mediaType(), 'image/') !== 0) {
return [
'width' => null,
'height' => null,
];
}

$filepath = $this->_mediaFilePath($media, $imageType);

$result = $this->_getWidthAndHeight($filepath);

if (empty($result['width']) || empty($result['height'])) {
throw new Exception("Failed to get image resolution: $filepath");
}

return $result;
}

/**
* Get the path to an original or derivative file for an image.
*
Expand Down Expand Up @@ -810,46 +780,4 @@ protected function getStoragePath($prefix, $name, $extension = null)
{
return sprintf('%s/%s%s', $prefix, $name, $extension ? ".$extension" : null);
}

/**
* Helper to get width and height of an image.
*
* @param string $filepath This should be an image (no check here).
* @return array Associative array of width and height of the image file.
* If the file is not an image, the width and the height will be null.
* @see \IiifServer\View\Helper\IiifInfo::_getWidthAndHeight()
* @todo Refactorize.
*/
protected function _getWidthAndHeight($filepath)
{
// An internet path.
if (strpos($filepath, 'https://') === 0 || strpos($filepath, 'http://') === 0) {
$tempFile = $this->tempFileFactory->build();
$tempPath = $tempFile->getTempPath();
$tempFile->delete();
$result = file_put_contents($tempPath, $filepath);
if ($result !== false) {
list($width, $height) = getimagesize($tempPath);
unlink($tempPath);
return [
'width' => $width,
'height' => $height,
];
}
unlink($tempPath);
}
// A normal path.
elseif (file_exists($filepath)) {
list($width, $height) = getimagesize($filepath);
return [
'width' => $width,
'height' => $height,
];
}

return [
'width' => null,
'height' => null,
];
}
}
119 changes: 119 additions & 0 deletions src/Mvc/Controller/Plugin/ImageSize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
namespace IiifServer\Mvc\Controller\Plugin;

use Omeka\Api\Representation\MediaRepresentation;
use Omeka\File\TempFileFactory;
use Omeka\Mvc\Exception\RuntimeException;
use Omeka\Stdlib\Message;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;

class ImageSize extends AbstractPlugin
{
/**
* @var string
*/
protected $basePath;

/**
* @var TempFileFactory
*/
protected $tempFileFactory;

/**
* @param string $basePath
* @param TempFileFactory $tempFileFactory
*/
public function __construct($basePath, TempFileFactory $tempFileFactory)
{
$this->basePath = $basePath;
$this->tempFileFactory = $tempFileFactory;
}

/**
* Get an array of the width and height of the image file from a media.
*
* @todo Store size in the data of the media.
*
* @param MediaRepresentation $media
* @param string $imageType
* @throws RuntimeException
* @return array|null Associative array of width and height of the image
* file, else null.
*/
public function __invoke(MediaRepresentation $media, $imageType = 'original')
{
// Check if this is an image.
if (strtok($media->mediaType(), '/') !== 'image') {
return null;
}

// The storage adapter should be checked for external storage.
$storagePath = $imageType == 'original'
? $this->getStoragePath($imageType, $media->filename())
: $this->getStoragePath($imageType, $media->storageId(), 'jpg');
$filepath = $this->basePath . DIRECTORY_SEPARATOR . $storagePath;
$result = $this->getWidthAndHeight($filepath);

// This is an image, but failed to get the resolution.
if (empty($result)) {
throw new RuntimeException(new Message('Failed to get image resolution: %s', // @translate
$storagePath));
}

return $result;
}

/**
* Get a storage path.
*
* @param string $prefix The storage prefix
* @param string $name The file name, or basename if extension is passed
* @param null|string $extension The file extension
* @return string
*/
protected function getStoragePath($prefix, $name, $extension = '')
{
return sprintf('%s/%s%s', $prefix, $name, strlen($extension) ? '.' . $extension : '');
}

/**
* Helper to get width and height of an image.
*
* @param string $filepath This should be an image (no check here).
* @return array|null Associative array of width and height of the image
* file, else null.
*/
protected function getWidthAndHeight($filepath)
{
// An internet path.
if (strpos($filepath, 'https://') === 0 || strpos($filepath, 'http://') === 0) {
$tempFile = $this->tempFileFactory->build();
$tempPath = $tempFile->getTempPath();
$tempFile->delete();
$result = file_put_contents($tempPath, $filepath);
if ($result !== false) {
$result = getimagesize($tempPath);
if ($result) {
list($width, $height) = $result;
}
}
unlink($tempPath);
}
// A normal path.
elseif (file_exists($filepath)) {
$result = getimagesize($filepath);
if ($result) {
list($width, $height) = $result;
}
}

if (empty($width) || empty($height)) {
return null;
}

return [
'width' => $width,
'height' => $height,
];
}
}
16 changes: 16 additions & 0 deletions src/Service/ControllerPlugin/ImageSizeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace IiifServer\Service\ControllerPlugin;

use IiifServer\Mvc\Controller\Plugin\ImageSize;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class ImageSizeFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $services, $requestedName, array $options = null)
{
$basePath = $services->get('Config')['file_store']['local']['base_path'] ?: (OMEKA_PATH . '/files');
$tempFileFactory = $services->get('Omeka\File\TempFileFactory');
return new ImageSize($basePath, $tempFileFactory);
}
}
18 changes: 18 additions & 0 deletions src/Service/ViewHelper/ImageSizeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace IiifServer\Service\ViewHelper;

use IiifServer\View\Helper\ImageSize;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class ImageSizeFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $services, $requestedName, array $options = null)
{
$pluginManager = $services->get('ControllerPluginManager');
$plugin = $pluginManager->get('imageSize');
return new ImageSize(
$plugin
);
}
}
Loading

0 comments on commit 87b377b

Please sign in to comment.