From 5161b1c8fdc8ff69005f864a89127fc18db6d4ed Mon Sep 17 00:00:00 2001 From: Cash Costello Date: Sun, 24 Oct 2010 01:53:21 +0000 Subject: started cleaning up the upload action --- actions/upload.php | 90 ++++++++++++------------------------------------------ lib/image.php | 66 +++++++++++++++++++++++++++++++++++++++ lib/upload.php | 38 +++++++++++++++++++++++ 3 files changed, 124 insertions(+), 70 deletions(-) create mode 100644 lib/upload.php diff --git a/actions/upload.php b/actions/upload.php index 652ae16af..2d7b311bb 100644 --- a/actions/upload.php +++ b/actions/upload.php @@ -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) { diff --git a/lib/image.php b/lib/image.php index 7c7ef2d90..51b31f6d7 100644 --- a/lib/image.php +++ b/lib/image.php @@ -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 index 000000000..a7dd7611d --- /dev/null +++ b/lib/upload.php @@ -0,0 +1,38 @@ + $mem_avail) { + return false; + } + + return true; +} \ No newline at end of file -- cgit v1.2.3