-
Notifications
You must be signed in to change notification settings - Fork 149
Add imagick extension support #2194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danloveg
wants to merge
29
commits into
artefactual:qa/2.x
Choose a base branch
from
danloveg:dev/2163-switch-to-imagick-library
base: qa/2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
274284e
Install imagick==3.8.0 in app image
danloveg 0134ffb
Create sfImagickAdapter class
danloveg 84b8286
Fix logic when requested page > page count
danloveg 936152a
Add deprecation warning to sfImageMagickAdapter
danloveg 805d8df
Enable sfImagickAdapter
danloveg 11cb9cd
Re-write ad hoc multi-page logic with Imagick
danloveg d531651
Simplify getting MIME type
danloveg b2f3d97
Fix incorrect page error issue
danloveg c7ada99
Add error checking in loadFile
danloveg 509db25
Use constants instead of magic strings/nums
danloveg 5015b72
Fix setting resolution
danloveg 2d546f9
Add AtoM copyright block
danloveg 42483ae
Check image magick availability for fallback cases
danloveg e056770
Change 'white' to constant in adapter
danloveg 18a2d17
Revert changes to memcache installation
danloveg 719dbed
Add missing cleanup command
danloveg 266207d
Remove sfImageMagickAdapter references
danloveg a77b974
Remove the sfImageMagickAdapter.class.php file
danloveg eba011e
Update comments in QubitDigitalObject
danloveg a29a839
Add thumbnail tests for QubitDigitalObject
danloveg 37e751b
Simplify logic in setPageCount()
danloveg 5b950a2
Add more tests for thumbnails and page counts
danloveg 6224535
Remove PDF tests that policy.xml is preventing
danloveg 49302a5
Revert changes to BaseDigitalObject.php
danloveg 4a92bf2
Add functions to check for image extensions
danloveg d6de81e
Adjust extension priority in plugin
danloveg c721411
Use pathinfo instead of regex for filename
danloveg a8b0610
Capitalize Imagick in message
danloveg a36230a
Remove types, fix error message
danloveg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -30,6 +30,16 @@ class QubitDigitalObject extends BaseDigitalObject | |
| public const THUMB_MIME_TYPE = 'image/jpeg'; | ||
| public const THUMB_EXTENSION = 'jpg'; | ||
|
|
||
| // Constants for exploding multi-page assets | ||
| public const string MULTI_PAGE_ASSET_EXTRACT_BACKGROUND = 'white'; | ||
| public const string MULTI_PAGE_ASSET_EXTRACT_FORMAT = 'jpeg'; | ||
| public const int MULTI_PAGE_ASSET_EXTRACT_QUALITY = 100; | ||
| public const int MULTI_PAGE_ASSET_EXTRACT_RESOLUTION = 300; // This controls the DPI | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should omit the types here as well since this would break in PHP 8.1 and PHP 8.2.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed in a36230a |
||
|
|
||
| // Cached properties for finding the state of loaded image manipulation extensions | ||
| public static ?bool $IMAGICK_EXTENSION_LOADED = null; | ||
| public static ?bool $GD_EXTENSION_LOADED = null; | ||
|
|
||
| // Variables for save actions | ||
| public $assets = []; | ||
| public $indexOnSave = true; | ||
|
|
@@ -1929,38 +1939,31 @@ public function createRepresentations($usageId, $connection = null) | |
| /** | ||
| * Set 'page_count' property for this asset. | ||
| * | ||
| * NOTE: requires the ImageMagick library | ||
| * Requires the imagick extension. | ||
| * | ||
| * @param null|mixed $connection | ||
| * | ||
| * @return QubitDigitalObject this object | ||
| */ | ||
| public function setPageCount($connection = null) | ||
| { | ||
| if ($this->canThumbnail() && sfImageMagickAdapter::isImageMagickAvailable()) { | ||
| $filename = ($this->derivativesGeneratedFromExternalMaster($this->usageId)) ? $this->getLocalPath() : $this->getAbsolutePath(); | ||
| if (!$this->canThumbnail() || !self::imagickExtensionLoaded()) { | ||
| return $this; | ||
| } | ||
|
|
||
| $extension = pathinfo($filename, PATHINFO_EXTENSION); | ||
| $filename = ($this->derivativesGeneratedFromExternalMaster($this->usageId)) ? $this->getLocalPath() : $this->getAbsolutePath(); | ||
|
|
||
| // If processing a PDF, attempt to use pdfinfo as it's faster | ||
| if ('pdf' == strtolower($extension) && sfImageMagickAdapter::pdfinfoToolAvailable()) { | ||
| $pages = sfImageMagickAdapter::getPdfinfoPageCount($filename); | ||
| } else { | ||
| $command = 'identify '.$filename; | ||
| exec($command, $output, $status); | ||
| $pages = count($output); | ||
| } | ||
| $im = new Imagick(); | ||
|
danloveg marked this conversation as resolved.
|
||
| $im->pingImage($filename); | ||
| $pages = $im->getNumberImages(); | ||
| $im->clear(); | ||
|
|
||
| if (0 == $status) { | ||
| // Add "number of pages" property | ||
| $pageCount = new QubitProperty(); | ||
| $pageCount->setObjectId($this->id); | ||
| $pageCount->setName('page_count'); | ||
| $pageCount->setScope('digital_object'); | ||
| $pageCount->setValue($pages, ['sourceCulture' => true]); | ||
| $pageCount->save($connection); | ||
| } | ||
| } | ||
| $pageCount = new QubitProperty(); | ||
| $pageCount->setObjectId($this->id); | ||
| $pageCount->setName('page_count'); | ||
| $pageCount->setScope('digital_object'); | ||
| $pageCount->setValue($pages, ['sourceCulture' => true]); | ||
| $pageCount->save($connection); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
@@ -1980,6 +1983,8 @@ public function getPageCount() | |
| if ($pageCount) { | ||
| return (int) $pageCount->getValue(); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| // TODO: add $options for filter | ||
|
|
@@ -1996,35 +2001,50 @@ public function getPage($index) | |
|
|
||
| /** | ||
| * Explode multi-page asset into multiple image files. | ||
| * | ||
| * @return unknown | ||
| */ | ||
| public function explodeMultiPageAsset() | ||
| { | ||
| $pageCount = $this->getPageCount(); | ||
|
|
||
| if ($pageCount > 1 && $this->canThumbnail()) { | ||
| $fileList = []; | ||
|
|
||
| if ($pageCount > 1 && $this->canThumbnail() && self::imagickExtensionLoaded()) { | ||
| if ($this->derivativesGeneratedFromExternalMaster($this->usageId)) { | ||
| $path = $this->localPath; | ||
| } else { | ||
| $path = $this->getAbsolutePath(); | ||
| } | ||
|
|
||
| $filenameMinusExtension = preg_replace('/\.[a-zA-Z]{2,3}$/', '', $path); | ||
| $filenameMinusExtension = pathinfo($path, PATHINFO_FILENAME); | ||
|
|
||
| $command = 'convert -density 300 -alpha remove -quality 100 '; | ||
| $command .= $path; | ||
| $command .= ' '.$filenameMinusExtension.'_%02d.'.self::THUMB_EXTENSION; | ||
| exec($command, $output, $status); | ||
| $imagick = new Imagick(); | ||
|
danloveg marked this conversation as resolved.
|
||
|
|
||
| if (1 == $status) { | ||
| throw new sfException('Encountered error'.(is_array($output) && count($output) > 0 ? ': '.implode('\n'.$output) : ' ').' while running convert (ImageMagick).'); | ||
| } | ||
| // Set read resolution before opening file | ||
| $imagick->setResolution( | ||
| self::MULTI_PAGE_ASSET_EXTRACT_RESOLUTION, | ||
| self::MULTI_PAGE_ASSET_EXTRACT_RESOLUTION, | ||
| ); | ||
| $imagick->readImage($path); | ||
|
|
||
| foreach ($imagick as $index => $page) { | ||
| $page->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); | ||
| $page->setImageBackgroundColor(self::MULTI_PAGE_ASSET_EXTRACT_BACKGROUND); | ||
| $page->setImageFormat(self::MULTI_PAGE_ASSET_EXTRACT_FORMAT); | ||
| $page->setImageCompressionQuality(self::MULTI_PAGE_ASSET_EXTRACT_QUALITY); | ||
|
|
||
| // Build an array of the exploded file names | ||
| for ($i = 0; $i < $pageCount; ++$i) { | ||
| $fileList[] = $filenameMinusExtension.sprintf('_%02d.', $i).self::THUMB_EXTENSION; | ||
| $filename = sprintf( | ||
| '%s_%02d.%s', | ||
| $filenameMinusExtension, | ||
| $index, | ||
| self::THUMB_EXTENSION, | ||
| ); | ||
|
|
||
| $page->writeImage($filename); | ||
|
|
||
| $fileList[] = $filename; | ||
| } | ||
|
|
||
| $imagick->clear(); | ||
| } | ||
|
|
||
| return $fileList; | ||
|
|
@@ -2038,21 +2058,21 @@ public function explodeMultiPageAsset() | |
| * object and linked child digital object and move the derived | ||
| * asset to the appropriate directory for the new (child) info object | ||
| * | ||
| * NOTE: Requires the Imagemagick library for creating derivative assets | ||
| * NOTE: Requires the imagick extension for creating derivative assets | ||
| * | ||
| * @param null|mixed $connection | ||
| * | ||
| * @return QubitDigitalObject this object | ||
| */ | ||
| public function createCompoundChildren($connection = null) | ||
| { | ||
| // Bail out if the imagemagick library is not installed | ||
| if (false === sfImageMagickAdapter::isImageMagickAvailable()) { | ||
| $pages = $this->explodeMultiPageAsset(); | ||
|
|
||
| // Bail out if no pages were found | ||
| if (0 === count($pages)) { | ||
| return $this; | ||
| } | ||
|
|
||
| $pages = $this->explodeMultiPageAsset(); | ||
|
|
||
| foreach ($pages as $i => $filepath) { | ||
| $filename = basename($filepath); | ||
|
|
||
|
|
@@ -2167,6 +2187,38 @@ public static function getMin($settings) | |
| * ----------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| /** | ||
| * Determine whether the imagick extension is loaded. | ||
| * | ||
| * @return bool true if the extension is loaded and can be used, false otherwise | ||
| */ | ||
| public static function imagickExtensionLoaded(): bool | ||
| { | ||
| if (null !== self::$IMAGICK_EXTENSION_LOADED) { | ||
| return self::$IMAGICK_EXTENSION_LOADED; | ||
| } | ||
|
|
||
| self::$IMAGICK_EXTENSION_LOADED = extension_loaded('imagick'); | ||
|
|
||
| return self::$IMAGICK_EXTENSION_LOADED; | ||
| } | ||
|
|
||
| /** | ||
| * Determine whether the gd extension is loaded. | ||
| * | ||
| * @return bool true if the extension is loaded and can be used, false otherwise | ||
| */ | ||
| public static function gdExtensionLoaded() | ||
| { | ||
| if (null !== self::$GD_EXTENSION_LOADED) { | ||
| return self::$GD_EXTENSION_LOADED; | ||
| } | ||
|
|
||
| self::$GD_EXTENSION_LOADED = extension_loaded('gd'); | ||
|
|
||
| return self::$GD_EXTENSION_LOADED; | ||
| } | ||
|
|
||
| /** | ||
| * Create a thumbnail derivative for the current digital object. | ||
| * | ||
|
|
@@ -2204,7 +2256,7 @@ public function getLocalPath() | |
| if (null === $this->localPath && QubitTerm::EXTERNAL_FILE_ID == $this->usageId) { | ||
| $filename = basename($this->path); | ||
| if (false === $contents = $this->file_get_contents_if_not_empty($this->path)) { | ||
| throw new sfException(sprintf('Error reading file or file is empty.', $filepath)); | ||
| throw new sfException(sprintf('Error reading file or file is empty.', $filename)); | ||
| } | ||
| $this->localPath = Qubit::saveTemporaryFile($filename, $contents); | ||
| } | ||
|
|
@@ -2382,7 +2434,7 @@ public static function resizeImage($originalImageName, $width = null, $height = | |
| } | ||
|
|
||
| /** | ||
| * Get a valid adapter for the sfThumbnail library (either GD or ImageMagick) | ||
| * Get a valid adapter for the sfThumbnail library (either GD or imagick) | ||
| * Cache the adapter value because is very expensive to calculate it. | ||
| * | ||
| * @return mixed name of adapter on success, false on failure | ||
|
|
@@ -2396,9 +2448,9 @@ public static function getThumbnailAdapter() | |
| return $context->get('thumbnailAdapter'); | ||
| } | ||
|
|
||
| if (sfImageMagickAdapter::isImageMagickAvailable()) { | ||
| $adapter = 'sfImageMagickAdapter'; | ||
| } elseif (QubitDigitalObject::hasGdExtension()) { | ||
| if (self::imagickExtensionLoaded()) { | ||
| $adapter = 'sfImagickAdapter'; | ||
| } elseif (self::gdExtensionLoaded()) { | ||
| $adapter = 'sfGDAdapter'; | ||
| } | ||
|
|
||
|
|
@@ -2407,16 +2459,6 @@ public static function getThumbnailAdapter() | |
| return $adapter; | ||
| } | ||
|
|
||
| /** | ||
| * Test if GD Extension for PHP is installed. | ||
| * | ||
| * @return bool true if GD extension found | ||
| */ | ||
| public static function hasGdExtension() | ||
| { | ||
| return extension_loaded('gd'); | ||
| } | ||
|
|
||
| /** | ||
| * Wrapper for canThumbnailMimeType() for use on instantiated objects. | ||
| * | ||
|
|
@@ -2445,13 +2487,13 @@ public static function canThumbnailMimeType($mimeType) | |
|
|
||
| $canThumbnail = false; | ||
|
|
||
| // For Images, we can create thumbs with either GD or ImageMagick | ||
| // For Images, we can create thumbs with either GD or imagick | ||
| if ('image' == substr($mimeType, 0, 5) && strlen($adapter)) { | ||
| $canThumbnail = true; | ||
| } | ||
|
|
||
| // For PDFs we can only create thumbs with ImageMagick | ||
| elseif ('application/pdf' == $mimeType && 'sfImageMagickAdapter' == $adapter) { | ||
| // For PDFs we can only create thumbs with imagick | ||
| elseif ('application/pdf' == $mimeType && 'sfImagickAdapter' == $adapter) { | ||
| $canThumbnail = true; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.