From 82519e3e23c5c56db420a64b7d17ca524720d1a2 Mon Sep 17 00:00:00 2001 From: Cash Costello Date: Sat, 16 May 2009 18:33:35 +0000 Subject: cleaning up upload code --- actions/upload.php | 420 ++++++++++++++++++++++++++--------------------------- languages/en.php | 4 +- 2 files changed, 210 insertions(+), 214 deletions(-) diff --git a/actions/upload.php b/actions/upload.php index 3357fe469..3bfd35367 100644 --- a/actions/upload.php +++ b/actions/upload.php @@ -14,9 +14,6 @@ if (!$container_guid) $container_guid == $_SESSION['user']->getGUID(); - $not_uploaded = array(); - $uploaded_images = array(); - // test to make sure at least 1 image was selected by user $num_images = 0; foreach($_FILES as $key => $sent_file) { @@ -29,224 +26,223 @@ forward(get_input('forward_url', $_SERVER['HTTP_REFERER'])); } - + $not_uploaded = array(); + $uploaded_images = array(); foreach($_FILES as $key => $sent_file) { - if (!empty($sent_file['name'])) { - $name = $_FILES[$key]['name']; - $mime = $_FILES[$key]['type']; - - //error_log('Name ' . $sent_file['name']); - //error_log('MIME ' . $sent_file['type']); - //error_log('error ' . $sent_file['error']); - //error_log('Size ' . $sent_file['size']); + + // skip empty entries + if (empty($sent_file['name'])) + continue; + + $name = $sent_file['name']; + $mime = $sent_file['type']; + + if ($sent_file['error']) { + array_push($not_uploaded, $sent_file['name']); + continue; + } + + //make sure file is an image + if ($mime != 'image/jpeg' && $mime != 'image/gif' && $mime != 'image/png' && $mime != 'image/pjpeg') { + array_push($not_uploaded, $sent_file['name']); + continue; + } - //make sure file is an image - if ($mime == 'image/jpeg' || $mime == 'image/gif' || $mime == 'image/png' || $mime == 'image/pjpeg') { - //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->setMimeType($mime); - $file->originalfilename = $name; - $file->subtype="image"; - $file->access_id = $access_id; - if ($container_guid) { - $file->container_guid = $container_guid; - } - $file->open("write"); - $file->write(get_uploaded_file($key)); - $file->close(); - $result = $file->save(); + // make sure file does not exceed limit + $maxfilesize = get_plugin_setting('maxfilesize','tidypics'); + if (!$maxfilesize) + $maxfilesize = 5; // default to 5 MB if not set + + $maxfilesize = 1024 * 1024 * $maxfilesize; // convert to bytes from MBs + if ($sent_file['size'] > $maxfilesize) { + array_push($not_uploaded, $sent_file['name']); + continue; + } - if ($result) { //file was saved; now create some thumbnails - //get maximum file size from plugin settings - if (get_plugin_setting('maxfilesize','tidypics')) { - if (((int) get_plugin_setting('maxfilesize','tidypics')) < 1 || ((int) get_plugin_setting('maxfilesize','tidypics')) > 1048576) { - $maxfilesize = 10240; //if file size is less than 1KB or greater than 1GB, default to 10MB - } else { - $maxfilesize = (int) get_plugin_setting('maxfilesize','tidypics'); - } - } else { - $maxfilesize = 10240; //if the file size limit is not set, default to 10MB - } - $maxfilesize = 1024 * $maxfilesize; //convert to bytes - - //check file size and remove picture if it exceeds the maximum - if (filesize($file->getFilenameOnFilestore())<= $maxfilesize) { - array_push($uploaded_images, $file->guid); + //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->setMimeType($mime); + $file->originalfilename = $name; + $file->subtype="image"; + $file->access_id = $access_id; + if ($container_guid) { + $file->container_guid = $container_guid; + } + $file->open("write"); + $file->write(get_uploaded_file($key)); + $file->close(); + $result = $file->save(); - $image_lib = get_plugin_setting('image_lib', 'tidypics'); + if (!$result) { + array_push($not_uploaded, $sent_file['name']); + continue; + } + + // successfully saved image + array_push($uploaded_images, $file->guid); - if ($image_lib === 'GD') { - - // Generate thumbnail - //TODO: This code needs a complete rewrite - hardcoded to ~2.5 MB - if (filesize($file->getFilenameOnFilestore())<= 2500000) { - try { - $thumblarge = get_resized_image_from_existing_file( $file->getFilenameOnFilestore(), - $CONFIG->tidypics->image_large_width, - $CONFIG->tidypics->image_large_height, - false); - } catch (Exception $e) { $thumblarge = false; } - - try { - $thumbsmall = get_resized_image_from_existing_file( $file->getFilenameOnFilestore(), - $CONFIG->tidypics->image_small_width, - $CONFIG->tidypics->image_small_height, - true); - } catch (Exception $e) { $thumbsmall = false; } - - try { - $thumbnail = get_resized_image_from_existing_file( $file->getFilenameOnFilestore(), - $CONFIG->tidypics->image_thumb_width, - $CONFIG->tidypics->image_thumb_height, - true); - } catch (Exception $e) { $thumbnail = false; } - } - - if ($thumbnail) { - $thumb = new ElggFile(); - $thumb->setMimeType($mime); - $thumb->setFilename($prefix."thumb".$filestorename); - $thumb->open("write"); - if ($thumb->write($thumbnail)) { - $file->thumbnail = $prefix."thumb".$filestorename; - } else { - $thumb->delete(); - } - $thumb->close(); - } - - if ($thumbsmall) { - $thumb = new ElggFile(); - $thumb->setMimeType($mime); - $thumb->setFilename($prefix."smallthumb".$filestorename); - $thumb->open("write"); - if ($thumb->write($thumbsmall)) { - $file->smallthumb = $prefix."smallthumb".$filestorename; - } else { - $thumb->delete(); - } - $thumb->close(); - } - - if ($thumblarge) { - $thumb = new ElggFile(); - $thumb->setMimeType($mime); - $thumb->setFilename($prefix."largethumb".$filestorename); - $thumb->open("write"); - if ($thumb->write($thumblarge)) { - $file->largethumb = $prefix."largethumb".$filestorename; - } else { - $thumb->delete(); - } - $thumb->close(); - } - - } else { + $image_lib = get_plugin_setting('image_lib', 'tidypics'); + + if ($image_lib === 'GD') { + + // Generate thumbnail + //TODO: This code needs a complete rewrite - hardcoded to ~2.5 MB + if (filesize($file->getFilenameOnFilestore())<= 2500000) { + try { + $thumblarge = get_resized_image_from_existing_file( $file->getFilenameOnFilestore(), + $CONFIG->tidypics->image_large_width, + $CONFIG->tidypics->image_large_height, + false); + } catch (Exception $e) { $thumblarge = false; } + try { + $thumbsmall = get_resized_image_from_existing_file( $file->getFilenameOnFilestore(), + $CONFIG->tidypics->image_small_width, + $CONFIG->tidypics->image_small_height, + true); + } catch (Exception $e) { $thumbsmall = false; } + + try { + $thumbnail = get_resized_image_from_existing_file( $file->getFilenameOnFilestore(), + $CONFIG->tidypics->image_thumb_width, + $CONFIG->tidypics->image_thumb_height, + true); + } catch (Exception $e) { $thumbnail = false; } + } + - //gfroese: build the actual thumbnails now - $album = get_entity($container_guid); - $user = get_user_entity_as_row($album->owner_guid); - $username = $user->username; - - try { - $thumblarge = tp_resize($file->getFilenameOnFilestore(), - "largethumb", - $CONFIG->tidypics->image_large_width, - $CONFIG->tidypics->image_large_height, - false); - } catch (Exception $e) { $thumblarge = false; } - try { - $thumbsmall = tp_resize($file->getFilenameOnFilestore(), - "smallthumb", - $CONFIG->tidypics->image_small_width, - $CONFIG->tidypics->image_small_height, - true); - } catch (Exception $e) { $thumbsmall = false; } - try { - $thumbnail = tp_resize($file->getFilenameOnFilestore(), - "thumb", - $CONFIG->tidypics->image_thumb_width, - $CONFIG->tidypics->image_thumb_height, - true); - } catch (Exception $e) { $thumbnail = false; } - - if ($thumbnail) { - $thumb = new ElggFile(); - $thumb->setMimeType($mime); - $thumb->setFilename($prefix."thumb".$filestorename); - $file->thumbnail = $prefix."thumb".$filestorename; - } - - if ($thumbsmall) { - $thumb = new ElggFile(); - $thumb->setMimeType($mime); - $thumb->setFilename($prefix."smallthumb".$filestorename); - $file->smallthumb = $prefix."smallthumb".$filestorename; - } - - if ($thumblarge) { - $thumb = new ElggFile(); - $thumb->setMimeType($mime); - $thumb->setFilename($prefix."largethumb".$filestorename); - $file->largethumb = $prefix."largethumb".$filestorename; - } - - $im_path = get_plugin_setting('convert_command', 'tidypics'); - if(!$im_path) { - $im_path = "/usr/bin/"; - } - if(substr($im_path, strlen($im_path)-1, 1) != "/") $im_path .= "/"; - - $watermark_text = get_plugin_setting('watermark_text', 'tidypics'); - if( $watermark_text ) { //get this value from the plugin settings - if( $thumblarge ) { - $ext = ".png"; - - $watermark_filename = strtolower($watermark_text); - $watermark_filename = preg_replace("/[^\w-]+/", "-", $watermark_filename); - $watermark_filename = trim($watermark_filename, '-'); - - $viewer = get_loggedin_user(); - $user_stamp_base = dirname(__FILE__) . "/" . $viewer->name . "_" . $watermark_filename . "_stamp"; - if( !file_exists( $user_stamp_base . $ext )) { //create the watermark if it doesn't exist - $commands = array(); - $commands[] = $im_path . 'convert -size 300x50 xc:grey30 -pointsize 20 -gravity center -draw "fill grey70 text 0,0 \''. $watermark_text . '\'" '. $user_stamp_base . '_fgnd' . $ext; - $commands[] = $im_path . 'convert -size 300x50 xc:black -pointsize 20 -gravity center -draw "fill white text 1,1 \''. $watermark_text . '\' text 0,0 \''. $watermark_text . '\' fill black text -1,-1 \''. $watermark_text . '\'" +matte ' . $user_stamp_base . '_mask' . $ext; - $commands[] = $im_path . 'composite -compose CopyOpacity ' . $user_stamp_base . "_mask" . $ext . ' ' . $user_stamp_base . '_fgnd' . $ext . ' ' . $user_stamp_base . $ext; - $commands[] = $im_path . 'mogrify -trim +repage ' . $user_stamp_base . $ext; - $commands[] = 'rm ' . $user_stamp_base . '_mask' . $ext; - $commands[] = 'rm ' . $user_stamp_fgnd . '_mask' . $ext; - - foreach( $commands as $command ) { - exec( $command ); - } - } - //apply the watermark - $commands = array(); - $commands[] = $im_path . 'composite -gravity south -geometry +0+10 ' . $user_stamp_base . $ext . ' ' . $thumblarge . ' ' . $thumblarge . '_watermarked'; - $commands[] = "mv $thumblarge" . "_watermarked $thumblarge"; - foreach( $commands as $command ) { - exec( $command ); - } - } - } + if ($thumbnail) { + $thumb = new ElggFile(); + $thumb->setMimeType($mime); + $thumb->setFilename($prefix."thumb".$filestorename); + $thumb->open("write"); + if ($thumb->write($thumbnail)) { + $file->thumbnail = $prefix."thumb".$filestorename; + } else { + $thumb->delete(); + } + $thumb->close(); + } + + if ($thumbsmall) { + $thumb = new ElggFile(); + $thumb->setMimeType($mime); + $thumb->setFilename($prefix."smallthumb".$filestorename); + $thumb->open("write"); + if ($thumb->write($thumbsmall)) { + $file->smallthumb = $prefix."smallthumb".$filestorename; + } else { + $thumb->delete(); + } + $thumb->close(); + } + + if ($thumblarge) { + $thumb = new ElggFile(); + $thumb->setMimeType($mime); + $thumb->setFilename($prefix."largethumb".$filestorename); + $thumb->open("write"); + if ($thumb->write($thumblarge)) { + $file->largethumb = $prefix."largethumb".$filestorename; + } else { + $thumb->delete(); + } + $thumb->close(); + } + + } else { + //gfroese: build the actual thumbnails now + $album = get_entity($container_guid); + $user = get_user_entity_as_row($album->owner_guid); + $username = $user->username; + + try { + $thumblarge = tp_resize($file->getFilenameOnFilestore(), + "largethumb", + $CONFIG->tidypics->image_large_width, + $CONFIG->tidypics->image_large_height, + false); + } catch (Exception $e) { $thumblarge = false; } + try { + $thumbsmall = tp_resize($file->getFilenameOnFilestore(), + "smallthumb", + $CONFIG->tidypics->image_small_width, + $CONFIG->tidypics->image_small_height, + true); + } catch (Exception $e) { $thumbsmall = false; } + try { + $thumbnail = tp_resize($file->getFilenameOnFilestore(), + "thumb", + $CONFIG->tidypics->image_thumb_width, + $CONFIG->tidypics->image_thumb_height, + true); + } catch (Exception $e) { $thumbnail = false; } + + if ($thumbnail) { + $thumb = new ElggFile(); + $thumb->setMimeType($mime); + $thumb->setFilename($prefix."thumb".$filestorename); + $file->thumbnail = $prefix."thumb".$filestorename; + } + + if ($thumbsmall) { + $thumb = new ElggFile(); + $thumb->setMimeType($mime); + $thumb->setFilename($prefix."smallthumb".$filestorename); + $file->smallthumb = $prefix."smallthumb".$filestorename; + } + + if ($thumblarge) { + $thumb = new ElggFile(); + $thumb->setMimeType($mime); + $thumb->setFilename($prefix."largethumb".$filestorename); + $file->largethumb = $prefix."largethumb".$filestorename; + } + + $im_path = get_plugin_setting('convert_command', 'tidypics'); + if(!$im_path) { + $im_path = "/usr/bin/"; + } + if(substr($im_path, strlen($im_path)-1, 1) != "/") $im_path .= "/"; + + $watermark_text = get_plugin_setting('watermark_text', 'tidypics'); + if( $watermark_text ) { //get this value from the plugin settings + if( $thumblarge ) { + $ext = ".png"; + + $watermark_filename = strtolower($watermark_text); + $watermark_filename = preg_replace("/[^\w-]+/", "-", $watermark_filename); + $watermark_filename = trim($watermark_filename, '-'); + + $viewer = get_loggedin_user(); + $user_stamp_base = dirname(__FILE__) . "/" . $viewer->name . "_" . $watermark_filename . "_stamp"; + if( !file_exists( $user_stamp_base . $ext )) { //create the watermark if it doesn't exist + $commands = array(); + $commands[] = $im_path . 'convert -size 300x50 xc:grey30 -pointsize 20 -gravity center -draw "fill grey70 text 0,0 \''. $watermark_text . '\'" '. $user_stamp_base . '_fgnd' . $ext; + $commands[] = $im_path . 'convert -size 300x50 xc:black -pointsize 20 -gravity center -draw "fill white text 1,1 \''. $watermark_text . '\' text 0,0 \''. $watermark_text . '\' fill black text -1,-1 \''. $watermark_text . '\'" +matte ' . $user_stamp_base . '_mask' . $ext; + $commands[] = $im_path . 'composite -compose CopyOpacity ' . $user_stamp_base . "_mask" . $ext . ' ' . $user_stamp_base . '_fgnd' . $ext . ' ' . $user_stamp_base . $ext; + $commands[] = $im_path . 'mogrify -trim +repage ' . $user_stamp_base . $ext; + $commands[] = 'rm ' . $user_stamp_base . '_mask' . $ext; + $commands[] = 'rm ' . $user_stamp_fgnd . '_mask' . $ext; + + foreach( $commands as $command ) { + exec( $command ); } - } else { //file exceeds file size limit, so delete it - $file->delete(); - array_push($not_uploaded, $name); - } //end of file size check - } else { //file was not saved for some unknown reason - array_push($not_uploaded, $name); - } //end of file saved check and thumbnail creation - } else { // file is not a supported image type - array_push($not_uploaded, $name); - } //end of mimetype block - } //end of file name empty check + } + //apply the watermark + $commands = array(); + $commands[] = $im_path . 'composite -gravity south -geometry +0+10 ' . $user_stamp_base . $ext . ' ' . $thumblarge . ' ' . $thumblarge . '_watermarked'; + $commands[] = "mv $thumblarge" . "_watermarked $thumblarge"; + foreach( $commands as $command ) { + exec( $command ); + } + } + } + } // end of image library selector } //end of for loop if (count($not_uploaded) == 0) { diff --git a/languages/en.php b/languages/en.php index 5198817b9..70dc57bc5 100644 --- a/languages/en.php +++ b/languages/en.php @@ -6,7 +6,7 @@ 'image' => "Image", 'images' => "Images", - 'caption' => "Caption", + 'caption' => "Caption", 'photos' => "Photos", 'images:upload' => "Upload Images", 'album' => "Photo Album", @@ -19,7 +19,7 @@ 'album:group' => "Group albums", 'item:object:image' => "Photos", 'item:object:album' => "Albums", - 'tidypics:settings:maxfilesize' => "Maximum file size in kilo bytes (KB):", + 'tidypics:settings:maxfilesize' => "Maximum file size in megabytes (MB):", 'tidypics:settings:watermark' => "Enter text to appear in the watermark - ImageMagick must be selected for the image library", 'tidypics:settings:im_path' => "Enter the path to your ImageMagick commands", 'tidypics:enablephotos' => 'Enable Group Photo Albums', -- cgit v1.2.3