Add function to imageUpload to strip metadata
authorLaury GvR <laury@gaslightmedia.com>
Thu, 22 Jun 2017 13:14:41 +0000 (09:14 -0400)
committerLaury GvR <laury@gaslightmedia.com>
Thu, 22 Jun 2017 13:14:41 +0000 (09:14 -0400)
We were having an issue with EXIF data giving unwelcome image
orientation to uploads. Through Image magick and GD any metadata
is now stripped. If neither is installed, the image will be
unchanged.

models/admin/ajax/imageUpload.php

index 55c76a7..769e0fd 100644 (file)
@@ -193,6 +193,9 @@ class GlmMembersAdmin_ajax_imageUpload extends GlmDataImages
             $newFilename = strtolower($fInfo['filename'].'_'.time().'.'.$fInfo['extension']);
         }
 
+        // Strip meta data off image - this prevents EXIF orientation shenanigans
+        $file['tmp_name'] = $this->stripImageMeta($file['tmp_name']);
+
         // Get new image using temporary file name
         $newImage = wp_get_image_editor($file['tmp_name']);
 
@@ -285,4 +288,44 @@ class GlmMembersAdmin_ajax_imageUpload extends GlmDataImages
 
     }
 
+
+    /*
+     * Strip an image of all its metadata using either imagemagick or GD
+     *
+     * None of the metadata will be preserved. This function can be refined with settings
+     * to preserve certain metadata fields. In case of GD, image type is determined, but
+     * if it is not jpg, png or bmp, no stripping will take place.
+     *
+     * @param $imageName string Image resource to be processed
+     *
+     * @return string
+     *
+     */
+    public function stripImageMeta($imageName) {
+
+        if (extension_loaded('imagick')) {
+            $imageName->stripImage();
+        } else if (extension_loaded('gd') && function_exists('gd_info')) {
+            $image_type = exif_imagetype($imageName);
+            switch ($image_type) {
+                case "IMAGETYPE_JPEG":
+                    $imageName = imagecreatefromjpeg($imageName);
+                    break;
+
+                case "IMAGETYPE_PNG":
+                    $imageName = imagecreatefrompng($imageName);
+                    break;
+
+                case "IMAGETYPE_BMP":
+                    $imageName = imagecreatefrombmp($imageName);
+                    break;
+
+                default:
+                    break;
+            }
+        }
+
+        return $imageName;
+    }
+
 }