Update image upload
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 14 Nov 2016 20:37:43 +0000 (15:37 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 14 Nov 2016 20:37:43 +0000 (15:37 -0500)
For the front end form.
Need to process the image better.

index.php
models/front/events/frontAdd.php

index ef6b7b6..b7cb65c 100644 (file)
--- a/index.php
+++ b/index.php
@@ -3,7 +3,7 @@
  * Plugin Name: GLM Members Database Events
  * Plugin URI: http://www.gaslightmedia.com/
  * Description: Gaslight Media Members Database.
- * Version: 1.5.4
+ * Version: 1.5.5
  * Author: Chuck Scott
  * Author URI: http://www.gaslightmedia.com/
  * License: GPL2
@@ -20,7 +20,7 @@
  * @package glmMembersDatabaseEventsAddOn
  * @author Chuck Scott <cscott@gaslightmedia.com>
  * @license http://www.gaslightmedia.com Gaslightmedia
- * @version 1.5.4
+ * @version 1.5.5
  */
 
 /*
@@ -38,7 +38,7 @@
  *  so that we're sure the other add-ons see an up to date
  *  version from this plugin.
  */
-define('GLM_MEMBERS_EVENTS_PLUGIN_VERSION', '1.5.4');
+define('GLM_MEMBERS_EVENTS_PLUGIN_VERSION', '1.5.5');
 define('GLM_MEMBERS_EVENTS_PLUGIN_DB_VERSION', '0.1.1');
 
 // This is the minimum version of the GLM Members DB plugin require for this plugin.
index 8182a2a..5608882 100644 (file)
@@ -99,6 +99,80 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
             )
         );
     }
+    public function uploadImage( $image, $imageName )
+    {
+        // Get new image using temporary file name
+        $newImage = wp_get_image_editor( $image );
+
+        // If we have a good image
+        if ( ! is_wp_error( $newImage ) ) {
+
+            // Check if there's a prefix that should be included in the image file name
+            $prefix = '';
+            if (isset($f['i_prefix']) && trim($f['i_prefix']) != '') {
+                $prefix = $f['i_prefix'];
+            }
+
+            // Get the desired file name and add a timestamp to it to ensure that it's unique
+            $fInfo = pathinfo( $imageName );
+
+            // Strip all but permitted characters from the file name.
+            $fInfo['filename'] = preg_replace('/([^a-zA-Z0-9-_\.]+)/','_', $fInfo['filename']);
+
+            $newFilename = $prefix.strtolower($fInfo['filename'].'_'.time().'.'.$fInfo['extension']);
+
+            // Get image temp file name - Not currently using, but should be using to check for resizing sanity
+            $size = $newImage->get_size();
+
+            // Try to store the image using that file name in the 'original' directory
+            $storedImage = $newImage->save( GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$newFilename );
+
+            // Now resize the images using $sizeArray
+            $sizes = $newImage->multi_resize($this->config['imageSizes']);
+
+            // Finally, move the files to the various size directories and rename them back to the correct name
+            reset($this->config['imageSizes']);
+            while (list($k, $v) = each($this->config['imageSizes'])) {
+
+                // Check if size directory needs to be made
+                if (!file_exists(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$k)) {
+                    mkdir(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$k);
+                }
+
+                // If there's an entry in the $sizes array, it means that the image resized to this size
+                if (isset($sizes[$k])) {
+
+                    // Move resized file to desired direectory
+                    rename(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$sizes[$k]['file'], GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$k.'/'.$newFilename);
+                    if (is_file(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$sizes[$k]['file'])) {
+                        unlink(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$sizes[$k]['file']);
+                    }
+
+                } else {
+
+                    // Image didn't resize to this size, probably because it was smaller than the destination size (silly WP Image Editor class) - so just use the original
+                    copy(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$newFilename, GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$k.'/'.$newFilename);
+
+                }
+            }
+
+            // Move the original to original directory
+            if (!file_exists(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/original')) {
+                mkdir(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/original');
+            }
+            rename(GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/'.$newFilename, GLM_MEMBERS_PLUGIN_IMAGES_PATH.'/original/'.$newFilename);
+
+            $current_img = $newFilename;
+
+        } else {
+
+            if (is_admin() && GLM_MEMBERS_PLUGIN_ADMIN_DEBUG_VERBOSE && class_exists('glmMembersAdmin')) {
+                $this->addDataAbstractNotice($newImage, 'DataBlock', "DataAbstract - imageInput() wp_get_image_editor($tmpFile) Error");
+            }
+
+        }
+        return $current_img;
+    }
     /**
      * modelAction
      *
@@ -311,17 +385,7 @@ class GLmMembersFront_events_frontAdd extends GlmDataEvents
                 $allowed = array('jpeg', 'png', 'jpg', 'bmp', 'gif','svg');
                 if(in_array($imageExt, $allowed)){
                     if($imageError === 0){
-                        if($imageSize <= 1000000){
-                            $imageName = $tmpName . "-" . uniqid() . "." . $imageExt;
-                            $imgUrl = GLM_MEMBERS_PLUGIN_MEDIA_PATH . "/images/small/" . $imageName;
-                            if(!file_exists($imgUrl)){
-                               if(move_uploaded_file($imageTmp, $imgUrl)){
-
-                               }
-                           } else {
-                                echo "Image Already Exists";
-                           }
-                       }
+                        $imageName = $this->uploadImage( $imageTmp, $imageName);
                     }
                 }
                 //file upload settings