-
-
Notifications
You must be signed in to change notification settings - Fork 450
Imagick adapter, handles color profiles better #1750
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
Draft
woutersamaey
wants to merge
24
commits into
OpenMage:main
Choose a base branch
from
woutersamaey:patch-17
base: main
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.
+264
−14
Draft
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
b9b802c
Imagick adapter, handles color profiles better
woutersamaey 7efabc6
Update Imagick.php
woutersamaey 0e6103a
Update Imagick.php
woutersamaey 1471a61
Update Imagick.php
woutersamaey 9a00be9
Update Adapter.php
woutersamaey fd513ee
Update Imagick.php
woutersamaey c41214b
Added support for "exif:Orientation"
woutersamaey c1c09d8
Update Imagick.php
woutersamaey ebf5626
Update Imagick.php
woutersamaey 309f4ae
Update Imagick.php
woutersamaey 536046b
Update lib/Varien/Image/Adapter/Imagick.php
colinmollenhour 206ba74
Merge branch '20.0' into patch-17
fballiano 65d6d62
removed empty lines
fballiano 0404edf
removed strict types
fballiano 91dc8d7
Update lib/Varien/Image/Adapter/Imagick.php
fballiano 721e9c2
Update lib/Varien/Image/Adapter/Imagick.php
fballiano a68f9de
Minor fixes
fballiano e98bd5d
phpdoc
fballiano b14b9dd
Merge branch 'main' into patch-17
fballiano 966932a
copyright and phpcs
fballiano 8966330
phpstan
fballiano d832611
Merge branch 'main' into patch-17
sreichel ff818db
Merge branch 'main' into patch-17
sreichel 5a36615
Merge branch 'main' into patch-17
sreichel 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
<?php | ||
/** | ||
* OpenMage | ||
* | ||
* NOTICE OF LICENSE | ||
* | ||
* This source file is subject to the Open Software License (OSL 3.0) | ||
* that is bundled with this package in the file LICENSE.txt. | ||
* It is also available at https://opensource.org/license/osl-3-0-php | ||
* | ||
* @category Varien | ||
* @package Varien_Image | ||
* @copyright Copyright (c) 2024 The OpenMage Contributors (https://www.openmage.org) | ||
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
*/ | ||
|
||
class Varien_Image_Adapter_Imagick extends Varien_Image_Adapter_Abstract | ||
{ | ||
/** | ||
* @var Imagick | ||
*/ | ||
protected $_imageHandler; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
protected $_requiredExtensions = ["imagick"]; | ||
|
||
/** | ||
* Whether image was resized or not | ||
* | ||
* @var bool | ||
*/ | ||
protected $_resized = false; | ||
|
||
public function __construct() | ||
{ | ||
register_shutdown_function(array($this, 'destruct')); | ||
} | ||
|
||
/** | ||
* Destroy object image on shutdown | ||
*/ | ||
public function destruct() | ||
{ | ||
if ($this->_imageHandler) { | ||
$this->_imageHandler->destroy(); | ||
} | ||
} | ||
|
||
/** | ||
* Opens image file. | ||
* | ||
* @param string $filename | ||
* @throws ImagickException | ||
* @throws Mage_Core_Exception | ||
*/ | ||
public function open($filename) | ||
{ | ||
$this->_fileName = $filename; | ||
$this->getMimeType(); | ||
$this->_getFileAttributes(); | ||
|
||
$this->_imageHandler = new Imagick(); | ||
$this->_imageHandler->readImage($filename); | ||
|
||
$orientation = $this->_imageHandler->getImageProperty('exif:Orientation'); | ||
if (!empty($orientation)) { | ||
switch ($orientation) { | ||
case 1: | ||
// Do nothing | ||
break; | ||
case 3: | ||
$this->_imageHandler->rotateImage('#000000', 180); | ||
break; | ||
case 6: | ||
$this->_imageHandler->rotateImage('#000000', 90); | ||
break; | ||
case 8: | ||
$this->_imageHandler->rotateImage('#000000', -90); | ||
break; | ||
default: | ||
Mage::throwException('Unsupported EXIF orientation: ' . $orientation); | ||
} | ||
} | ||
$this->refreshImageDimensions(); | ||
} | ||
|
||
public function save($destination = null, $newName = null) | ||
{ | ||
if ($destination && $newName) { | ||
$fileName = $destination . "/" . $newName; | ||
} elseif (isset($destination) && !isset($newName)) { | ||
$info = pathinfo($destination); | ||
$fileName = $destination; | ||
$destination = $info['dirname']; | ||
} elseif (!isset($destination) && isset($newName)) { | ||
$fileName = $this->_fileSrcPath . "/" . $newName; | ||
} else { | ||
$fileName = $this->_fileSrcPath . $this->_fileSrcName; | ||
} | ||
|
||
$destinationDir = (isset($destination)) ? $destination : $this->_fileSrcPath; | ||
|
||
if (!is_writable($destinationDir)) { | ||
try { | ||
$io = new Varien_Io_File(); | ||
$io->mkdir($destination); | ||
} catch (Exception $e) { | ||
throw new Exception("Unable to write file into directory '{$destinationDir}'. Access forbidden."); | ||
} | ||
} | ||
|
||
// set quality param for PNG file type | ||
$quality = $this->quality(); | ||
if ($quality !== null) { | ||
if ($quality < 1) { | ||
throw new RuntimeException('Image compression quality cannot be < 1'); | ||
} elseif ($quality > 100) { | ||
throw new RuntimeException('Image compression quality cannot be > 100'); | ||
} | ||
$this->_imageHandler->setCompressionQuality($quality); | ||
} | ||
$this->_imageHandler->writeImage($fileName); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function display() | ||
fballiano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
header("Content-type: " . $this->getMimeTypeWithOutFileType()); | ||
echo $this->_imageHandler->getImageBlob(); | ||
} | ||
|
||
/** | ||
* @param int $frameWidth | ||
* @param int $frameHeight | ||
*/ | ||
public function resize($frameWidth = null, $frameHeight = null) | ||
{ | ||
if (!$frameWidth && !$frameHeight) { | ||
throw new RuntimeException('Invalid image dimensions.'); | ||
} | ||
|
||
// calculate lacking dimension | ||
if ($this->_keepFrame) { | ||
if (null === $frameWidth) { | ||
$frameWidth = $frameHeight; | ||
} elseif (null === $frameHeight) { | ||
$frameHeight = $frameWidth; | ||
} | ||
} else { | ||
if (null === $frameWidth) { | ||
$frameWidth = round($frameHeight * ($this->_imageSrcWidth / $this->_imageSrcHeight)); | ||
} elseif (null === $frameHeight) { | ||
$frameHeight = round($frameWidth * ($this->_imageSrcHeight / $this->_imageSrcWidth)); | ||
} | ||
} | ||
|
||
// define coordinates of image inside new frame | ||
$dstWidth = $frameWidth; | ||
$dstHeight = $frameHeight; | ||
|
||
if ($this->_keepAspectRatio) { | ||
// do not make picture bigger, than it is, if required | ||
if ($this->_constrainOnly) { | ||
if (($frameWidth >= $this->_imageSrcWidth) && ($frameHeight >= $this->_imageSrcHeight)) { | ||
$dstWidth = $this->_imageSrcWidth; | ||
$dstHeight = $this->_imageSrcHeight; | ||
} | ||
} | ||
// keep aspect ratio | ||
if ($this->_imageSrcWidth / $this->_imageSrcHeight >= $frameWidth / $frameHeight) { | ||
$dstHeight = ($dstWidth / $this->_imageSrcWidth) * $this->_imageSrcHeight; | ||
} else { | ||
$dstWidth = ($dstHeight / $this->_imageSrcHeight) * $this->_imageSrcWidth; | ||
} | ||
} | ||
|
||
$dstWidth = (int)round($dstWidth); | ||
$dstHeight = (int)round($dstHeight); | ||
$frameWidth = (int)round($frameWidth); | ||
$frameHeight = (int)round($frameHeight); | ||
|
||
|
||
$filter = \Imagick::FILTER_LANCZOS; | ||
$this->_imageHandler->resizeImage($dstWidth, $dstHeight, $filter, 1); | ||
|
||
if ($this->_keepFrame) { | ||
// Add borders top+bottom or left+right | ||
$canvas = new Imagick(); | ||
// TODO support more than just JPG? | ||
$canvas->newImage($frameWidth, $frameHeight, 'white', 'jpg'); | ||
$offsetX = (int)round(($frameWidth - $dstWidth) / 2); | ||
$offsetY = (int)round(($frameHeight - $dstHeight) / 2); | ||
$canvas->compositeImage($this->_imageHandler, \Imagick::COMPOSITE_OVER, $offsetX, $offsetY); | ||
$this->_imageHandler = $canvas; | ||
} | ||
|
||
$this->refreshImageDimensions(); | ||
$this->_resized = true; | ||
} | ||
|
||
/** | ||
* @param $angle float | ||
* @return void | ||
* @throws ImagickException | ||
*/ | ||
public function rotate($angle) | ||
fballiano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$this->_imageHandler->rotateImage($this->imageBackgroundColor, $angle); | ||
$this->refreshImageDimensions(); | ||
} | ||
|
||
public function watermark($watermarkImage, $positionX = 0, $positionY = 0, $watermarkImageOpacity = 30, $repeat = false) | ||
fballiano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
throw new RuntimeException('Watermark is not supported.'); | ||
} | ||
|
||
public function crop($top = 0, $left = 0, $right = 0, $bottom = 0) | ||
{ | ||
if ($left == 0 && $top == 0 && $right == 0 && $bottom == 0) { | ||
return; | ||
} | ||
|
||
$newWidth = $this->_imageSrcWidth - $left - $right; | ||
$newHeight = $this->_imageSrcHeight - $top - $bottom; | ||
|
||
$this->_imageHandler->cropImage($newWidth, $newHeight, $left, $top); | ||
$this->refreshImageDimensions(); | ||
} | ||
|
||
public function checkDependencies() | ||
{ | ||
foreach ($this->_requiredExtensions as $value) { | ||
if (!extension_loaded($value)) { | ||
throw new Exception("Required PHP extension '{$value}' was not loaded."); | ||
} | ||
} | ||
} | ||
|
||
private function refreshImageDimensions() | ||
{ | ||
$this->_imageSrcWidth = $this->_imageHandler->getImageWidth(); | ||
$this->_imageSrcHeight = $this->_imageHandler->getImageHeight(); | ||
} | ||
|
||
/** | ||
* Gives real mime-type with not considering file type field | ||
* | ||
* @return string | ||
*/ | ||
public function getMimeTypeWithOutFileType() | ||
{ | ||
return $this->_fileMimeType; | ||
} | ||
} |
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.