-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added controller plugin and view helper imageSize().
- Loading branch information
Showing
10 changed files
with
245 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
Oops, something went wrong.