]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
started cleaning up the upload action
authorCash Costello <cash.costello@gmail.com>
Sun, 24 Oct 2010 01:53:21 +0000 (01:53 +0000)
committerCash Costello <cash.costello@gmail.com>
Sun, 24 Oct 2010 01:53:21 +0000 (01:53 +0000)
actions/upload.php
lib/image.php
lib/upload.php [new file with mode: 0644]

index 652ae16afc7c6198f03ccb23194f6a0a4a4dd48e..2d7b311bb997d4f49fc4eba375861b916970967e 100644 (file)
@@ -5,8 +5,7 @@
  * This will upload up to 10 images at at time to an album
  */
 
-include dirname(dirname(__FILE__)) . "/lib/resize.php";
-include dirname(dirname(__FILE__)) . "/lib/exif.php";
+include_once dirname(dirname(__FILE__)) . "/lib/upload.php";
 
 // Get common variables
 $access_id = (int) get_input("access_id");
@@ -33,6 +32,9 @@ if (!$image_lib) {
        $image_lib = "GD";
 }
 
+$img_river_view = get_plugin_setting('img_river_view', 'tidypics');
+
+
 // post limit exceeded
 if (count($_FILES) == 0) {
        trigger_error('Tidypics warning: user exceeded post limit on image upload', E_USER_WARNING);
@@ -57,17 +59,6 @@ $uploaded_images = array();
 $not_uploaded = array();
 $error_msgs = array();
 
-$img_river_view = get_plugin_setting('img_river_view', 'tidypics');
-
-$accepted_formats = array(
-               'image/jpeg',
-               'image/png',
-               'image/gif',
-               'image/pjpeg',
-               'image/x-png',
-);
-
-
 foreach($_FILES as $key => $sent_file) {
 
        // skip empty entries
@@ -89,8 +80,8 @@ foreach($_FILES as $key => $sent_file) {
                continue;
        }
 
-       //make sure file is an image
-       if (!in_array($mime, $accepted_formats)) {
+       // must be an image
+       if (!tp_upload_check_format($mime)) {
                array_push($not_uploaded, $sent_file['name']);
                array_push($error_msgs, elgg_echo('tidypics:not_image'));
                continue;
@@ -112,44 +103,25 @@ foreach($_FILES as $key => $sent_file) {
                continue;
        }
 
-       // make sure the in memory image size does not exceed memory available - GD only
+       // make sure the in memory image size does not exceed memory available
        $imginfo = getimagesize($sent_file['tmp_name']);
-       $mem_avail = ini_get('memory_limit');
-       $mem_avail = rtrim($mem_avail, 'M');
-       $mem_avail = $mem_avail * 1024 * 1024;
-       if ($image_lib == 'GD') {
-               $mem_required = ceil(5.35 * $imginfo[0] * $imginfo[1]);
-
-               $mem_used = memory_get_usage();
-
-               $mem_avail = $mem_avail - $mem_used - 2097152; // 2 MB buffer
-               if ($mem_required > $mem_avail) {
-                       array_push($not_uploaded, $sent_file['name']);
-                       array_push($error_msgs, elgg_echo('tidypics:image_pixels'));
-                       trigger_error('Tidypics warning: image memory size too large for resizing so rejecting', E_USER_WARNING);
-                       continue;
-               }
-       } else if ($image_lib == 'ImageMagickPHP') {
-               // haven't been able to determine a limit like there is for GD
+       if (!tp_upload_memory_check($image_lib, $imginfo[0] * $imginfo[1])) {
+               array_push($not_uploaded, $sent_file['name']);
+               array_push($error_msgs, elgg_echo('tidypics:image_pixels'));
+               trigger_error('Tidypics warning: image memory size too large for resizing so rejecting', E_USER_WARNING);
+               continue;
        }
 
        //this will save to users folder in /image/ and organize by photo album
-       $prefix = "image/" . $container_guid . "/";
-       $file = new ElggFile();
-       $filestorename = strtolower(time().$name);
-       $file->setFilename($prefix.$filestorename);
+       $file = new TidypicsImage();
+       $file->container_guid = $container_guid;
        $file->setMimeType($mime);
-       $file->originalfilename = $name;
-       $file->subtype="image";
        $file->simpletype="image";
        $file->access_id = $access_id;
-       //$file->title = substr($file->originalfilename, 0, strrpos($file->originalfilename, '.'));
-       if ($container_guid) {
-               $file->container_guid = $container_guid;
-       }
-       $file->open("write");
-       $file->write(get_uploaded_file($key));
-       $file->close();
+       //$file->title = substr($name, 0, strrpos($name, '.'));
+
+       $file->setOriginalFilename($name);
+       $file->saveImageFile(get_uploaded_file($key));
        $result = $file->save();
 
        if (!$result) {
@@ -158,30 +130,8 @@ foreach($_FILES as $key => $sent_file) {
                continue;
        }
 
-       //get and store the exif data
-       td_get_exif($file);
-
-
-       // resize photos to create thumbnails
-       if ($image_lib == 'ImageMagick') { // ImageMagick command line
-
-               if (tp_create_im_cmdline_thumbnails($file, $prefix, $filestorename) != true) {
-                       trigger_error('Tidypics warning: failed to create thumbnails - ImageMagick command line', E_USER_WARNING);
-               }
-
-       } else if ($image_lib == 'ImageMagickPHP') {  // imagick php extension
-
-               if (tp_create_imagick_thumbnails($file, $prefix, $filestorename) != true) {
-                       trigger_error('Tidypics warning: failed to create thumbnails - ImageMagick PHP', E_USER_WARNING);
-               }
-
-       } else {
-
-               if (tp_create_gd_thumbnails($file, $prefix, $filestorename) != true) {
-                       trigger_error('Tidypics warning: failed to create thumbnails - GD', E_USER_WARNING);
-               }
-
-       } // end of image library selector
+       $file->extractExifData();
+       $file->saveThumbnails($image_lib);
 
        //keep one file handy so we can add a notice to the river if single image option selected
        if (!$file_for_river) {
index 7c7ef2d90e20405e903ec2cb5edbb3668f463703..51b31f6d706c187487720da9b837b6e1988a0a37 100644 (file)
@@ -38,6 +38,72 @@ class TidypicsImage extends ElggFile {
                return parent::delete();
        }
 
+       /**
+        * Set the internal filenames
+        * 
+        * @warning container needs to be set first
+        */
+       public function setOriginalFilename($originalName) {
+               $prefix = "image/" . $this->container_guid . "/";
+               $filestorename = strtolower(time() . $originalName);
+               $this->setFilename($prefix . $filestorename);
+               $this->originalfilename = $originalName;
+       }
+
+       /**
+        * Save the uploaded image
+        *
+        * @warning filename needs to be set first
+        * 
+        * @param string $image
+        */
+       public function saveImageFile($image) {
+               $this->open("write");
+               $this->write($image);
+               $this->close();
+       }
+
+       /**
+        * Save the image thumbnails
+        *
+        * @warning container guid and filename must be set
+        * 
+        * @param string $imageLib
+        */
+       public function saveThumbnails($imageLib) {
+               include_once dirname(__FILE__) . "/resize.php";
+               
+               $prefix = "image/" . $this->container_guid . "/";
+               $filename = $this->getFilename();
+               $filename = substr($filename, strrpos($filename, '/') + 1);
+               
+               if ($imageLib == 'ImageMagick') {
+                       // ImageMagick command line
+                       if (tp_create_im_cmdline_thumbnails($this, $prefix, $filename) != true) {
+                               trigger_error('Tidypics warning: failed to create thumbnails - ImageMagick command line', E_USER_WARNING);
+                       }
+               } else if ($imageLib == 'ImageMagickPHP') {
+                       // imagick php extension
+                       if (tp_create_imagick_thumbnails($this, $prefix, $filename) != true) {
+                               trigger_error('Tidypics warning: failed to create thumbnails - ImageMagick PHP', E_USER_WARNING);
+                       }
+               } else {
+                       if (tp_create_gd_thumbnails($this, $prefix, $filename) != true) {
+                               trigger_error('Tidypics warning: failed to create thumbnails - GD', E_USER_WARNING);
+                       }
+               }
+       }
+
+       /**
+        * Extract EXIF Data from image
+        *
+        * @warning image file must be saved first
+        */
+       public function extractExifData() {
+               include_once dirname(__FILE__) . "/exif.php";
+               td_get_exif($this);
+       }
+       
        /**
         * Has the photo been tagged with "in this photo" tags
         *
diff --git a/lib/upload.php b/lib/upload.php
new file mode 100644 (file)
index 0000000..a7dd761
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+/**
+ * Helper library for working with uploads
+ */
+
+function tp_upload_check_format($mime) {
+       $accepted_formats = array(
+               'image/jpeg',
+               'image/png',
+               'image/gif',
+               'image/pjpeg',
+               'image/x-png',
+       );
+
+       if (!in_array($mime, $accepted_formats)) {
+               return false;
+       }
+       return true;
+}
+
+function tp_upload_memory_check($image_lib, $num_pixels) {
+       if ($image_lib !== 'GD') {
+               return true;
+       }
+
+       $mem_avail = ini_get('memory_limit');
+       $mem_avail = rtrim($mem_avail, 'M');
+       $mem_avail = $mem_avail * 1024 * 1024;
+       $mem_used = memory_get_usage();
+       $mem_required = ceil(5.35 * $num_pixels);
+
+       $mem_avail = $mem_avail - $mem_used - 2097152; // 2 MB buffer
+       if ($mem_required > $mem_avail) {
+               return false;
+       }
+
+       return true;
+}
\ No newline at end of file