Skip to content

Commit

Permalink
Imagick: fix image rotation before resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
buschmann23 committed Nov 22, 2024
1 parent ba63291 commit 296fd83
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions src/HbnImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,45 +192,44 @@ public function resizeImageWithImagick(string $cacheFilePath, string $origFilePa
return false;
}

switch ($this->imagick->getImageOrientation()) {
case \Imagick::ORIENTATION_TOPLEFT:
break;
case \Imagick::ORIENTATION_TOPRIGHT:
$this->imagick->flopImage();
break;
case \Imagick::ORIENTATION_BOTTOMRIGHT:
$this->imagick->rotateImage("#000", 180);
break;
case \Imagick::ORIENTATION_BOTTOMLEFT:
$this->imagick->flopImage();
$this->imagick->rotateImage("#000", 180);
break;
case \Imagick::ORIENTATION_LEFTTOP:
$this->imagick->flopImage();
$this->imagick->rotateImage("#000", -90);
break;
case \Imagick::ORIENTATION_RIGHTTOP:
$this->imagick->rotateImage("#000", 90);
break;
case \Imagick::ORIENTATION_RIGHTBOTTOM:
$this->imagick->flopImage();
$this->imagick->rotateImage("#000", 90);
break;
case \Imagick::ORIENTATION_LEFTBOTTOM:
$this->imagick->rotateImage("#000", -90);
break;
default: // Invalid orientation
break;
}
$this->imagick->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT);

if (!$this->imagick->resizeImage($width, $height, \Imagick::FILTER_LANCZOS, 1)) {
$this->log("Imagick: Failed to resize image {$origFilePath}", Log::ERROR);
return false;
}

if ($this->options['stripmetadata'] !== 0) {

switch ($this->imagick->getImageOrientation()) {
case \Imagick::ORIENTATION_TOPLEFT:
break;
case \Imagick::ORIENTATION_TOPRIGHT:
$this->imagick->flopImage();
break;
case \Imagick::ORIENTATION_BOTTOMRIGHT:
$this->imagick->rotateImage("#000", 180);
break;
case \Imagick::ORIENTATION_BOTTOMLEFT:
$this->imagick->flopImage();
$this->imagick->rotateImage("#000", 180);
break;
case \Imagick::ORIENTATION_LEFTTOP:
$this->imagick->flopImage();
$this->imagick->rotateImage("#000", -90);
break;
case \Imagick::ORIENTATION_RIGHTTOP:
$this->imagick->rotateImage("#000", 90);
break;
case \Imagick::ORIENTATION_RIGHTBOTTOM:
$this->imagick->flopImage();
$this->imagick->rotateImage("#000", 90);
break;
case \Imagick::ORIENTATION_LEFTBOTTOM:
$this->imagick->rotateImage("#000", -90);
break;
default: // Invalid orientation
break;
}
$this->imagick->setImageOrientation(\Imagick::ORIENTATION_TOPLEFT);

if (!$this->imagick->stripImage()) {
$this->log("Imagick: Failed to strip metadata from {$origFilePath}", Log::WARNING);
}
Expand Down

0 comments on commit 296fd83

Please sign in to comment.