Skip to content

Instantly share code, notes, and snippets.

@michitux
Created April 27, 2020 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michitux/a48c831a5fa48d45705ef1836bbf690d to your computer and use it in GitHub Desktop.
Save michitux/a48c831a5fa48d45705ef1836bbf690d to your computer and use it in GitHub Desktop.
Patch to rotate resized images in DokuWiki based on EXIF orientation
--- a/inc/media.php 2018-12-28 17:46:14.000000000 +0100
+++ b/inc/media.php 2018-08-20 21:05:55.000000000 +0200
@@ -1084,6 +1084,15 @@
$w = (int) $info[0];
$h = (int) $info[1];
+ if ($meta) {
+ $orientation = media_getTag('Orientation', $meta);
+ if ($orientation == 3 || $orientation == 6) {
+ $tmp = $h;
+ $h = $w;
+ $w = $tmp;
+ }
+ }
+
if($meta && ($w > $size || $h > $size)){
$ratio = $meta->getResizeRatio($size, $size);
$w = floor($w * $ratio);
@@ -1694,6 +1703,14 @@
$w = (int) $item['meta']->getField('File.Width');
$h = (int) $item['meta']->getField('File.Height');
+
+ $orientation = media_getTag('Orientation', $item['meta']);
+ if ($orientation == 3 || $orientation == 6) {
+ $tmp = $w;
+ $w = $h;
+ $h = $tmp;
+ }
+
if($w>$size || $h>$size){
if (!$fullscreen) {
$ratio = $item['meta']->getResizeRatio($size);
@@ -2023,6 +2040,16 @@
$info = @getimagesize($file); //get original size
if($info == false) return $file; // that's no image - it's a spaceship!
+ $meta = new JpegMeta($file);
+ if ($meta) {
+ $orientation = media_getTag('Orientation', $meta);
+ if ($orientation == 3 || $orientation == 6) {
+ $tmp = $info[0];
+ $info[0] = $info[1];
+ $info[1] = $tmp;
+ }
+ }
+
if(!$h) $h = round(($w * $info[1]) / $info[0]);
if(!$w) $w = round(($h * $info[0]) / $info[1]);
@@ -2069,6 +2096,16 @@
$info = @getimagesize($file); //get original size
if($info == false) return $file; // that's no image - it's a spaceship!
+ $meta = new JpegMeta($file);
+ if ($meta) {
+ $orientation = media_getTag('Orientation', $meta);
+ if ($orientation == 3 || $orientation == 6) {
+ $tmp = $info[0];
+ $info[0] = $info[1];
+ $info[1] = $tmp;
+ }
+ }
+
// calculate crop size
$fr = $info[0]/$info[1];
$tr = $w/$h;
@@ -2331,6 +2368,33 @@
}
if(!$image) return false;
+ $meta = new JpegMeta($from);
+ if ($meta) {
+ $orientation = media_getTag('Orientation', $meta);
+
+ $degrees = 0;
+
+ if ($orientation == 7 || $orientation == 8) {
+ $degrees = 90;
+ } elseif ($orientation == 5 || $orientation == 6) {
+ $degrees = 270;
+ } elseif ($orientation == 3 || $orientation == 4) {
+ $degrees = 180;
+ }
+
+ if ($degrees) {
+ $rotate = imagerotate($image, $degrees, 0);
+ imagedestroy($image);
+
+ if (!$rotate) {
+ return false;
+ }
+
+ $image = $rotate;
+ }
+ }
+
+
$newimg = false;
if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor") && $ext != 'gif'){
$newimg = @imagecreatetruecolor ($to_w, $to_h);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment