function imagepicker_upload_form_submit in Image Picker 7
Same name and namespace in other branches
- 5.2 imagepicker.module \imagepicker_upload_form_submit()
- 5 imagepicker.module \imagepicker_upload_form_submit()
- 6.2 imagepicker.upload.inc \imagepicker_upload_form_submit()
Submit form
File
- ./
imagepicker.upload.inc, line 231
Code
function imagepicker_upload_form_submit($form, &$form_state) {
$success = 0;
$errormsg = '';
if ($form_state['values']['op'] == t('Upload')) {
if (isset($form_state['values']['account']) && isset($form_state['values']['admin'])) {
$user = user_load($form_state['values']['account']);
}
else {
global $user;
}
// absolute paths
$destdir = imagepicker_get_path(FALSE, isset($form_state['values']['admin']) ? array(
'name' => $user->name,
'uid' => $user->uid,
) : TRUE);
$thumbsdir = $destdir . IMAGEPICKER_THUMBS_DIR;
$browserdir = $destdir . IMAGEPICKER_BROWSER_DIR;
$origdir = $destdir . IMAGEPICKER_ORIG_DIR;
// relative paths
$destdirscheme = imagepicker_get_path(FALSE, isset($form_state['values']['admin']) ? array(
'name' => $user->name,
'uid' => $user->uid,
) : TRUE, TRUE);
$thumbsdirscheme = $destdirscheme . IMAGEPICKER_THUMBS_DIR . DIRECTORY_SEPARATOR;
$browserdirscheme = $destdirscheme . IMAGEPICKER_BROWSER_DIR . DIRECTORY_SEPARATOR;
$origdirscheme = $destdirscheme . IMAGEPICKER_ORIG_DIR . DIRECTORY_SEPARATOR;
if (file_prepare_directory($destdir, FILE_CREATE_DIRECTORY) && file_prepare_directory($thumbsdir, FILE_CREATE_DIRECTORY) && file_prepare_directory($browserdir, FILE_CREATE_DIRECTORY) && file_prepare_directory($origdir, FILE_CREATE_DIRECTORY)) {
// clear out the noisy 'created' messages
drupal_get_messages('status', TRUE);
// Add DIRECTORY_SEPARATORS here because drupals' functions remove trailing slashes
$destdir .= DIRECTORY_SEPARATOR;
$thumbsdir .= DIRECTORY_SEPARATOR;
$browserdir .= DIRECTORY_SEPARATOR;
$origdir .= DIRECTORY_SEPARATOR;
$max_uploads = imagepicker_variable_get('imagepicker_max_uploads', 1);
for ($ct = 1; $ct <= $max_uploads; $ct++) {
$maxthumbsize = $form_state['values']['thumb_' . $ct] ? $form_state['values']['thumb_' . $ct] : 100;
$scaleto = $form_state['values']['scale_' . $ct] ? $form_state['values']['scale_' . $ct] : FALSE;
$destination = $origdirscheme;
foreach ($form_state['values'] as $name => $value) {
if ($name == 'file_upload_' . $ct && !empty($_FILES['files']['name'][$name])) {
// save the original and use that from here on
if ($imagemoved = imagepicker_copy_uploaded_file($destination, $name)) {
$file = $imagemoved;
// $source is absolute
$source = $origdir . $file;
// enforce a max size
$max_scale = imagepicker_variable_get('imagepicker_max_scale', '');
if ($max_scale) {
if ($scaleto && $scaleto > $max_scale) {
$scaleto = $max_scale;
}
else {
if ($info = image_get_info($source)) {
if ($info['width'] > $max_scale || $info['height'] > $max_scale) {
$scaleto = $max_scale;
}
}
}
}
if ($scaleto) {
$imagescaled = imagepicker_scale_image($source, IMAGEPICKER_FILE_SCHEME . $destdirscheme . $file, $scaleto);
}
else {
// no scaling, save direct from $origdir to $destdirscheme
imagepicker_file_unmanaged_copy($source, IMAGEPICKER_FILE_SCHEME . $destdirscheme, FILE_EXISTS_RENAME);
}
}
if (!$scaleto && $imagemoved || $scaleto && $imagescaled) {
// if watermark is enabled just apply to destdir image, not orig or the thumbs
if (isset($form_state['values']['watermark_' . $ct]) && $form_state['values']['watermark_' . $ct]) {
$perc = isset($form_state['values']['watermark_perc' . $ct]) ? $form_state['values']['watermark_perc' . $ct] : variable_get('image_jpeg_quality', 75);
if (!imagepicker_watermark_do($destdir . $file, $user, $perc)) {
drupal_set_message(t('Error while watermarking an uploaded image.'), 'error');
}
}
if (imagepicker_scale_image($source, IMAGEPICKER_FILE_SCHEME . $thumbsdirscheme . $file, $maxthumbsize)) {
imagepicker_scale_image($source, IMAGEPICKER_FILE_SCHEME . $browserdirscheme . $file, imagepicker_variable_get('imagepicker_default_browser_thumbnail_size', imagepicker_variable_get('imagepicker_default_browser_thumbnail_size', 100), $user->uid));
$title = htmlspecialchars($form_state['values']['title_' . $ct]);
$description = htmlspecialchars($form_state['values']['description_' . $ct]);
$nextimgid = imagepicker_insert_image($user->uid, $file, $title, $description);
if ($nextimgid) {
if (isset($form_state['values']['grouplist']) && is_array($form_state['values']['grouplist']) && imagepicker_variable_get('imagepicker_groups_enabled', 1) && imagepicker_variable_get('imagepicker_groups_in_upload_enabled', 1)) {
$query = db_select('imagepicker', 'i');
$query
->fields('i', array(
'img_id',
));
$query
->condition('uid', $user->uid)
->condition('img_name', $file);
$record = $query
->execute()
->fetchObject();
foreach ($form_state['values']['grouplist'] as $gid) {
$record->gid = $gid;
imagepicker_insert_group_image($record);
}
}
// external functions
if (imagepicker_variable_get('imagepicker_invoke_extra', 0)) {
module_invoke_all('imagepicker_upload_extra', $nextimgid);
}
$success++;
}
else {
file_unmanaged_delete($thumbsdir . $file);
file_unmanaged_delete($browserdir . $file);
file_unmanaged_delete($origdir . $file);
file_unmanaged_delete($destdir . $file);
$errormsg = t('Error while saving information to database for uploaded image.');
}
}
else {
$errormsg = t('Error while creating a thumbnail for uploaded image.');
}
}
else {
if (!$scaleto && !$imagemoved) {
$errormsg = t('Error while moving uploaded file to its destination.');
}
else {
$errormsg = t('Error while scaling uploaded file.');
}
}
}
}
}
// ct
}
else {
$errormsg = t('Unable to create a directory structure for your images.');
}
}
if ($success) {
drupal_set_message(t('%c successfully uploaded.', array(
'%c' => format_plural($success, '1 image', '@count images'),
)));
if (isset($form_state['values']['admin'])) {
$outpath = IMAGEPICKER_ADMIN_PATH . '/images/user/' . $user->uid . '/browse';
}
elseif (isset($form_state['values']['account'])) {
$outpath = 'user/' . $user->uid . '/imagepicker/images/browse';
}
else {
$outpath = 'imagepicker/browse' . ($success == 1 ? '/' . $nextimgid : '');
}
drupal_goto($outpath);
}
else {
if (!$errormsg) {
$max_filesize = ini_get('upload_max_filesize');
$errormsg = t('No files uploaded. This may be because the files totalled more than %c', array(
'%c' => $max_filesize,
));
}
drupal_set_message($errormsg, 'error');
}
}